| 1 | rodolico | 1 | <?php
 | 
        
           |  |  | 2 |   | 
        
           |  |  | 3 |    class Logging {
 | 
        
           |  |  | 4 |       public $logFileName;
 | 
        
           |  |  | 5 |       public $maxLogSize;
 | 
        
           |  |  | 6 |       public $truncateLogByPercent;
 | 
        
           |  |  | 7 |   | 
        
           |  |  | 8 |       public function __construct( $logFileName = '', $maxLogSize = 0, $truncateLogByPercent = '0.1' ) {
 | 
        
           |  |  | 9 |          $this->logFileName = $logFileName;
 | 
        
           |  |  | 10 |          $this->maxLogSize = $maxLogSize;
 | 
        
           |  |  | 11 |          $this->truncateLogByPercent = $truncateLogByPercent;
 | 
        
           |  |  | 12 |       } // function __construct
 | 
        
           |  |  | 13 |   | 
        
           |  |  | 14 |       public function log( $string ) {
 | 
        
           |  |  | 15 |          if ( $logFileName ) {
 | 
        
           |  |  | 16 |             $fh = fopen( $this->logFileName, 'a');
 | 
        
           |  |  | 17 |             fwrite($fh, date('Y-m-d H:i:s') . "\t$string\n" );
 | 
        
           |  |  | 18 |             fclose($fh);
 | 
        
           |  |  | 19 |          }
 | 
        
           |  |  | 20 |          if ($this->maxLogSize) { // it is defined, so we must check on the size
 | 
        
           |  |  | 21 |             if (sprintf("%u", filesize($this->logFileName)) >= $this->maxLogSize ) // file is too big, remove top $truncateLogByPercent percent
 | 
        
           |  |  | 22 |                $this->truncateLog();
 | 
        
           |  |  | 23 |          }
 | 
        
           |  |  | 24 |       } // function log
 | 
        
           |  |  | 25 |   | 
        
           |  |  | 26 |    /* 
 | 
        
           |  |  | 27 |       Note: this is an initial attempt to build this function.
 | 
        
           |  |  | 28 |       First, it is not horribly critical if we don't truncate right at $maxLogSize, so if we don't get a lock, we just blow it off
 | 
        
           |  |  | 29 |       Second, we need to remove the first 10% (or whatever) of the file, so we can't use built in's like truncate or anything
 | 
        
           |  |  | 30 |       What I do here is read the first 10% (or whatever $truncateLogByPercent is), then do a gets (to get to the end of that line)
 | 
        
           |  |  | 31 |       Then, create a temporary file, dump the rest of the log file into it.
 | 
        
           |  |  | 32 |       Then, truncate the log file to 0, and dump the temp file back into it.
 | 
        
           |  |  | 33 |   | 
        
           |  |  | 34 |       I'm sure there is a much, much better way of doing this, and I would think that having two file handles on the same file would
 | 
        
           |  |  | 35 |       do it, but I'm scared of what other file systems would do, so I'll do it this way.
 | 
        
           |  |  | 36 |   | 
        
           |  |  | 37 |       NOTE: file_get_contents and file_put_contents would be more efficient, but I can't lock the file that way.
 | 
        
           |  |  | 38 |    */
 | 
        
           |  |  | 39 |    private function truncateLog() {
 | 
        
           |  |  | 40 |       $fh = fopen( $this->logFileName, 'w');
 | 
        
           |  |  | 41 |       if ( flock( $fh, LOCK_EX  | LOCK_NB ) ) { // try to get a lock, but don't be too concerned if we can't right now. We can always try next time
 | 
        
           |  |  | 42 |          // got the lock, truncate the file from the top :(
 | 
        
           |  |  | 43 |          fseek( $fh, $this->maxLogSize * $this->truncateLogByPercent ); // go past the first truncateLogByPercent percent of file
 | 
        
           |  |  | 44 |          fgets( $fh ); // go to the end of the line (text only)
 | 
        
           |  |  | 45 |          $tmpfh = tmpfile(); // create a temporary file
 | 
        
           |  |  | 46 |          while ( fwrite($tmpfh, fread($fh, 1024) ) ) {} // copy the rest of the file to a temp file
 | 
        
           |  |  | 47 |          fseek( $fh, 0 );  // move back to the beginning of the log file
 | 
        
           |  |  | 48 |          ftruncate( $fh, 0 ); // and empty it
 | 
        
           |  |  | 49 |          fseek( $tmpfh, 0); // and the temp file
 | 
        
           |  |  | 50 |          while ( fwrite($fh, fread($tmpfh, 1024) ) ) {} // copy the rest of the file to a temp file
 | 
        
           |  |  | 51 |          fclose ($fh); // unlocks it at the same time
 | 
        
           |  |  | 52 |          fclose ($tmpfh); // removes it from disk also
 | 
        
           |  |  | 53 |       }
 | 
        
           |  |  | 54 |    }
 | 
        
           |  |  | 55 |   | 
        
           |  |  | 56 |    } // class Logging
 | 
        
           |  |  | 57 |   | 
        
           |  |  | 58 | ?>
 |