diff --git a/classes/Endpoint.php b/classes/Endpoint.php index 49a3dbedca8bd31d0940930170eca92c2be751f8..188da5e8a8c12b66d86dee4131fb31e6b3174f9b 100644 --- a/classes/Endpoint.php +++ b/classes/Endpoint.php @@ -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"); } } diff --git a/classes/Importer.php b/classes/Importer.php index 3a4374b8c5e2e48040efe0f670f25ddbf2e59204..1ee4212a0149435f513efb1800b9dc24fc7fbe01 100644 --- a/classes/Importer.php +++ b/classes/Importer.php @@ -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"; diff --git a/classes/Logging.php b/classes/Logging.php new file mode 100644 index 0000000000000000000000000000000000000000..39a43dc788db61427f04b7118829246d8f814ca9 --- /dev/null +++ b/classes/Logging.php @@ -0,0 +1,89 @@ +<?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)); + } + } +} diff --git a/classes/MetaDb.php b/classes/MetaDb.php index 1ac1ea19428ba15ca04a641a1991297875432729..404a3793027548e7c597c9c6024c727cc5ab9c07 100644 --- a/classes/MetaDb.php +++ b/classes/MetaDb.php @@ -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; diff --git a/classes/Utils.php b/classes/Utils.php index 95ab1cdc44e9a5a2ebf79286b65ed1000969a8cb..7464b88f7ccceb9ba6b32b997d2d680818c3b4bd 100644 --- a/classes/Utils.php +++ b/classes/Utils.php @@ -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"); - } - } } ?> diff --git a/classes/modules/serviceModule.php b/classes/modules/serviceModule.php index 1a541a0a0250e0e99a7ad423b8633ec948c57ea2..bd585a67b4623fc1a32393146fc4c1f4ae27ee70 100644 --- a/classes/modules/serviceModule.php +++ b/classes/modules/serviceModule.php @@ -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); } diff --git a/classes/modules/sparqlFilterModule.php b/classes/modules/sparqlFilterModule.php index 5751c2220bb2371e67dacd33d5fd34ed5e3c7a0b..397fb2f34129faa5c53a59ea0b56e949b6e9242b 100644 --- a/classes/modules/sparqlFilterModule.php +++ b/classes/modules/sparqlFilterModule.php @@ -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; } } diff --git a/classes/modules/staticModule.php b/classes/modules/staticModule.php index 3c41ac98750b507cfb5fecf9718328f6ebc31892..b450238d029c52ab42d4a43ef74da1bbf4ebf0cc 100644 --- a/classes/modules/staticModule.php +++ b/classes/modules/staticModule.php @@ -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){ diff --git a/classes/modules/typeModule.php b/classes/modules/typeModule.php index c2682765511f0200ac4aeb961a17674ffc03d59b..4ad114b418fb5cbcd4a865b86b332f773902316d 100644 --- a/classes/modules/typeModule.php +++ b/classes/modules/typeModule.php @@ -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; diff --git a/classes/modules/uriModule.php b/classes/modules/uriModule.php index 78be32985a523b13e9d2e6dc0d07f299f8326707..9a69592707263142e07f12c7c5511f7efa740c8d 100644 --- a/classes/modules/uriModule.php +++ b/classes/modules/uriModule.php @@ -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); diff --git a/index.php b/index.php index 56d75648366fe38e827df7614ae803769762514b..feb9493d216f0dc9befeb09c7616c89644454f40 100755 --- a/index.php +++ b/index.php @@ -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); diff --git a/utils/modules/debug.php b/utils/modules/debug.php index 66c06f3a24a1c43b351761be025b4da07afddb76..d259bcff4fa7f03be32ab97665dc940ca539bbad 100644 --- a/utils/modules/debug.php +++ b/utils/modules/debug.php @@ -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"; + } } ?>