Skip to content

Commit

Permalink
qnd: kalcli - added support for printing the log portion of the execu…
Browse files Browse the repository at this point in the history
…ted api call

git-svn-id: svn+ssh://kelev.kaltura.com/usr/local/kalsource/backend/server/trunk/core@100298 6b8eccd3-e8c5-4e7d-8186-e12b5326b719
  • Loading branch information
erank committed Jun 23, 2013
1 parent 6fe90ec commit 7f0df34
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
47 changes: 47 additions & 0 deletions generator/sources/cli/kalcli.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
$DATE_FIELD_SUFFIXES = array('At', 'Date', 'On');
define('MIN_TIME_STAMP', 946677600); // 2000
define('MAX_TIME_STAMP', 2147483647); // 2038
define('API_LOG_FILENAME', '/var/log/kaltura_api_v3.log');

function formatResponse($resp, $indent = '', $varName = null)
{
Expand Down Expand Up @@ -83,6 +84,32 @@ function formatResponse($resp, $indent = '', $varName = null)
}
}

function isLineLogStart($curLine)
{
if (strlen($curLine) < 20)
return false;
if ($curLine[4] == '-' && $curLine[7] == '-' && $curLine[10] == ' ' && $curLine[13] == ':' && $curLine[16] == ':')
return true;
return false;
}

function printLogFiltered($logPortion, $sessionId)
{
$curSession = null;
$logLines = explode("\n", $logPortion);
foreach ($logLines as $logLine)
{
if (isLineLogStart($logLine))
{
$explodedLine = explode(' ', $logLine, 6);
$curSession = substr($explodedLine[4], 1, -1);
}

if ($curSession == $sessionId)
echo $logLine . "\n";
}
}

KalturaSecretRepository::init();

// parse command line
Expand Down Expand Up @@ -212,9 +239,29 @@ function formatResponse($resp, $indent = '', $varName = null)
$curlWrapper->useGet = isset($options['get']);
$curlWrapper->ignoreCertErrors = isset($options['insecure']);

if (isset($options['log']))
{
$initialLogSize = filesize(API_LOG_FILENAME);
}

// issue the request
$result = $curlWrapper->getUrl($url, $params);

if (isset($options['log']))
{
clearstatcache();
$currentLogSize = filesize(API_LOG_FILENAME);

$logPortion = file_get_contents(API_LOG_FILENAME, false, null, $initialLogSize, $currentLogSize - $initialLogSize);

if (preg_match('/X-Kaltura-Session: (\d+)/', $curlWrapper->responseHeaders, $matches))
{
$sessionId = $matches[1];
printLogFiltered($logPortion, $sessionId);
}
die;
}

// output the response
if (isset($options['include']) || isset($options['head']))
echo $curlWrapper->responseHeaders;
Expand Down
1 change: 1 addition & 0 deletions generator/sources/cli/kalcliSwitches.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
$commandLineSwitches = array(
array(KalturaCommandLineParser::SWITCH_NO_VALUE, 'i', 'include', 'Include output headers as well as the response body'),
array(KalturaCommandLineParser::SWITCH_NO_VALUE, 'I', 'head', 'Output only response headers'),
array(KalturaCommandLineParser::SWITCH_NO_VALUE, 'l', 'log', 'Output only the API execution log'),
array(KalturaCommandLineParser::SWITCH_NO_VALUE, 's', 'https', 'Use https transport'),
array(KalturaCommandLineParser::SWITCH_NO_VALUE, 't', 'time', 'Output request execution time'),
array(KalturaCommandLineParser::SWITCH_NO_VALUE, 'g', 'get', 'Use GET instead of POST'),
Expand Down

0 comments on commit 7f0df34

Please sign in to comment.