Skip to content
Snippets Groups Projects
Commit 8057257e authored by alvaro's avatar alvaro
Browse files

Added logging class

GUI implemented

TODO: GUI needs work
parent 61a0a93b
No related branches found
No related tags found
No related merge requests found
......@@ -34,7 +34,7 @@ class Endpoint{
if(is_int($conf['cache']['global']) && $conf['cache']['global'] > 0 && $modified + $conf['cache']['global'] > $now){
if($conf['debug']){
$msg = "Taking data from cache ($cacheFile). Renewal in ".($modified + $conf['cache']['global'] - $now)." seconds\n";
Utils::log($msg);
Logging::log($msg);
echo $msg;
}
$aux = (file_get_contents($cacheFile));
......@@ -53,7 +53,7 @@ class Endpoint{
$aux = curl_exec($c); // execute the curl command
if($conf['debug']){
if($aux == false){
Utils::log("Error executing SPARQL query (".$this->sparqlUrl."): ".curl_error($c), E_USER_ERROR);
Logging::log("Error executing SPARQL query (".$this->sparqlUrl."): ".curl_error($c), E_USER_ERROR);
echo("Error executing SPARQL query (".$this->sparqlUrl."): ".curl_error($c));
}
}
......@@ -64,7 +64,7 @@ class Endpoint{
file_put_contents($cacheFile,($aux), LOCK_EX);
if($conf['debug']){
$msg = "Notice: Writing results in ".$cacheFile."\n";
Utils::log($msg, E_USER_NOTICE);
Logging::log($msg, E_USER_NOTICE);
echo($msg);
}
}
......@@ -73,7 +73,7 @@ class Endpoint{
$r = json_decode($aux, true);
if($conf['debug']){
if($r == false){
Utils::log("Warning: Results from a SELECT sparql query couldn't get parsed", E_USER_WARNING);
Logging::log("Warning: Results from a SELECT sparql query couldn't get parsed", E_USER_WARNING);
echo("Warning: Results from a SELECT sparql query couldn't get parsed");
}
}
......
......@@ -127,7 +127,7 @@ class Importer{
}elseif($compType == LS."LodspeakrVisualComponent"){
$this->createViews($inputs);
}else{
Utils::log("Component '$component' (of type $compType) not supported", E_USER_WARNING);
Logging::log("Component '$component' (of type $compType) not supported", E_USER_WARNING);
}
}
$content .= "?>\n";
......
<?php
class Logging{
public static function init(){
$logs = array();
if ($handle = opendir('cache/')) {
while (false !== ($entry = readdir($handle))) {
if (strpos($entry, ".log") == strlen($entry)-4) {
$logs[] = $entry;
}
}
closedir($handle);
}
sort($logs);
$alogs = "";
foreach($logs as $v){
$alogs .= "<p><a href='#lodspeakr/cache/$v'>$v</a></p>";
}
echo "
<!DOCTYPE html>
<html>
<head>
<meta charset='UTF-8'>
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
<link href='css/bootstrap.min.css' rel='stylesheet' type='text/css' media='screen' />
<style>
body {
padding-top: 60px; /* 60px to make the container go all the way to the bottom of the topbar */
}
</style>
<link href='css/bootstrap-responsive.min.css' rel='stylesheet' type='text/css' media='screen' />
<script type='text/javascript' src='js/jquery.js'></script>
<script type='text/javascript' src='js/bootstrap.min.js'></script>
</head>
<body>
<div class='navbar navbar-fixed-top'>
<div class='navbar-inner'>
<div class='container'>
<a class='btn btn-navbar' data-toggle='collapse' data-target='.nav-collapse'>
<span class='icon-bar'></span>
<span class='icon-bar'></span>
<span class='icon-bar'></span>
</a>
<a class='brand' href='logs'>Logs</a>
<div class='nav-collapse'>
<ul class='nav'>
<li class='active'><a href='#'>Home</a></li>
</ul>
</div><!--/.nav-collapse -->
</div>
</div>
</div>
<div class='row'>
<div class='span4 well'>
<h3>Logs</h3>
$alogs
</div>
<div class='span8' id='log'>
</div>
</div>
<script>
$('a').on('click', function(){
var link = $(this).attr('href').replace('#', '');
$.ajax({
url: link,
dataType: 'json',
success: function(data){
var pres = '';
$.each(data.logs, function(i, item){
pres += '<h4>'+item.timestamp+'</h4><pre>'+item.message+'</pre>';
});
$('#log').html(pres);
}
});
});
</script>
</body>
</html>";
}
public static function log($msg){
global $conf;
$log = array('timestamp' => time());
$log['message'] = $msg;
if($conf['logfile'] != null){
fwrite($conf['logfile'], ", ".json_encode($log));
}
}
}
......@@ -35,7 +35,7 @@ class MetaDb{
$db = NULL;
}catch(Exception $e){
echo "Can't write in SQLite database. Please check you have granted write permissions to <tt>meta/</tt> and <tt>meta/db.sqlite</tt>.<br/>Also you can check a list of <a href='https://github.com/alangrafu/lodspeakr/wiki/CommonErrors'>common errors</a> and how to <a href='https://github.com/alangrafu/lodspeakr/wiki/Wipe-out-the-database'>wipe out the database</a>'";
Utils::log('Exception exec: '.$e->getMessage(), E_USER_ERROR);
Logging::log('Exception exec: '.$e->getMessage(), E_USER_ERROR);
exit(1);
}
return $results;
......
......@@ -270,7 +270,7 @@ class Utils{
$lodspk['model'] = $modelDir;
$originalDir = getcwd();
$subDirs= array();
Utils::log("Entering $strippedModelDir from ".getcwd(), E_USER_NOTICE);
Logging::log("Entering $strippedModelDir from ".getcwd(), E_USER_NOTICE);
chdir($modelDir);
$handle = opendir('.');
......@@ -278,16 +278,16 @@ class Utils{
if($modelFile != "." && $modelFile != ".." && strpos($modelFile, ".") !== 0){
if(is_dir($modelFile)){
if(strpos('endpoint.', $modelFile) == 0){
Utils::log("Save $modelFile for later, after all the queries in the current directory has been resolved", E_USER_NOTICE);
Logging::log("Save $modelFile for later, after all the queries in the current directory has been resolved", E_USER_NOTICE);
$subDirs[]=$modelFile;
}
}else{
if(preg_match('/\.query$/', $modelFile)){
$e = null;
if(!isset($endpoints[$strippedModelDir])){
Utils::log("Creating endpoint for $strippedModelDir", E_USER_NOTICE);
Logging::log("Creating endpoint for $strippedModelDir", E_USER_NOTICE);
if(!isset($conf['endpoint'][$strippedModelDir])){
Utils::log("Couldn't find $strippedModelDir as a list of available endpoints. Will continue using local", E_USER_WARNING);
Logging::log("Couldn't find $strippedModelDir as a list of available endpoints. Will continue using local", E_USER_WARNING);
$e = $endpoints['local'];
}else{
$endpoints[$strippedModelDir] = new Endpoint($conf['endpoint'][$strippedModelDir], $conf['endpoint']['config']);
......@@ -432,15 +432,15 @@ class Utils{
}
}
if($conf['debug']){
Utils::log($modelFile." against ".$e->getSparqlUrl());
Utils::log($query);
Utils::log("Running query from ".$modelFile." on endpoint ".$e->getSparqlURL(), E_USER_NOTICE);
Logging::log($modelFile." against ".$e->getSparqlUrl());
Logging::log($query);
Logging::log("Running query from ".$modelFile." on endpoint ".$e->getSparqlURL(), E_USER_NOTICE);
}
$initTime = microtime(true);
$aux = $e->query($query, Utils::getResultsType($query));
$endTime = microtime(true);
if($conf['debug']){
Utils::log("Execution time: ".($endTime - $initTime)." seconds");
Logging::log("Execution time: ".($endTime - $initTime)." seconds");
}
$timeObj = new stdClass();
$timeObj->query = new stdClass();
......@@ -472,7 +472,7 @@ class Utils{
}else{
if(strpos('endpoint.', $modelFile) == 0){
Utils::log("$modelFile is a directory, will process it later", E_USER_NOTICE);
Logging::log("$modelFile is a directory, will process it later", E_USER_NOTICE);
if($modelFile != $lodspk['type']){
if(!isset($rPointer[$strippedModelFile])){
$rPointer[$strippedModelFile] = array();
......@@ -564,7 +564,7 @@ class Utils{
//unset($lodspk);
$vars = compact('uri','lodspk', 'conf', 'models', 'first');
if($conf['debug']){
//Utils::log(var_export($vars, true));
//Logging::log(var_export($vars, true));
}
if(is_string($data)){
echo($data);
......@@ -636,12 +636,6 @@ class Utils{
return $result;
}
public static function log($msg){
global $conf;
if($conf['logfile'] != null){
fwrite($conf['logfile'], time()."\t".$msg."\n");
}
}
}
?>
......@@ -96,7 +96,7 @@ class ServiceModule extends abstractModule{
return array($modelFile, $viewFile);
}elseif(file_exists($lodspk['model'])){
HTTPStatus::send406($uri);
exit(0);
return null;
}
array_unshift($arguments, array_pop($tokens));
}
......@@ -196,10 +196,9 @@ class ServiceModule extends abstractModule{
Utils::processDocument($viewFile, $lodspk, $results);
}catch (Exception $ex){
echo $ex->getMessage();
Utils::log($ex->getMessage(), E_ERROR);
Logging::log($ex->getMessage(), E_ERROR);
HTTPStatus::send500($uri);
}
exit(0);
}
......
......@@ -185,7 +185,7 @@ public function execute($pair){
$lodspk['transform_select_query'] = true;
$objResult['viewFile'] = null;
}
Utils::log("LODSPeaKr can't find the proper query. Using HTML query instead.", E_USER_NOTICE);
Logging::log("LODSPeaKr can't find the proper query. Using HTML query instead.", E_USER_NOTICE);
break;
}
}
......
......@@ -30,7 +30,7 @@ class StaticModule extends abstractModule{
header("Content-type: ".$ct);
$uri = $localUri;
if($conf['debug']){
Utils::log("In ".$conf['static']['directory']." static file $file");
Logging::log("In ".$conf['static']['directory']." static file $file");
}
$htmlExtension = 'html';
if($conf['static']['haanga'] && substr_compare($file, $htmlExtension, -strlen($htmlExtension), strlen($htmlExtension)) === 0){
......
......@@ -155,7 +155,7 @@ class TypeModule extends abstractModule{
$lodspk['transform_select_query'] = true;
$objResult['viewFile'] = null;
}
Utils::log("LODSPeaKr can't find the proper query. Using HTML query instead.", E_USER_NOTICE);
Logging::log("LODSPeaKr can't find the proper query. Using HTML query instead.", E_USER_NOTICE);
break;
}else{
$found = false;
......
......@@ -120,7 +120,7 @@ class UriModule extends abstractModule{
}
//chdir($conf['home']);
if($conf['debug']){
Utils::log("Using template ".$viewFile, E_USER_NOTICE);
Logging::log("Using template ".$viewFile, E_USER_NOTICE);
echo("TEMPLATE: ".$viewFile."\n\n");
}
Utils::processDocument($viewFile, $lodspk, $resultsObj);
......
......@@ -19,8 +19,23 @@ include_once('common.inc.php');
$conf['logfile'] = null;
if($conf['debug']){
$conf['logfile'] = fopen("cache/log_".time().rand().".log", "w");
//error_reporting(E_ALL);
include_once('classes/Logging.php');
if(isset($_GET['q']) && $_GET['q'] == 'logs'){
Logging::init();
exit(0);
}else{
$oldtokens = array( ".", "/");
$newtokens = array("_", "_");
$filename = str_replace($oldtokens, $newtokens, $_GET['q']);
$conf['logfile'] = fopen("cache/".$filename."_".time().rand().".log", "w");
if($conf['logfile'] === FALSE){
die("Can't create log file. Check permissions in <tt>cache/</tt> directory.");
}
$initialmsg = array('timestamp' => time(), 'message' => "Log for ".$_GET['q']);
fwrite($conf['logfile'], "{ \"logs\": [".json_encode($initialmsg));
//error_reporting(E_ALL);
}
}else{
error_reporting(E_ERROR);
}
......@@ -93,6 +108,7 @@ foreach($conf['modules']['available'] as $i){
if($matching != FALSE){
$module->execute($matching);
if($conf['logfile'] != null){
fwrite($conf['logfile'], "]}");
fclose($conf['logfile']);
}
exit(0);
......
......@@ -11,6 +11,10 @@ if(file_put_contents($s, $newC) === FALSE){
echo "An error ocurred";
exit(1);
}else{
include_once($s);
echo "Debug mode turned ".$option."\n";
if($option == 'true'){
echo "Visit ".$conf['basedir']."logs to see logs of the different requests\n";
}
}
?>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment