/** * Add a trace in the application : * - If server is in production, we write into the logs * - If server is in development, output is used * * @access public static * @param Exception $exception the raised exception contained the message to trace * @param bool $stack flag if we want the stacktrace call * @param string $complementary html complementary description * @since 1.0 */ public static function addTrace($exception,$stack=true,$complementary="") { $generic = SLS_Generic::getInstance(); $e = $exception->getTrace(); // If logs if ($generic->isProd()) { $nbOccurencesFiles = 0; $nbMaxLines = 2000; $directory = date("Y-m"); $fileName = date("Y-m-d"); $filePath = ""; $traces = SLS_Tracing::stackTracing($e,false); // If month directory doesn't exists, create it if (!file_exists($generic->getPathConfig("logs").$directory)) mkdir($generic->getPathConfig("logs").$directory,0777); // Count the number of hits of log file $handle = opendir($generic->getPathConfig("logs").$directory); while (false !== ($file = readdir($handle))) if (SLS_String::startsWith($file,$fileName)) $nbOccurencesFiles++; closedir($handle); // If the current file log doesn't exists, create it if ($nbOccurencesFiles == 0) { touch($generic->getPathConfig("logs").$directory."/".$fileName."_0.log"); $filePath = $generic->getPathConfig("logs").$directory."/".$fileName."_0.log"; } // Else, locate it else $filePath = $generic->getPathConfig("logs").$directory."/".$fileName."_".($nbOccurencesFiles-1).".log"; // If the max number of lines has been reach, increase the file log version if (SLS_String::countLines(file_get_contents($filePath)) >= $nbMaxLines) { touch($generic->getPathConfig("logs").$directory."/".$fileName."_".$nbOccurencesFiles.".log"); $filePath = $generic->getPathConfig("logs").$directory."/".$fileName."_".$nbOccurencesFiles.".log"; } // Then, if and only if the file log has been located, increased or created : write into the logs if (is_file($filePath)) { $oldContentLog = file_get_contents($filePath); $newContentLog = date("Y-m-d H:i:s").' - '.$exception->getMessage()."\n"; for ($i=0 ; $i<count($traces) ; $i++) $newContentLog .= " ".trim($traces[$i]["call"]." ".$traces[$i]["file"])."\n"; file_put_contents($filePath,$newContentLog.$oldContentLog,LOCK_EX); } } // Else output else { $traces = SLS_Tracing::stackTracing($e,true); if (!SLS_Tracing::$_exceptionThrown) { echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'."\n". '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">'."\n". '<head>'."\n". '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />'."\n"; SLS_Tracing::makeCss(); SLS_Tracing::loadMootools(); echo '</head>'."\n". '<body>'."\n"; } echo '<div class="mainTitle">'.date("Y-m-d H:i:s").' - '.$exception->getMessage().''."\n". '<div>in '.SLS_Tracing::getTraceInfo($e[0],true).'</div></div>'."\n"; if ($stack) { echo '<div class="stackTrace">'."\n"; for($i=0 ; $i<count($traces) ; $i++) { $style = ($i<(count($traces)-1)) ? 'border-bottom:1px dotted darkgray;padding-bottom:2px;' : ''; echo '<div style="'.$style.'">'.$traces[$i]["call"].'<span>'.$traces[$i]["file"].'</span></div>'."\n"; } echo '</div>'."\n"; } if ($complementary != "") { echo $complementary; } //echo '</body>'."\n". '</html>'."\n"; SLS_Tracing::$_exceptionThrown = true; } }