Location: PHPKode > projects > Network Configuration Store > NCS-3.0.2/wwwroot/inc/CSLog.class.php
<?php

class CSLog
{
	private $fileName;
	public function getFileName() { return $this->file; }

	private $filePath;
	private $fileInfo;

	public function getTimeStamp() { return date("d/m/Y H:i:s", $this->fileInfo['mtime']); }
	public function getFileSize() { return sprintf("%0.2f kb", ($this->fileInfo['size'] / 1024)); }

	public function __construct($logPath=null, $logDate=null, $logTime=null, $isDebugFile=FALSE)
	{
		global $CS;

		if (!is_null($logPath))
		{
			$this->filePath = $logPath;
			$this->fileName = basename($logPath);
		}
		else
		{
			try {
				$this->fileName = $logDate.'.logdir/'.$logTime;
				if ($isDebugFile) {
					$this->fileName .= ".debug";
				} else {
					$this->fileName .= ".log";
				}
				$this->filePath = $CS->GetOption('log_path').'/'.$this->fileName;
			}
			catch (Exception $e) {
				throw $e;
			}
		}

		if (!file_exists($this->filePath))
			throw new Exception('Specified log file does not exist: '.$this->filePath);

		$this->fileInfo = stat($this->filePath);
	}

	public function __destruct()
	{
	}

	public function getContents()
	{
		try {
			$lines = CS::GetFileContents($this->filePath);
		} catch (Exception $e) {
			return '[file is empty]';
		}

		return array_reverse($lines);
	}

	// ###############################################################################
	// Static methods

	public static function GetLogFolders()
	{
		global $CS;

		try {
			$logPath = $CS->GetOption('log_path');
		} catch (Exception $e) {
			throw $e;
		}

		if (!is_dir($logPath))
			return array();

		$logFolders = array();

		foreach(glob($logPath.'/*.logdir') as $logFolder)
		{
			if (!is_dir($logFolder))
				continue;

			list($logDate, ) = explode('.', basename($logFolder), 2);

			// Create our folder in logFolders array, containing two
			// pre-defined empty arrays for the logs and debug logs.
			//
			$logFolders[$logDate] = array(
							'log' => array(),
							'debug' => array()
						);

			foreach(glob($logFolder.'/*.log') as $logFile)
			{
				list($logTime, ) = explode('.', basename($logFile), 2);
				try {
					$logFolders[$logDate]['log'][$logTime] = new CSLog($logFile);
				} catch (Exception $e) {
					throw $e;
				}
			}

			foreach(glob($logFolder.'/*.debug') as $debugFile)
			{
				list($debugTime, ) = explode('.', basename($debugFile), 2);
				try {
					$logFolders[$logDate]['debug'][$debugTime] = new CSLog($debugFile);
				} catch (Exception $e) {
					throw $e;
				}
			}
		}

		return $logFolders;
	}

	public static function LoadLogNodes(CSTreeView &$tree, $logDate, Array $logItems)
	{
		if (count($logItems) == 0)
			return;

		// Cycle the array and create the tree structure.
		//

		$s = explode($logDate, '-');
		$thisNodeId = $tree->AddNode($tree->getRootName(), array(
					'label'		=> $logDate,
					'icon'		=> 'folder'
				));

		// Skip to the next item unless we have some debug items.
		//
		if (count($logItems['debug']) > 0)
		{
			$debugNodeId = $tree->AddNode($thisNodeId, array(
					'label'		=> 'Debug Messages',
					'icon'		=> 'folder'
				));

			foreach($logItems['debug'] as $debugTime => $debugItem)
			{
				$tree->AddNode($debugNodeId, array(
					'label'		=> $debugItem->getTimeStamp(),
					'href'		=> 'View.php?date='.$logDate.'&amp;time='.$debugTime.'&amp;debug',
					'target'	=> 'MainFrame',
					'icon'		=> 'log',
					'expand'	=> FALSE
				));
			}
		}

		if (count($logItems['log']) > 0)
		{
			$logNodeId = $tree->AddNode($thisNodeId, array(
					'label'		=> 'Scan Logs',
					'icon'		=> 'folder'
				));

			// Now cycle through the log items and add them!
			//
			foreach($logItems['log'] as $logTime => $logItem)
			{
				$tree->AddNode($logNodeId, array(
					'label'		=> $logItem->getTimeStamp(),
					'href'		=> 'View.php?date='.$logDate.'&amp;time='.$logTime,
					'target'	=> 'MainFrame',
					'icon'		=> 'log',
					'expand'	=> FALSE
				));
			}
		}
	}
}

?>
Return current item: Network Configuration Store