From 61a0a93b130ca1430c57df78ce2521860322c4c2 Mon Sep 17 00:00:00 2001
From: alvaro <alvaro@graves.cl>
Date: Sat, 11 May 2013 17:23:16 -0700
Subject: [PATCH] start of improved logging

---
 classes/Endpoint.php                   |  8 ++++----
 classes/Importer.php                   |  2 +-
 classes/MetaDb.php                     |  2 +-
 classes/Utils.php                      | 27 +++++++++++++++-----------
 classes/modules/serviceModule.php      |  2 +-
 classes/modules/sparqlFilterModule.php |  2 +-
 classes/modules/staticModule.php       |  3 +--
 classes/modules/typeModule.php         |  2 +-
 classes/modules/uriModule.php          |  2 +-
 index.php                              | 11 ++++++++---
 10 files changed, 35 insertions(+), 26 deletions(-)

diff --git a/classes/Endpoint.php b/classes/Endpoint.php
index 0474d5fb..49a3dbed 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";
-       trigger_error($msg);
+       Utils::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){
-          trigger_error("Error executing SPARQL query (".$this->sparqlUrl."): ".curl_error($c), E_USER_ERROR);
+          Utils::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";
-          trigger_error($msg, E_USER_NOTICE);
+          Utils::log($msg, E_USER_NOTICE);
           echo($msg);
         }
       }
