1: <?php
2:
3: /**
4: * This file contains the cException class.
5: *
6: * @package Core
7: * @subpackage Exception
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: * Defines if an exception if this type should be logged.
29: * May be defined by any exception individually.
30: *
31: * @see CON-1690
32: * @var bool
33: */
34: protected $_log_exception = false;
35:
36: /**
37: * Saves an instance of the logger class for logging exceptions in the
38: * corresponding log.
39: *
40: * @var cLog the logger instance
41: */
42: protected $_logger = NULL;
43:
44: /**
45: * Constructor to create an instance of this class.
46: *
47: * @param string $message
48: * The Exception message to throw.
49: * @param int $code [optional]
50: * The Exception code.
51: * @param Exception $previous [optional]
52: * The previous exception used for the exception chaining.
53: */
54: public function __construct($message, $code = 0, Exception $previous = NULL) {
55: parent::__construct($message, $code, $previous);
56:
57: // create a logger class and save it for all logging purposes
58: $cfg = cRegistry::getConfig();
59: $writer = cLogWriter::factory("File", array(
60: 'destination' => $cfg['path']['contenido_logs'] . 'exception.txt'
61: ));
62: $this->_logger = new cLog($writer);
63:
64: // determine if exception should be logged
65: if (false === $this->_log_exception
66: && isset($cfg['debug']['log_exceptions'])) {
67: $this->_log_exception = $cfg['debug']['log_exceptions'];
68: }
69:
70: // log the exception if it should be logged
71: if (true === $this->_log_exception) {
72: $this->log();
73: }
74: }
75:
76: /**
77: * Logs this exception no matter if the log flag is set or not.
78: */
79: public function log() {
80: // construct the log message with all infos and write it via the logger
81: $logMessage = get_class($this) . ' thrown at line ' . $this->getLine() . ' of file ' . $this->getFile() . ".\r\n";
82: $logMessage .= 'Exception message: ' . $this->getMessage() . "\r\n";
83: $logMessage .= "Call stack:\r\n";
84: $logMessage .= $this->getTraceAsString();
85: $logMessage .= "\r\n";
86: $this->_logger->log($logMessage);
87: }
88:
89: }
90: