Subversion Repositories phpLibraryV2

Rev

Rev 6 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

<?php

   require_once 'File.class.php';
   
   class LogFile extends File {
      // if set, will compress and save old file and start new
      // when this size is reached. May have suffix 'k','m', 'g'
      // null means unlimited
      public $maxSize = null;
      // if $maxSize, this will unlink any old compressed versions
      // to keep the number of files below this.
      // null means keep all
      public $maxOld = null;
      
      public function __constructor (  $filename = null, 
                                       $mode = null, 
                                       $maxSize = null,
                                       $maxOld = null ) {
         $this->maxSize = $maxSize;
         $this->maxOld = $maxOld;
         if ( ! isset( $filename ) && isset( $_SESSION['global']['logger']['filename'] ) )
            $this->filename = $_SESSION['global']['logger']['filename'];
         parent::constructor( $filename, $mode );
      } // __constructor


      /*
       * writeLog
       * writes a log entry to $fullFileName if $fullFileName is set
       * log entry consists of a date/time stamp, a tab, the message, 
       * and a line return for EACH array element passed (if an array)
       * 
       * returns true if the bytes written on all lines
       * returns false if $file can not be opened or if byte count 
       * does not match string output size
       */
      public function writeLog ( $message, $format = null ) {
         if ( ! isset( $this->fh ) ) $this->openFile();
         if ( ! isset( $this->fh ) ) return false;
         if ( ! $message ) return true; // if empty message

         // if $message is a scalar, convert to array
         if ( ! is_array( $message ) ) 
            $message = array( $message );
         if ( ! isset( $format ) )
            $format = date( 'Y-m-d H:i:s' ) . "\t";
         $line = $format . implode( "\n" . $format , $message ) . "\n";
         return ( fwrite( $this->fh, $line ) === strlen( $line ) );
      } // function writeLog
   } // class logFile
   
?>