1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
14:
15: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
16:
17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38:
39: class cLog {
40:
41: 42: 43: 44: 45:
46: const EMERG = 0;
47:
48: 49: 50: 51: 52:
53: const ALERT = 1;
54:
55: 56: 57: 58: 59:
60: const CRIT = 2;
61:
62: 63: 64: 65: 66:
67: const ERR = 3;
68:
69: 70: 71: 72: 73:
74: const WARN = 4;
75:
76: 77: 78: 79: 80:
81: const NOTICE = 5;
82:
83: 84: 85: 86: 87:
88: const INFO = 6;
89:
90: 91: 92: 93: 94:
95: const DEBUG = 7;
96:
97: 98: 99: 100: 101:
102: protected $_writer;
103:
104: 105: 106: 107: 108:
109: protected $_shortcutHandlers = array();
110:
111: 112: 113: 114: 115:
116: protected $_priorities = array();
117:
118: 119: 120: 121: 122:
123: protected $_defaultPriorities = array();
124:
125: 126: 127: 128: 129:
130: protected $_buffer = array();
131:
132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149:
150: public function __construct($writer = false) {
151: global $cfg;
152:
153: $createWriter = false;
154:
155: if ($writer == false) {
156: $createWriter = true;
157: } else if (!is_object($writer) || ($writer instanceof cLogWriter) == false) {
158: cWarning(__FILE__, __LINE__, "The passed class is not a subclass of cLogWriter. Creating new one.");
159: $createWriter = true;
160: }
161:
162: if ($createWriter == true) {
163: $options = array('destination' => $cfg['path']['contenido_logs'] . 'data/contenido.log');
164: $writer = cLogWriter::factory("File", $options);
165: }
166:
167: $this->setWriter($writer);
168: $this->setShortcutHandler("%date", array($this, "shDate"));
169: $this->setShortcutHandler("%level", array($this, "shLevel"));
170: $this->setShortcutHandler("%message", array($this, "shMessage"));
171:
172: $this->getWriter()->setOption('log_format', '[%date] [%level] %message', false);
173:
174: $reflection = new ReflectionClass($this);
175: $this->_priorities = $this->_defaultPriorities = array_flip($reflection->getConstants());
176: }
177:
178: 179: 180: 181: 182:
183: public function getWriter() {
184: return $this->_writer;
185: }
186:
187: 188: 189: 190: 191: 192:
193: public function setWriter(cLogWriter $writer) {
194: $this->_writer = $writer;
195: }
196:
197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212:
213: public function setShortcutHandler($shortcut, $handler) {
214: if ($shortcut == '') {
215: throw new cInvalidArgumentException('The shortcut name must not be empty.');
216: }
217:
218: if (substr($shortcut, 0, 1) == "%") {
219: $shortcut = substr($shortcut, 1);
220: }
221:
222: if (is_callable($handler) == false) {
223: throw new cInvalidArgumentException('The specified shortcut handler does not exist.');
224: }
225:
226: if (array_key_exists($shortcut, $this->_shortcutHandlers)) {
227: throw new cInvalidArgumentException('The shortcut ' . $shortcut . ' is already in use!');
228: }
229:
230: $this->_shortcutHandlers[$shortcut] = $handler;
231:
232: return true;
233: }
234:
235: 236: 237: 238: 239: 240: 241: 242: 243:
244: public function unsetShortcutHandler($shortcut) {
245: if (!in_array($shortcut, $this->_shortcutHandlers)) {
246: throw new cInvalidArgumentException('The specified shortcut handler does not exist.');
247: }
248:
249: unset($this->_shortcutHandlers[$shortcut]);
250: return true;
251: }
252:
253: 254: 255: 256: 257: 258: 259: 260:
261: public function buffer($message, $priority = NULL) {
262: $this->_buffer[] = array($message, $priority);
263: }
264:
265: 266: 267: 268: 269: 270: 271: 272:
273: public function commit($revoke = true) {
274: if (count($this->_buffer) == 0) {
275: cWarning(__FILE__, __LINE__, "There are no buffered messages to commit.");
276: return false;
277: }
278:
279: foreach ($this->_buffer as $bufferInfo) {
280: $this->log($bufferInfo[0], $bufferInfo[1]);
281: }
282:
283: if ($revoke == true) {
284: $this->revoke();
285: }
286: }
287:
288: 289: 290:
291: public function revoke() {
292: $this->_buffer = array();
293: }
294:
295: 296: 297: 298: 299: 300: 301: 302:
303: public function log($message, $priority = NULL) {
304: if ($priority && is_int($priority) == false && in_array($priority, $this->_priorities)) {
305: $priority = array_search($priority, $this->_priorities);
306: }
307:
308: if ($priority === NULL || array_key_exists($priority, $this->_priorities) == false) {
309: $priority = $this->getWriter()->getOption('default_priority');
310: }
311:
312: $logMessage = $this->getWriter()->getOption('log_format');
313: $lineEnding = $this->getWriter()->getOption('line_ending');
314:
315: foreach ($this->_shortcutHandlers as $shortcut => $handler) {
316: if (substr($shortcut, 0, 1) != "%") {
317: $shortcut = "%" . $shortcut;
318: }
319:
320: $info = array(
321: 'message' => $message,
322: 'priority' => $priority
323: );
324:
325: $value = call_user_func($handler, $info);
326:
327: $logMessage = str_replace($shortcut, $value, $logMessage);
328: }
329:
330: $this->getWriter()->write($logMessage . $lineEnding, $priority);
331: }
332:
333: 334: 335: 336: 337: 338: 339: 340: 341: 342:
343: public function addPriority($name, $value) {
344: if ($name == '') {
345: throw new cInvalidArgumentException('Priority name must not be empty.');
346: }
347:
348: if (in_array($name, $this->_priorities)) {
349: throw new cInvalidArgumentException('The given priority name already exists.');
350: }
351:
352: if (array_key_exists($value, $this->_priorities)) {
353: throw new cInvalidArgumentException('The priority value already exists.');
354: }
355:
356: $this->_priorities[$value] = $name;
357: }
358:
359: 360: 361: 362: 363: 364: 365: 366: 367:
368: public function removePriority($name) {
369: if ($name == '') {
370: throw new cInvalidArgumentException('Priority name must not be empty.');
371: }
372:
373: if (in_array($name, $this->_priorities) == false) {
374: throw new cInvalidArgumentException('Priority name does not exist.');
375: }
376:
377: if (in_array($name, $this->_defaultPriorities) == true) {
378: throw new cInvalidArgumentException('Removing default priorities is not allowed.');
379: }
380:
381: $priorityIndex = array_search($name, $this->_priorities);
382:
383: unset($this->_priorities[$priorityIndex]);
384: }
385:
386: 387: 388: 389: 390: 391: 392: 393: 394: 395:
396: public function __call($method, $arguments) {
397: $priorityName = strtoupper($method);
398:
399: if (in_array($priorityName, $this->_priorities) == false) {
400: throw new cInvalidArgumentException('The given priority ' . $priorityName . ' is not supported.');
401: }
402:
403: $priorityIndex = array_search($priorityName, $this->_priorities);
404:
405: $this->log($arguments[0], $priorityIndex);
406: }
407:
408: 409: 410: 411: 412: 413: 414:
415: public function shDate() {
416: return date("Y-m-d H:i:s");
417: }
418:
419: 420: 421: 422: 423: 424: 425: 426: 427: 428:
429: public function shLevel($info) {
430: $logLevel = $info['priority'];
431: return str_pad($this->_priorities[$logLevel], 10, " ", STR_PAD_BOTH);
432: }
433:
434: 435: 436: 437: 438: 439: 440: 441:
442: public function shMessage($info) {
443: return $info['message'];
444: }
445: }
446: