Subversion Repositories phpLibraryV2

Rev

Rev 6 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

<?php

   class file {
      
      // fully qualified path and file name
      protected $fullFileName;
      // file name
      protected $filename;
      // path to file
      protected $path;
      // permissions to assign to path
      protected $pathPermission = 0777;
      // mode to open in. Default is append with lock
      public $mode;

      /*
       * constructor must have file name, all else is default
       * however filename may be empty or null
       */
      public function __construct( $filename, $mode = null ) {
         $this->mode = $mode ? $mode : ( FILE_APPEND | LOCK_EX );
         if ( isset( $filename ) && $filename ) {
            $temp = pathinfo( $filename );
            $this->filename = $temp['basename'];
            $this->path = $temp['dirname'];
            $this->fullFileName = $this->path . '/' . $this->filename;
            $this->makePath(); // will return false if we could not create the path
         }
      } // __construct
      
      /*
       * creates path $path if it does not exist
       * sets permissions to $pathPermission
       * Note: this does a recursive creation (mkdir -p) but does not
       *       check permissions on existing directories
       * returns true on success, false on failure
       */
      private function makePath() {
         /* we'll return true if the path exists. HOWEVER, note that
          * we do not check permissions, so this could cause issues
          */
         if ( is_dir( $this->path ) )
            return true; 
         /* path does not exist. Save the current umask and set it 
          * temporarily to 0, then create the directory and restore
          * umask
          */
         $oldmask = umask(0); // set umask to 0 temporarily
         $result = mkdir( $this->path, $this->pathPermission, true ); // recursively build tree
         umask( $oldmask ); // restore umask
         return $result;
      } // makePath
   } // class file
   
   
   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 $fullFileName not initialized or if byte count 
       * does not match string output size
       */
      public function writeLog ( $message, $format = null ) {
         file_put_contents( $this->fullFileName, $message, $this->mode );
         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";
         if ( isset( $this->fullFileName ) ) {
            return file_put_contents( $this->fullFileName, $line, $this->mode ) === strlen( $line );
         }
         return false; // $fullFileName is not set???
      } // function writeLog
      
   } // class logFile
   
   class debugFile extends logFile {
      
      public $logLevel = 0; // logging level
      
      public function __constructor( $filename, $logLevel = 0, $maxSize = null ) {
         $this->logLevel = $logLevel;
         parent::_constructor( $filename, null, $maxSize, null );
      }
      
      public function writeLog( $logLevel, $message, $file = null, $class = null, $function = null, $line = null ) {
         if ( $logLevel <= $this->logLevel ) {
            $extra = array();
            if ( isset( $file ) ) $extra[] = "File: $file";
            if ( isset( $class ) ) $extra[] = "Class: $class";
            if ( isset( $function ) ) $extra[] = "Function: $function";
            if ( isset( $line ) ) $extra[] = "Line: $line";
            $message = implode( ',', $extra ) . $message;
            return parent::writeLog( $message );
         } else {
            return true;
         }
      } // writeLog
   } // class logging
   
?>