ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
sqlite.php
Go to the documentation of this file.
1<?php
31class Log_sqlite extends Log
32{
38 var $_options = array('mode' => 0666,
39 'persistent' => false);
40
46 var $_db = null;
47
54
60 var $_table = 'log_table';
61
62
74 function Log_sqlite($name, $ident = '', &$conf, $level = PEAR_LOG_DEBUG)
75 {
76 $this->_id = md5(microtime());
77 $this->_table = $name;
78 $this->_ident = $ident;
79 $this->_mask = Log::UPTO($level);
80
81 if (is_array($conf)) {
82 foreach ($conf as $k => $opt) {
83 $this->_options[$k] = $opt;
84 }
85 } else {
86 // If an existing database connection was provided, use it.
87 $this->_db =& $conf;
88 $this->_existingConnection = true;
89 }
90 }
91
99 function open()
100 {
101 if (is_resource($this->_db)) {
102 $this->_opened = true;
103 return $this->_createTable();
104 } else {
105 /* Set the connection function based on the 'persistent' option. */
106 if (empty($this->_options['persistent'])) {
107 $connectFunction = 'sqlite_open';
108 } else {
109 $connectFunction = 'sqlite_popen';
110 }
111
112 /* Attempt to connect to the database. */
113 if ($this->_db = $connectFunction($this->_options['filename'],
114 (int)$this->_options['mode'],
115 $error)) {
116 $this->_opened = true;
117 return $this->_createTable();
118 }
119 }
120
121 return $this->_opened;
122 }
123
132 function close()
133 {
134 /* We never close existing connections. */
135 if ($this->_existingConnection) {
136 return false;
137 }
138
139 if ($this->_opened) {
140 $this->_opened = false;
141 sqlite_close($this->_db);
142 }
143
144 return ($this->_opened === false);
145 }
146
160 function log($message, $priority = null)
161 {
162 /* If a priority hasn't been specified, use the default value. */
163 if ($priority === null) {
164 $priority = $this->_priority;
165 }
166
167 /* Abort early if the priority is above the maximum logging level. */
168 if (!$this->_isMasked($priority)) {
169 return false;
170 }
171
172 /* If the connection isn't open and can't be opened, return failure. */
173 if (!$this->_opened && !$this->open()) {
174 return false;
175 }
176
177 // Extract the string representation of the message.
178 $message = $this->_extractMessage($message);
179
180 // Build the SQL query for this log entry insertion.
181 $q = sprintf('INSERT INTO [%s] (logtime, ident, priority, message) ' .
182 "VALUES ('%s', '%s', %d, '%s')",
183 $this->_table,
184 strftime('%Y-%m-%d %H:%M:%S', time()),
185 sqlite_escape_string($this->_ident),
186 $priority,
187 sqlite_escape_string($message));
188 if (!($res = @sqlite_unbuffered_query($this->_db, $q))) {
189 return false;
190 }
191 $this->_announce(array('priority' => $priority, 'message' => $message));
192
193 return true;
194 }
195
202 function _createTable()
203 {
204 $q = "SELECT name FROM sqlite_master WHERE name='" . $this->_table .
205 "' AND type='table'";
206
207 $res = sqlite_query($this->_db, $q);
208
209 if (sqlite_num_rows($res) == 0) {
210 $q = 'CREATE TABLE [' . $this->_table . '] (' .
211 'id INTEGER PRIMARY KEY NOT NULL, ' .
212 'logtime NOT NULL, ' .
213 'ident CHAR(16) NOT NULL, ' .
214 'priority INT NOT NULL, ' .
215 'message)';
216
217 if (!($res = sqlite_unbuffered_query($this->_db, $q))) {
218 return false;
219 }
220 }
221
222 return true;
223 }
224
225}
const PEAR_LOG_DEBUG
Definition: Log.php:17
_createTable()
Checks whether the log table exists and creates it if necessary.
Definition: sqlite.php:202
log($message, $priority=null)
Inserts $message to the currently open database.
Definition: sqlite.php:160
Log_sqlite($name, $ident='', &$conf, $level=PEAR_LOG_DEBUG)
Constructs a new sql logging object.
Definition: sqlite.php:74
$_existingConnection
Definition: sqlite.php:53
close()
Closes the connection to the database if it is still open and we were the ones that opened it.
Definition: sqlite.php:132
open()
Opens a connection to the database, if it has not already been opened.
Definition: sqlite.php:99
UPTO($priority)
Calculate the log mask for all priorities up to the given priority.
Definition: Log.php:642
$_priority
Definition: Log.php:69
_isMasked($priority)
Check if the given priority is included in the current level mask.
Definition: Log.php:726
$_opened
Definition: Log.php:45
_announce($event)
Informs each registered observer instance that a new message has been logged.
Definition: Log.php:811
_extractMessage($message)
Returns the string representation of the message data.
Definition: Log.php:417
The Log:: class implements both an abstraction for various logging mechanisms and the Subject end of ...