Take two:
[www-register-wizard.git] / libraries / Log.php
1 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');\r
2 /**\r
3  * CodeIgniter\r
4  *\r
5  * An open source application development framework for PHP 4.3.2 or newer\r
6  *\r
7  * @package             CodeIgniter\r
8  * @author              ExpressionEngine Dev Team\r
9  * @copyright   Copyright (c) 2008, EllisLab, Inc.\r
10  * @license             http://codeigniter.com/user_guide/license.html\r
11  * @link                http://codeigniter.com\r
12  * @since               Version 1.0\r
13  * @filesource\r
14  */\r
15 \r
16 // ------------------------------------------------------------------------\r
17 \r
18 /**\r
19  * Logging Class\r
20  *\r
21  * @package             CodeIgniter\r
22  * @subpackage  Libraries\r
23  * @category    Logging\r
24  * @author              ExpressionEngine Dev Team\r
25  * @link                http://codeigniter.com/user_guide/general/errors.html\r
26  */\r
27 class CI_Log {\r
28 \r
29         var $log_path;\r
30         var $_threshold = 1;\r
31         var $_date_fmt  = 'Y-m-d H:i:s';\r
32         var $_enabled   = TRUE;\r
33         var $_levels    = array('ERROR' => '1', 'DEBUG' => '2',  'INFO' => '3', 'ALL' => '4');\r
34 \r
35         /**\r
36          * Constructor\r
37          *\r
38          * @access      public\r
39          */\r
40         function CI_Log()\r
41         {\r
42                 $config =& get_config();\r
43                 \r
44                 $this->log_path = ($config['log_path'] != '') ? $config['log_path'] : BASEPATH.'logs/';\r
45                 \r
46                 if ( ! is_dir($this->log_path) OR ! is_really_writable($this->log_path))\r
47                 {\r
48                         $this->_enabled = FALSE;\r
49                 }\r
50                 \r
51                 if (is_numeric($config['log_threshold']))\r
52                 {\r
53                         $this->_threshold = $config['log_threshold'];\r
54                 }\r
55                         \r
56                 if ($config['log_date_format'] != '')\r
57                 {\r
58                         $this->_date_fmt = $config['log_date_format'];\r
59                 }\r
60         }\r
61         \r
62         // --------------------------------------------------------------------\r
63         \r
64         /**\r
65          * Write Log File\r
66          *\r
67          * Generally this function will be called using the global log_message() function\r
68          *\r
69          * @access      public\r
70          * @param       string  the error level\r
71          * @param       string  the error message\r
72          * @param       bool    whether the error is a native PHP error\r
73          * @return      bool\r
74          */             \r
75         function write_log($level = 'error', $msg, $php_error = FALSE)\r
76         {               \r
77                 if ($this->_enabled === FALSE)\r
78                 {\r
79                         return FALSE;\r
80                 }\r
81         \r
82                 $level = strtoupper($level);\r
83                 \r
84                 if ( ! isset($this->_levels[$level]) OR ($this->_levels[$level] > $this->_threshold))\r
85                 {\r
86                         return FALSE;\r
87                 }\r
88         \r
89                 $filepath = $this->log_path.'log-'.date('Y-m-d').EXT;\r
90                 $message  = '';\r
91                 \r
92                 if ( ! file_exists($filepath))\r
93                 {\r
94                         $message .= "<"."?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?".">\n\n";\r
95                 }\r
96                         \r
97                 if ( ! $fp = @fopen($filepath, FOPEN_WRITE_CREATE))\r
98                 {\r
99                         return FALSE;\r
100                 }\r
101 \r
102                 $message .= $level.' '.(($level == 'INFO') ? ' -' : '-').' '.date($this->_date_fmt). ' --> '.$msg."\n";\r
103                 \r
104                 flock($fp, LOCK_EX);    \r
105                 fwrite($fp, $message);\r
106                 flock($fp, LOCK_UN);\r
107                 fclose($fp);\r
108         \r
109                 @chmod($filepath, FILE_WRITE_MODE);             \r
110                 return TRUE;\r
111         }\r
112 \r
113 }\r
114 // END Log Class\r
115 \r
116 /* End of file Log.php */\r
117 /* Location: ./system/libraries/Log.php */