aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormistic100 <mistic@piwigo.org>2015-04-24 17:06:47 +0000
committermistic100 <mistic@piwigo.org>2015-04-24 17:06:47 +0000
commit271e1f5688cb1dbec065dadf07b5ddb1ac12c899 (patch)
tree551716e6063ae5ce11199bae34618299812d4cd3
parent0c576ea19d6d42950615f94feb33df305107a9a6 (diff)
feature 3221 Lazy log file open, clean code
git-svn-id: http://piwigo.org/svn/trunk@31103 68402e56-0260-453c-a942-63ccdbb3a9ee
-rw-r--r--admin/include/image.class.php8
-rw-r--r--i.php4
-rw-r--r--include/Logger.class.php119
3 files changed, 84 insertions, 47 deletions
diff --git a/admin/include/image.class.php b/admin/include/image.class.php
index e77699758..ee9b9bb12 100644
--- a/admin/include/image.class.php
+++ b/admin/include/image.class.php
@@ -621,6 +621,8 @@ class image_ext_imagick implements imageInterface
function write($destination_filepath)
{
+ global $logger;
+
$this->add_command('interlace', 'line'); // progressive rendering
// use 4:2:2 chroma subsampling (reduce file size by 20-30% with "almost" no human perception)
//
@@ -648,13 +650,13 @@ class image_ext_imagick implements imageInterface
$dest = pathinfo($destination_filepath);
$exec .= ' "'.realpath($dest['dirname']).'/'.$dest['basename'].'" 2>&1';
+ $logger->debug($exec, 'i.php');
@exec($exec, $returnarray);
- if (function_exists('ilog')) ilog($exec);
if (is_array($returnarray) && (count($returnarray)>0) )
{
- if (function_exists('ilog')) ilog('ERROR', $returnarray);
- foreach($returnarray as $line)
+ $logger->error('', 'i.php', $returnarray);
+ foreach ($returnarray as $line)
trigger_error($line, E_USER_WARNING);
}
return is_array($returnarray);
diff --git a/i.php b/i.php
index 297681c23..c9a807aac 100644
--- a/i.php
+++ b/i.php
@@ -614,9 +614,9 @@ $timing['send'] = time_step($step);
$timing['total'] = time_step($begin);
-if ($logger->severity() >= Logger::INFO)
+if ($logger->severity() >= Logger::DEBUG)
{
- $logger->info('perf', 'i.php', array(
+ $logger->debug('', 'i.php', array(
'src_path' => basename($page['src_path']),
'derivative_path' => basename($page['derivative_path']),
'o_size' => $o_size[0] . ' ' . $o_size[1] . ' ' . ($o_size[0]*$o_size[1]),
diff --git a/include/Logger.class.php b/include/Logger.class.php
index da90e7152..12eb396c8 100644
--- a/include/Logger.class.php
+++ b/include/Logger.class.php
@@ -95,6 +95,7 @@ class Logger
*/
private $_fileHandle = null;
+
/**
* Class constructor.
*
@@ -105,42 +106,59 @@ class Logger
{
$this->options = array_merge($this->options, $options);
- if (is_string($this->options['severity'])) {
+ if (is_string($this->options['severity']))
+ {
$this->options['severity'] = self::codeToLevel($this->options['severity']);
}
- if ($this->options['severity'] === self::OFF) {
+ if ($this->options['severity'] === self::OFF)
+ {
return;
}
$this->options['directory'] = rtrim($this->options['directory'], '\\/') . DIRECTORY_SEPARATOR;
- if ($this->options['filename'] == null) {
+ if ($this->options['filename'] == null)
+ {
$this->options['filename'] = 'log_' . date('Y-m-d') . '.txt';
}
$this->options['filePath'] = $this->options['directory'] . $this->options['filename'];
- if (!file_exists($this->options['directory'])) {
- mkgetdir($this->options['directory'], MKGETDIR_DEFAULT|MKGETDIR_PROTECT_HTACCESS);
- }
-
- if (file_exists($this->options['filePath']) && !is_writable($this->options['filePath'])) {
- $this->_logStatus = self::STATUS_OPEN_FAILED;
- throw new RuntimeException(self::$_messages['writefail']);
- return;
+ if ($this->options['archiveDays'] != self::ARCHIVE_NO_PURGE && rand() % 97 == 0)
+ {
+ $this->purge();
}
+ }
+
+ /**
+ * Open the log file if not already oppenned
+ */
+ private function open()
+ {
+ if ($this->status() == self::STATUS_LOG_CLOSED)
+ {
+ if (!file_exists($this->options['directory']))
+ {
+ mkgetdir($this->options['directory'], MKGETDIR_DEFAULT|MKGETDIR_PROTECT_HTACCESS);
+ }
- if (($this->_fileHandle = fopen($this->options['filePath'], 'a'))) {
- $this->_logStatus = self::STATUS_LOG_OPEN;
- }
- else {
- $this->_logStatus = self::STATUS_OPEN_FAILED;
- throw new RuntimeException(self::$_messages['openfail']);
- }
+ if (file_exists($this->options['filePath']) && !is_writable($this->options['filePath']))
+ {
+ $this->_logStatus = self::STATUS_OPEN_FAILED;
+ throw new RuntimeException(self::$_messages['writefail']);
+ return;
+ }
- if ($this->options['archiveDays'] != self::ARCHIVE_NO_PURGE && rand() % 97 == 0) {
- $this->purge();
+ if (($this->_fileHandle = fopen($this->options['filePath'], 'a')) != false)
+ {
+ $this->_logStatus = self::STATUS_LOG_OPEN;
+ }
+ else
+ {
+ $this->_logStatus = self::STATUS_OPEN_FAILED;
+ throw new RuntimeException(self::$_messages['openfail']);
+ }
}
}
@@ -149,7 +167,8 @@ class Logger
*/
public function __destruct()
{
- if ($this->_fileHandle) {
+ if ($this->_fileHandle)
+ {
fclose($this->_fileHandle);
}
}
@@ -280,8 +299,10 @@ class Logger
*/
public function log($severity, $message, $cat = null, $args = array())
{
- if ($this->severity() >= $severity) {
- if (is_array($cat)) {
+ if ($this->severity() >= $severity)
+ {
+ if (is_array($cat))
+ {
$args = $cat;
$cat = null;
}
@@ -297,8 +318,11 @@ class Logger
*/
public function write($line)
{
- if ($this->_logStatus == self::STATUS_LOG_OPEN) {
- if (fwrite($this->_fileHandle, $line) === false) {
+ $this->open();
+ if ($this->status() == self::STATUS_LOG_OPEN)
+ {
+ if (fwrite($this->_fileHandle, $line) === false)
+ {
throw new RuntimeException(self::$_messages['writefail']);
}
}
@@ -307,12 +331,15 @@ class Logger
/**
* Purges files matching 'globPattern' older than 'archiveDays'.
*/
- public function purge() {
+ public function purge()
+ {
$files = glob($this->options['directory'] . $this->options['globPattern']);
$limit = time() - $this->options['archiveDays'] * 86400;
- foreach ($files as $file) {
- if (@filemtime($file) < $limit) {
+ foreach ($files as $file)
+ {
+ if (@filemtime($file) < $limit)
+ {
@unlink($file);
}
}
@@ -328,12 +355,14 @@ class Logger
*/
private function formatMessage($level, $message, $cat, $context)
{
- if (!empty($context)) {
- $message .= "\n" . $this->indent($this->contextToString($context));
+ if (!empty($context))
+ {
+ $message.= "\n" . $this->indent($this->contextToString($context));
}
$line = "[" . $this->getTimestamp() . "]\t[" . self::levelToCode($level) . "]\t";
- if ($cat != null) {
- $line .= "[" . $cat . "]\t";
+ if ($cat != null)
+ {
+ $line.= "[" . $cat . "]\t";
}
return $line . $message . "\n";
}
@@ -349,7 +378,7 @@ class Logger
private function getTimestamp()
{
$originalTime = microtime(true);
- $micro = sprintf("%06d", ($originalTime - floor($originalTime)) * 1000000);
+ $micro = sprintf('%06d', ($originalTime - floor($originalTime)) * 1000000);
$date = new DateTime(date('Y-m-d H:i:s.'.$micro, $originalTime));
return $date->format($this->options['dateFormat']);
}
@@ -363,18 +392,22 @@ class Logger
private function contextToString($context)
{
$export = '';
- foreach ($context as $key => $value) {
- $export .= "{$key}: ";
- $export .= preg_replace(array(
+ foreach ($context as $key => $value)
+ {
+ $export.= $key . ': ';
+ $export.= preg_replace(array(
'/=>\s+([a-zA-Z])/im',
'/array\(\s+\)/im',
'/^ |\G /m'
- ), array(
+ ),
+ array(
'=> $1',
'array()',
' '
- ), str_replace('array (', 'array(', var_export($value, true)));
- $export .= PHP_EOL;
+ ),
+ str_replace('array (', 'array(', var_export($value, true))
+ );
+ $export.= PHP_EOL;
}
return str_replace(array('\\\\', '\\\''), array('\\', '\''), rtrim($export));
}
@@ -388,7 +421,7 @@ class Logger
*/
private function indent($string, $indent = ' ')
{
- return $indent.str_replace("\n", "\n".$indent, $string);
+ return $indent . str_replace("\n", "\n" . $indent, $string);
}
/**
@@ -399,7 +432,8 @@ class Logger
*/
static function levelToCode($level)
{
- switch ($level) {
+ switch ($level)
+ {
case self::EMERGENCY:
return 'EMERGENCY';
case self::ALERT:
@@ -429,7 +463,8 @@ class Logger
*/
static function codeToLevel($code)
{
- switch (strtoupper($code)) {
+ switch (strtoupper($code))
+ {
case 'EMERGENCY':
return self::EMERGENCY;
case 'ALERT':