Subversion Repositories phpLibraryV2

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
5 rodolico 1
<?php
2
 
3
   class file {
4
 
5
      // fully qualified path and file name
6
      protected $fullFileName;
7
      // file name
8
      protected $filename;
9
      // path to file
10
      protected $path;
11
      // permissions to assign to path
12
      protected $pathPermission = 0777;
13
      // mode to open in. Default is append with lock
14
      public $mode;
15
 
16
      /*
17
       * constructor must have file name, all else is default
18
       * however filename may be empty or null
19
       */
20
      public function __construct( $filename, $mode = null ) {
21
         $this->mode = $mode ? $mode : ( FILE_APPEND | LOCK_EX );
22
         if ( isset( $filename ) && $filename ) {
23
            $temp = pathinfo( $filename );
24
            $this->filename = $temp['basename'];
25
            $this->path = $temp['dirname'];
26
            $this->fullFileName = $this->path . '/' . $this->filename;
27
            $this->makePath(); // will return false if we could not create the path
28
         }
29
      } // __construct
30
 
31
      /*
32
       * creates path $path if it does not exist
33
       * sets permissions to $pathPermission
34
       * Note: this does a recursive creation (mkdir -p) but does not
35
       *       check permissions on existing directories
36
       * returns true on success, false on failure
37
       */
38
      private function makePath() {
39
         /* we'll return true if the path exists. HOWEVER, note that
40
          * we do not check permissions, so this could cause issues
41
          */
42
         if ( is_dir( $this->path ) )
43
            return true; 
44
         /* path does not exist. Save the current umask and set it 
45
          * temporarily to 0, then create the directory and restore
46
          * umask
47
          */
48
         $oldmask = umask(0); // set umask to 0 temporarily
49
         $result = mkdir( $this->path, $this->pathPermission, true ); // recursively build tree
50
         umask( $oldmask ); // restore umask
51
         return $result;
52
      } // makePath
53
   } // class file
54
 
55
 
56
   class logFile extends file {
57
      // if set, will compress and save old file and start new
58
      // when this size is reached. May have suffix 'k','m', 'g'
59
      // null means unlimited
60
      public $maxSize = null;
61
      // if $maxSize, this will unlink any old compressed versions
62
      // to keep the number of files below this.
63
      // null means keep all
64
      public $maxOld = null;
65
 
66
      public function __constructor (  $filename = null, 
67
                                       $mode = null, 
68
                                       $maxSize = null,
69
                                       $maxOld = null ) {
70
         $this->maxSize = $maxSize;
71
         $this->maxOld = $maxOld;
72
         if ( ! isset( $filename ) && isset( $_SESSION['global']['logger']['filename'] ) )
73
            $this->filename = $_SESSION['global']['logger']['filename'];
74
         parent::constructor( $filename, $mode );
75
      } // __constructor
76
 
77
 
78
      /*
79
       * writeLog
80
       * writes a log entry to $fullFileName if $fullFileName is set
81
       * log entry consists of a date/time stamp, a tab, the message, 
82
       * and a line return for EACH array element passed (if an array)
83
       * 
84
       * returns true if the bytes written on all lines
85
       * returns false if $fullFileName not initialized or if byte count 
86
       * does not match string output size
87
       */
88
      public function writeLog ( $message, $format = null ) {
89
         file_put_contents( $this->fullFileName, $message, $this->mode );
90
         if ( ! $message ) return true; // if empty message
91
         // if $message is a scalar, convert to array
92
         if ( ! is_array( $message ) ) 
93
            $message = array( $message );
94
         if ( ! isset( $format ) )
95
            $format = date( 'Y-m-d H:i:s' ) . "\t";
96
         $line = $format . implode( "\n" . $format , $message ) . "\n";
97
         if ( isset( $this->fullFileName ) ) {
98
            return file_put_contents( $this->fullFileName, $line, $this->mode ) === strlen( $line );
99
         }
100
         return false; // $fullFileName is not set???
101
      } // function writeLog
102
 
103
   } // class logFile
104
 
105
   class debugFile extends logFile {
106
 
107
      public $logLevel = 0; // logging level
108
 
109
      public function __constructor( $filename, $logLevel = 0, $maxSize = null ) {
110
         $this->logLevel = $logLevel;
111
         parent::_constructor( $filename, null, $maxSize, null );
112
      }
113
 
114
      public function writeLog( $logLevel, $message, $file = null, $class = null, $function = null, $line = null ) {
115
         if ( $logLevel <= $this->logLevel ) {
116
            $extra = array();
117
            if ( isset( $file ) ) $extra[] = "File: $file";
118
            if ( isset( $class ) ) $extra[] = "Class: $class";
119
            if ( isset( $function ) ) $extra[] = "Function: $function";
120
            if ( isset( $line ) ) $extra[] = "Line: $line";
121
            $message = implode( ',', $extra ) . $message;
122
            return parent::writeLog( $message );
123
         } else {
124
            return true;
125
         }
126
      } // writeLog
127
   } // class logging
128
 
129
?>
130