@@ -73,7 +73,7 @@ class Endpoint{
       $r = json_decode($aux, true);
       if($conf['debug']){
         if($r == false){
-          trigger_error("Warning: Results from a SELECT sparql query couldn't get parsed", E_USER_WARNING);
+          Utils::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 1835537b..3a4374b8 100644
--- a/classes/Importer.php
+++ b/classes/Importer.php
@@ -127,7 +127,7 @@ class Importer{
  	  }elseif($compType == LS."LodspeakrVisualComponent"){
  	  	$this->createViews($inputs);
  	  }else{
- 	  	trigger_error("Component '$component' (of type $compType) not supported", E_USER_WARNING);
+ 	  	Utils::log("Component '$component' (of type $compType) not supported", E_USER_WARNING);
  	  }
  	}
  	$content .= "?>\n";
diff --git a/classes/MetaDb.php b/classes/MetaDb.php
index 136b8a77..1ac1ea19 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>'";
-		  		trigger_error('Exception exec: '.$e->getMessage(), E_USER_ERROR);
+		  		Utils::log('Exception exec: '.$e->getMessage(), E_USER_ERROR);
 				exit(1);
 		}
         return $results;
diff --git a/classes/Utils.php b/classes/Utils.php
index 545975e8..95ab1cdc 100644
--- a/classes/Utils.php
+++ b/classes/Utils.php
@@ -270,7 +270,7 @@ class Utils{
   	$lodspk['model'] = $modelDir;
   	$originalDir = getcwd();
   	$subDirs= array();
-  	trigger_error("Entering $strippedModelDir from ".getcwd(), E_USER_NOTICE);
+  	Utils::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){
-  	  	  	trigger_error("Save $modelFile for later, after all the queries in the current directory has been resolved", E_USER_NOTICE);
+  	  	  	Utils::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])){
-  	  	      trigger_error("Creating endpoint for $strippedModelDir", E_USER_NOTICE);
+  	  	      Utils::log("Creating endpoint for $strippedModelDir", E_USER_NOTICE);
   	  	      if(!isset($conf['endpoint'][$strippedModelDir])){
-  	  	        trigger_error("Couldn't find $strippedModelDir as a list of available endpoints. Will continue using local", E_USER_WARNING);
+  	  	        Utils::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,16 +432,15 @@ class Utils{
 	  	}
 	  }
   	  if($conf['debug']){
-  	  	echo "\n-------------------------------------------------\nIn ".getcwd()."\n";
-  	    echo "$modelFile (against ".$e->getSparqlUrl().")\n";
-  	  	echo $query."\n";
+  	    Utils::log($modelFile." against ".$e->getSparqlUrl());
+  	  	Utils::log($query);
+    	  Utils::log("Running query from ".$modelFile." on endpoint ".$e->getSparqlURL(), E_USER_NOTICE);
   	  }
-  	  trigger_error("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']){
-  	    echo "Execution time: ".($endTime - $initTime)." seconds\n-------------------------------------------------\n\n";
+  	    Utils::log("Execution time: ".($endTime - $initTime)." seconds");
   	  }
   	  $timeObj = new stdClass();
   	  $timeObj->query = new stdClass();
@@ -473,7 +472,7 @@ class Utils{
   	}else{
   	  if(strpos('endpoint.', $modelFile) == 0){
   	  	
-  	  	trigger_error("$modelFile is a directory, will process it later", E_USER_NOTICE);
+  	  	Utils::log("$modelFile is a directory, will process it later", E_USER_NOTICE);
   	  	if($modelFile != $lodspk['type']){
   	  	  if(!isset($rPointer[$strippedModelFile])){
   	  	  	$rPointer[$strippedModelFile] = array();
@@ -565,7 +564,7 @@ class Utils{
   	//unset($lodspk);
   	$vars = compact('uri','lodspk', 'conf',  'models', 'first');
  	if($conf['debug']){
- 	  var_dump($vars); 	
+ 	  //Utils::log(var_export($vars, true)); 	
  	}
 	if(is_string($data)){
 	  echo($data);
@@ -637,6 +636,12 @@ 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 3dd168c2..1a541a0a 100644
--- a/classes/modules/serviceModule.php
+++ b/classes/modules/serviceModule.php
@@ -196,7 +196,7 @@ class ServiceModule extends abstractModule{
   	  Utils::processDocument($viewFile, $lodspk, $results);    	  
   	}catch (Exception $ex){
   	  echo $ex->getMessage();
-  	  trigger_error($ex->getMessage(), E_ERROR);
+  	  Utils::log($ex->getMessage(), E_ERROR);
   	  HTTPStatus::send500($uri);
   	}
   	exit(0);	
diff --git a/classes/modules/sparqlFilterModule.php b/classes/modules/sparqlFilterModule.php
index c2dc660e..5751c222 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;
   	  	}
-  	  	trigger_error("LODSPeaKr can't find the proper query. Using HTML query instead.", E_USER_NOTICE);
+  	  	Utils::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 594b6c17..3c41ac98 100644
--- a/classes/modules/staticModule.php
+++ b/classes/modules/staticModule.php
@@ -30,8 +30,7 @@ class StaticModule extends abstractModule{
   	header("Content-type: ".$ct);
   	$uri = $localUri;
   	if($conf['debug']){
-  	  echo "\n-------------------------------------------------\nIn ".$conf['static']['directory']."\n";
-  	  echo "Static file $file\n-------------------------------------------------\n\n";
+  	  Utils::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 38b7a279..c2682765 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;
   	  	}
-  	  	trigger_error("LODSPeaKr can't find the proper query. Using HTML query instead.", E_USER_NOTICE);
+  	  	Utils::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 fd933a43..78be3298 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']){
-  	  trigger_error("Using template ".$viewFile, E_USER_NOTICE);
+  	  Utils::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 87e51118..56d75648 100755
--- a/index.php
+++ b/index.php
@@ -1,7 +1,7 @@
 <?php
 
 //Import
-if($_GET['q'] == 'import'){
+if(isset($_GET['q']) && $_GET['q'] == 'import'){
   include_once('classes/Importer.php');
   $imp = new Importer();
   $imp->run();
@@ -15,10 +15,12 @@ if(!file_exists('settings.inc.php')){
 }
 
 include_once('common.inc.php');
-
 //Debug output
+
+$conf['logfile'] = null;
 if($conf['debug']){
-  error_reporting(E_ALL);
+  $conf['logfile'] = fopen("cache/log_".time().rand().".log", "w");
+  //error_reporting(E_ALL);
 }else{
   error_reporting(E_ERROR);
 }
@@ -90,6 +92,9 @@ foreach($conf['modules']['available'] as $i){
   $matching = $module->match($uri);
   if($matching != FALSE){
   	$module->execute($matching);
+  	if($conf['logfile'] != null){
+  	  fclose($conf['logfile']);
+  	}
   	exit(0);
   }
 }
-- 
GitLab