Exemplo n.º 1
0
	/**
	 * 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;
		}
	}
Exemplo n.º 2
0
	/**
	 * Log execution time
	 * 
	 * @access public
	 * @param float $time execution time
	 * @param string $message message to log
	 * @param string $detail detail
	 * @param string $type type of process
	 * @since 1.0.5
	 */
	public function logTime($time,$message,$detail="",$type="unknown")
	{
		#echo "[".$type."] || ".date("Y-m-d H:i:s")." || ".$time." || ".$message." || "."Detail: ".$detail."<br />";
		
		// Developer monitoring
		if ($this->getSide() == "user" && $this->getBo() != $this->getGenericControllerName())
		{
			$types = array("statics" 		=> array("time" => "0", "logs" => array()),
						   "components" 	=> array("time" => "0", "logs" => array()),
						   "routing" 		=> array("time" => "0", "msg"  => ""),
						   "init" 			=> array("time" => "0", "msg"  => ""),
						   "action" 		=> array("time" => "0", "msg"  => ""),
						   "sql" 			=> array("time" => "0", "logs" => array()),
						   "parsing_html" 	=> array("time" => "0", "msg"  => ""),
						   "parsing_xsl" 	=> array("time" => "0", "msg"  => ""),
						   "flush_cache" 	=> array("time" => "0", "logs" => array()));
			$logSession = $this->_session->getParam("sls_dev_logs");
			$devLogs = (empty($logSession)) ? $types : $logSession;
			switch($type)
	    	{
	    		case "Controller Static":
	    			$devLogs["statics"]["time"] += $time;
	    			$devLogs["statics"]["logs"][] = array("time" => $time, "msg" => trim(SLS_String::substrAfterFirstDelimiter($detail,"Controller:")));
	    			break;
	    		case "Controller Component":
	    			$devLogs["components"]["time"] += $time;
	    			$devLogs["components"]["logs"][] = array("time" => $time, "msg" => trim(SLS_String::substrAfterFirstDelimiter($detail,"Controller:")));
	    			break;
	    		case "Controller Front":
	    			$mapping = trim(SLS_String::substrBeforeFirstDelimiter(SLS_String::substrAfterFirstDelimiter($message,"Resolve Mapping ("),")"));
	    			$mappingController = SLS_String::substrBeforeFirstDelimiter($mapping,"/");
	    			$mappingAction = SLS_String::substrAfterFirstDelimiter($mapping,"/");
	    			$devLogs["routing"]["time"] += $time;
	    			$devLogs["routing"]["msg"] = SLS_String::printArray(array("mode" => $mappingController, "smode" => $mappingAction));
	    			break;
	    		case "Controller Init":
	    			$devLogs["init"]["time"] += $time;
	    			break;
	    		case "Controller Action":
	    			$devLogs["action"]["time"] += $time;
	    			break;
	    		case "MySQL Query":
					$message = trim((!SLS_String::startsWith($detail,"Query:")) ? $message : SLS_String::substrAfterFirstDelimiter($detail,"Query:"));
					if ($message == "MySQL Connecting")
						$message = "Connection"." |n|".$detail;
					$message = str_replace(array("|n|"),array("\n"),$message);
	    			$devLogs["sql"]["time"] += $time;
	    			$devLogs["sql"]["logs"][] = array("time" => $time, "msg" => $message);
	    			break;
	    		case "HTML Parsing":
	    			$devLogs["parsing_html"]["time"] += $time;
	    			break;
	    		case "XML/XSL Parsing":
	    			$devLogs["parsing_xsl"]["time"] += $time;
	    			break;
	    		case "Flush Cache":
	    			$devLogs["flush_cache"]["time"] += $time;
	    			$devLogs["flush_cache"]["logs"][] = array("time" => $time, "msg" => $detail);
	    			break;
	    	}
	    	$this->_session->setParam("sls_dev_logs",$devLogs);
		}
		
		if (!$this->isMonitoring())
			return;
		
		// Objects
		$nbOccurencesFiles = 0;
		$nbMaxLines = 5000;
		$directory = "monitoring/".date("Y-m");
		$fileName = date("Y-m-d");
		$filePath = "";
		
		// Check if monitoring directory exists
		if (!file_exists($this->getPathConfig("logs")."monitoring"))
			mkdir($this->getPathConfig("logs")."monitoring",0777);	
		
		// If month directory doesn't exists, create it			
		if (!file_exists($this->getPathConfig("logs").$directory))			
			mkdir($this->getPathConfig("logs").$directory,0777);
			
		// Count the number of hits of log file			
		$handle = opendir($this->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($this->getPathConfig("logs").$directory."/".$fileName."_0.log");
	    	$filePath = $this->getPathConfig("logs").$directory."/".$fileName."_0.log";
	    }
	    // Else, locate it
	    else	    
	    	$filePath = $this->getPathConfig("logs").$directory."/".$fileName."_".($nbOccurencesFiles-1).".log";
	    
	    // Then, if and only if the file log has been located, increased or created : write into the logs		    
	    if (is_file($filePath) && $this->getSide() == "user")
	    {
	    	$oldContentLog = file_get_contents($filePath);
	    	$newContentLog = "".$type." || ".date("Y-m-d H:i:s")." || ".$time." || ".$message." || ".$detail;
	    	if (SLS_String::endsWith($newContentLog,"\n"))
	    		$newContentLog = SLS_String::substrBeforeLastDelimiter($newContentLog,"\n");
	    	$newContentLog = str_replace("\n","|n|",$newContentLog);
	    	    	
	    	file_put_contents($filePath,$newContentLog."\n".$oldContentLog,LOCK_EX);
	    		    	
	    	if ($type == "Render")
	    	{
	    		$oldContentLog = file_get_contents($filePath);
	    		$newContentLog = "#|end|#";
	    		file_put_contents($filePath,$newContentLog."\n".$oldContentLog,LOCK_EX);
	    		
		    	// If the max number of lines has been reach, increase the file log version		    
			    if (SLS_String::countLines(file_get_contents($filePath)) >= $nbMaxLines)
			    {
			    	touch($this->getPathConfig("logs").$directory."/".$fileName."_".$nbOccurencesFiles.".log");
			    	$filePath = $this->getPathConfig("logs").$directory."/".$fileName."_".$nbOccurencesFiles.".log";
			    }
	    	}
	    }
	}