1: <?php
2: /**
3: * This file contains the cException class.
4: *
5: * @package Core
6: * @subpackage Exception
7: * @version SVN Revision $Rev:$
8: *
9: * @author Simon Sprankel
10: * @copyright four for business AG <www.4fb.de>
11: * @license http://www.contenido.org/license/LIZENZ.txt
12: * @link http://www.4fb.de
13: * @link http://www.contenido.org
14: */
15:
16: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
17:
18: /**
19: * cException is the base class for all exceptions.
20: * You should use this CONTENIDO exception instead of the standard PHP
21: * {@link Exception}.
22: * This exception type is logged to data/logs/exception.txt.
23: * If there is a more specific and more appropriate subclass, use the subclass!
24: */
25: class cException extends Exception {
26:
27: /**
28: * Saves an instance of the logger class for logging exceptions in the
29: * corresponding log.
30: *
31: * @var cLog the logger instance
32: */
33: protected $_logger = null;
34:
35: /**
36: * Saves whether the exception should be logged - defaults to true.
37: *
38: * @var boolean whether the exception should be logged
39: */
40: protected $_log = true;
41:
42: /**
43: * Constructs the Exception.
44: *
45: * @param string $message The Exception message to throw.
46: * @param int $code The Exception code.
47: * @param Exception $previous The previous exception used for the exception
48: * chaining.
49: */
50: public function __construct($message, $code = 0, Exception $previous = null) {
51: parent::__construct($message, $code, $previous);
52:
53: // create a logger class and save it for all logging purposes
54: $cfg = cRegistry::getConfig();
55: $writer = cLogWriter::factory("File", array(
56: 'destination' => $cfg['path']['contenido_logs'] . 'exception.txt'
57: ));
58: $this->_logger = new cLog($writer);
59:
60: // log the exception if it should be logged
61: if ($this->_log) {
62: $this->log();
63: }
64: }
65:
66: /**
67: * Logs this exception no matter if the log flag is set or not.
68: */
69: public function log() {
70: // construct the log message with all infos and write it via the logger
71: $logMessage = get_class($this) . ' thrown at line ' . $this->getLine() . ' of file ' . $this->getFile() . ".\r\n";
72: $logMessage .= 'Exception message: ' . $this->getMessage() . "\r\n";
73: $logMessage .= "Call stack:\r\n";
74: $logMessage .= $this->getTraceAsString();
75: $logMessage .= "\r\n";
76: $this->_logger->log($logMessage);
77: }
78:
79: }
80: