[ Index ]
 

Code source de eZ Publish 3.9.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/ezfile/classes/ -> ezlog.php (source)

   1  <?php
   2  //
   3  // Definition of eZLog class
   4  //
   5  // Created on: <17-Mar-2003 11:00:54 wy>
   6  //
   7  // SOFTWARE NAME: eZ publish
   8  // SOFTWARE RELEASE: 3.9.0
   9  // BUILD VERSION: 17785
  10  // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS
  11  // SOFTWARE LICENSE: GNU General Public License v2.0
  12  // NOTICE: >
  13  //   This program is free software; you can redistribute it and/or
  14  //   modify it under the terms of version 2.0  of the GNU General
  15  //   Public License as published by the Free Software Foundation.
  16  //
  17  //   This program is distributed in the hope that it will be useful,
  18  //   but WITHOUT ANY WARRANTY; without even the implied warranty of
  19  //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20  //   GNU General Public License for more details.
  21  //
  22  //   You should have received a copy of version 2.0 of the GNU General
  23  //   Public License along with this program; if not, write to the Free
  24  //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  25  //   MA 02110-1301, USA.
  26  //
  27  //
  28  
  29  /*! \defgroup eZUtils Utility classes */
  30  
  31  /*!
  32    \class eZLog ezlog.php
  33    \ingroup eZUtils
  34  */
  35  
  36  
  37  include_once ( "lib/ezutils/classes/ezsys.php" );
  38  define( "EZ_MAX_LOGROTATE_FILES", 3 );
  39  define( "EZ_MAX_LOGFILE_SIZE", 200*1024 );
  40  class eZLog
  41  {
  42      /*!
  43        Creates a new log object.
  44      */
  45      function eZLog( )
  46      {
  47      }
  48  
  49      /*!
  50       \public
  51       Writes a message $message to a given file name $name and directory $dir for logging
  52      */
  53      function write( $message, $logName = 'common.log', $dir = 'var/log' )
  54      {
  55          $fileName = $dir . '/' . $logName;
  56          if ( !file_exists( $dir ) )
  57          {
  58              include_once ( 'lib/ezfile/classes/ezdir.php' );
  59              eZDir::mkdir( $dir, 0775, true );
  60          }
  61          $oldumask = @umask( 0 );
  62  
  63          $fileExisted = @file_exists( $fileName );
  64          if ( $fileExisted and
  65               filesize( $fileName ) > eZLog::maxLogSize() )
  66          {
  67              if ( eZLog::rotateLog( $fileName ) )
  68                  $fileExisted = false;
  69          }
  70  
  71          $logFile = @fopen( $fileName, "a" );
  72          if ( $logFile )
  73          {
  74              $time = strftime( "%b %d %Y %H:%M:%S", strtotime( "now" ) );
  75              $logMessage = "[ " . $time . " ] $message\n";
  76              @fwrite( $logFile, $logMessage );
  77              @fclose( $logFile );
  78              if ( !$fileExisted )
  79                  @chmod( $fileName, 0666 );
  80              @umask( $oldumask );
  81          }
  82      }
  83  
  84      /*!
  85       \private
  86       Writes file name $name and storage directory $dir to storage log
  87      */
  88      function writeStorageLog( $name, $dir = false )
  89      {
  90          $ini =& eZINI::instance();
  91          $varDir = $ini->variable( 'FileSettings', 'VarDir' );
  92          $logDir = $ini->variable( 'FileSettings', 'LogDir' );
  93          $logName = 'storage.log';
  94          $fileName = $varDir . '/' . $logDir . '/' . $logName;
  95          if ( !file_exists( $varDir . '/' . $logDir ) )
  96          {
  97              include_once ( 'lib/ezfile/classes/ezdir.php' );
  98              eZDir::mkdir( $varDir . '/' . $logDir, 0775, true );
  99          }
 100          $oldumask = @umask( 0 );
 101  
 102          $fileExisted = @file_exists( $fileName );
 103          if ( $fileExisted and
 104               filesize( $fileName ) > eZLog::maxLogSize() )
 105          {
 106              if ( eZLog::rotateLog( $fileName ) )
 107                  $fileExisted = false;
 108          }
 109  
 110          if ( $dir !== false )
 111          {
 112              $dir = preg_replace( "#/$#", "", $dir );
 113              $dir .= "/";
 114          }
 115          else
 116          {
 117              $dir = "";
 118          }
 119  
 120          $logFile = @fopen( $fileName, "a" );
 121          if ( $logFile )
 122          {
 123              $time = strftime( "%b %d %Y %H:%M:%S", strtotime( "now" ) );
 124              $logMessage = "[ " . $time . " ] [" . $dir . $name . "]\n";
 125              @fwrite( $logFile, $logMessage );
 126              @fclose( $logFile );
 127              if ( !$fileExisted )
 128                  @chmod( $fileName, 0666 );
 129              @umask( $oldumask );
 130          }
 131      }
 132  
 133      /*!
 134       \static
 135       \return the maxium size for a log file in bytes.
 136      */
 137      function maxLogSize()
 138      {
 139          $maxLogSize =& $GLOBALS['eZMaxLogSize'];
 140          if ( isset( $maxLogSize ) )
 141              return $maxLogSize;
 142          return EZ_MAX_LOGFILE_SIZE;
 143      }
 144  
 145      /*!
 146       \static
 147       Sets the maxium size for a log file to \a $size.
 148      */
 149      function setMaxLogSize( $size )
 150      {
 151          $GLOBALS['eZMaxLogSize'] = $size;
 152      }
 153  
 154      /*!
 155       \static
 156       \return the maxium number of logrotate files to keep.
 157      */
 158      function maxLogrotateFiles()
 159      {
 160          $maxLogrotateFiles =& $GLOBALS['eZMaxLogrotateFiles'];
 161          if ( isset( $maxLogrotateFiles ) )
 162              return $maxLogrotateFiles;
 163          return EZ_MAX_LOGROTATE_FILES;
 164      }
 165  
 166      /*!
 167       \static
 168       Rotates logfiles so the current logfile is backed up,
 169       old rotate logfiles are rotated once more and those that
 170       exceed maxLogrotateFiles() will be removed.
 171       Rotated files will get the extension .1, .2 etc.
 172      */
 173      function rotateLog( $fileName )
 174      {
 175          $maxLogrotateFiles = eZLog::maxLogrotateFiles();
 176          for ( $i = $maxLogrotateFiles; $i > 0; --$i )
 177          {
 178              $logRotateName = $fileName . '.' . $i;
 179              if ( @file_exists( $logRotateName ) )
 180              {
 181                  if ( $i == $maxLogrotateFiles )
 182                  {
 183                      @unlink( $logRotateName );
 184                  }
 185                  else
 186                  {
 187                      $newLogRotateName = $fileName . '.' . ($i + 1);
 188                      @rename( $logRotateName, $newLogRotateName );
 189                  }
 190              }
 191          }
 192          if ( @file_exists( $fileName ) )
 193          {
 194              $newLogRotateName = $fileName . '.' . 1;
 195              @rename( $fileName, $newLogRotateName );
 196              return true;
 197          }
 198          return false;
 199      }
 200  
 201      /*!
 202       \static
 203       Sets the maxium number of logrotate files to keep to \a $files.
 204      */
 205      function setLogrotateFiles( $files )
 206      {
 207          $GLOBALS['eZMaxLogrotateFiles'] = $files;
 208      }
 209  
 210  }
 211  
 212  ?>


Généré le : Sat Feb 24 10:30:04 2007 par Balluche grâce à PHPXref 0.7