2011-06-10 21:55:32 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* This file is part of MySQLDumper released under the GNU/GPL 2 license
|
|
|
|
* http://www.mysqldumper.net
|
|
|
|
*
|
|
|
|
* @package MySQLDumper
|
|
|
|
* @subpackage Log
|
|
|
|
* @version SVN: $Rev$
|
|
|
|
* @author $Author$
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Log Class
|
|
|
|
*
|
|
|
|
* @package MySQLDumper
|
|
|
|
* @subpackage Log
|
|
|
|
*/
|
|
|
|
class Msd_Log
|
|
|
|
{
|
|
|
|
// Define constants
|
2012-08-04 17:31:19 +00:00
|
|
|
const PHP = 'PHP-Log';
|
|
|
|
const PERL = 'PERL-Log';
|
2011-06-10 21:55:32 +00:00
|
|
|
const PERL_COMPLETE = 'PERL-Complete-Log';
|
2012-08-04 17:31:19 +00:00
|
|
|
const ERROR = 'Error-Log';
|
2011-06-10 21:55:32 +00:00
|
|
|
|
|
|
|
// Define static Instance
|
|
|
|
private static $_instance = NULL;
|
|
|
|
private $_paths = NULL;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Init file handles
|
|
|
|
*
|
2012-08-04 17:09:48 +00:00
|
|
|
* @return Msd_Log
|
2011-06-10 21:55:32 +00:00
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
// define instance handler
|
2012-08-04 17:31:19 +00:00
|
|
|
$this->handle = array();
|
|
|
|
$this->handle[self::PHP] = false;
|
|
|
|
$this->handle[self::PERL] = false;
|
2011-06-10 21:55:32 +00:00
|
|
|
$this->handle[self::PERL_COMPLETE] = false;
|
2012-08-04 17:31:19 +00:00
|
|
|
$this->handle[self::ERROR] = false;
|
2011-06-10 21:55:32 +00:00
|
|
|
|
2012-08-04 17:31:19 +00:00
|
|
|
$config = Msd_Registry::getConfig();
|
|
|
|
$this->_paths = (object)$config->getParam('paths');
|
2011-06-10 21:55:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Close all open file handles on destruct
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __destruct()
|
|
|
|
{
|
|
|
|
if ($this->handle[self::PHP]) {
|
|
|
|
$this->_close(self::PHP);
|
|
|
|
}
|
|
|
|
if (is_resource($this->handle[self::PERL])) {
|
|
|
|
$this->_close(self::PERL);
|
|
|
|
}
|
|
|
|
if (is_resource($this->handle[self::PERL_COMPLETE])) {
|
|
|
|
$this->_close(self::PERL_COMPLETE);
|
|
|
|
}
|
|
|
|
if (is_resource($this->handle[self::ERROR])) {
|
|
|
|
$this->_close(self::ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Close a filehandle
|
|
|
|
*
|
|
|
|
* @param Loge $file The file to close
|
|
|
|
*/
|
|
|
|
private function _close($file)
|
|
|
|
{
|
2012-08-04 17:31:19 +00:00
|
|
|
$filename = $this->getFile($file);
|
2011-06-10 21:55:32 +00:00
|
|
|
$extension = pathinfo($filename, PATHINFO_EXTENSION);
|
|
|
|
if ($extension == 'gz') {
|
|
|
|
gzclose($this->handle[$file]);
|
|
|
|
} else {
|
|
|
|
fclose($this->handle[$file]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an instance of Msd_Log
|
|
|
|
*
|
|
|
|
* @return Msd_Log
|
|
|
|
*/
|
|
|
|
public static function getInstance()
|
|
|
|
{
|
|
|
|
if (self::$_instance === NULL) {
|
|
|
|
self::$_instance = new self;
|
|
|
|
}
|
|
|
|
return self::$_instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an instance of Msd_Log for a special type
|
|
|
|
*
|
|
|
|
* Allowed types are self::PHP, self::PERL, self::PERL_COMPLETE or
|
|
|
|
* self::ERROR
|
|
|
|
*
|
|
|
|
* @param Msd_Log $type
|
|
|
|
*
|
|
|
|
* @return Msd_Log
|
|
|
|
*/
|
|
|
|
public function getLogInstance($type)
|
|
|
|
{
|
|
|
|
if (!isset($this->_logInstance[$type])) {
|
2012-08-04 17:31:19 +00:00
|
|
|
$writer = new Zend_Log_Writer_Stream($this->getFile($type));
|
2011-06-10 21:55:32 +00:00
|
|
|
$formatter =
|
2012-08-04 17:31:19 +00:00
|
|
|
new Zend_Log_Formatter_Simple("%timestamp% %message%\n");
|
2011-06-10 21:55:32 +00:00
|
|
|
$writer->setFormatter($formatter);
|
|
|
|
$this->_logInstance[$type] = new Zend_Log($writer);
|
|
|
|
}
|
|
|
|
return $this->_logInstance[$type];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write to log file
|
|
|
|
*
|
|
|
|
* @param string $type The type of log file to write to
|
|
|
|
* @param string $message The message to add to the file
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function write($type, $message)
|
|
|
|
{
|
|
|
|
// @todo if log_maxsize reached => archive/delete log
|
|
|
|
$logger = self::getInstance();
|
2012-08-04 17:31:19 +00:00
|
|
|
$log = $logger->getLogInstance($type);
|
2011-06-10 21:55:32 +00:00
|
|
|
return $log->info($message);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the concrete filename with path for the given type.
|
|
|
|
*
|
|
|
|
* @param const $file
|
|
|
|
*
|
|
|
|
* @return string Filename of logfile
|
|
|
|
*/
|
|
|
|
public function getFile($file)
|
|
|
|
{
|
|
|
|
$filename = '';
|
|
|
|
switch ($file) {
|
|
|
|
case self::PHP:
|
2012-08-04 10:40:48 +00:00
|
|
|
$filename = $this->_paths->log . '/php.log';
|
2011-06-10 21:55:32 +00:00
|
|
|
break;
|
|
|
|
case self::PERL:
|
2012-08-04 10:40:48 +00:00
|
|
|
$filename = $this->_paths->log . '/perl.log';
|
2011-06-10 21:55:32 +00:00
|
|
|
break;
|
|
|
|
case self::PERL_COMPLETE:
|
2012-08-04 10:40:48 +00:00
|
|
|
$filename = $this->_paths->log . '/perlComplete.log';
|
2011-06-10 21:55:32 +00:00
|
|
|
break;
|
|
|
|
case self::ERROR:
|
2012-08-04 10:40:48 +00:00
|
|
|
$filename = $this->_paths->log . '/phpError.log';
|
2011-06-10 21:55:32 +00:00
|
|
|
}
|
|
|
|
return $filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a log file and recreate it.
|
|
|
|
*
|
2012-08-04 17:31:19 +00:00
|
|
|
* @param string $type Filename
|
2011-06-10 21:55:32 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function delete($type)
|
|
|
|
{
|
|
|
|
$filename = self::getFile($type);
|
|
|
|
@unlink($filename);
|
|
|
|
// re-create log file
|
|
|
|
$translator = Msd_Language::getInstance()->getTranslator();
|
|
|
|
$this->write($type, $translator->_('L_LOG_CREATED'));
|
|
|
|
}
|
2012-08-04 17:31:19 +00:00
|
|
|
|
2011-06-10 21:55:32 +00:00
|
|
|
/**
|
|
|
|
* Read a logfile and return content as array.
|
|
|
|
*
|
|
|
|
* If $revers is set to true the ordering of lines is reversed.
|
|
|
|
*
|
2012-08-04 17:31:19 +00:00
|
|
|
* @param string $type The type of logfile to read
|
|
|
|
* @param bool $reverse Whether to place latest entries first
|
2011-06-10 21:55:32 +00:00
|
|
|
*
|
2012-08-04 17:31:19 +00:00
|
|
|
* @return array Log data from file as array
|
2011-06-10 21:55:32 +00:00
|
|
|
*/
|
|
|
|
public function read($type = self::PHP, $reverse = false)
|
|
|
|
{
|
|
|
|
$filename = $this->getFile($type);
|
|
|
|
if (!is_readable($filename)) {
|
|
|
|
$timestamp = Zend_Date::ISO_8601;
|
2012-08-04 17:31:19 +00:00
|
|
|
$lang = Msd_Language::getInstance()->getTranslator();
|
|
|
|
$msg = $timestamp . ' <span class="error">' .
|
2011-06-10 21:55:32 +00:00
|
|
|
sprintf($lang->_('L_LOG_NOT_READABLE'), $filename) . '</span>';
|
|
|
|
return array($msg);
|
|
|
|
} else {
|
|
|
|
$output = file($filename);
|
|
|
|
}
|
|
|
|
if ($reverse == 1) {
|
2012-08-04 17:31:19 +00:00
|
|
|
$output = array_reverse($output);
|
2011-06-10 21:55:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
2012-08-04 10:40:48 +00:00
|
|
|
}
|