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: * @throws cInvalidArgumentException
55: */
56: public function __construct($message, $code = 0, Exception $previous = NULL) {
57: parent::__construct($message, $code, $previous);
58:
59: // create a logger class and save it for all logging purposes
60: $cfg = cRegistry::getConfig();
61: $writer = cLogWriter::factory("File", array(
62: 'destination' => $cfg['path']['contenido_logs'] . 'exception.txt'
63: ));
64: $this->_logger = new cLog($writer);
65:
66: // determine if exception should be logged
67: if (false === $this->_log_exception
68: && isset($cfg['debug']['log_exceptions'])) {
69: $this->_log_exception = $cfg['debug']['log_exceptions'];
70: }
71:
72: // log the exception if it should be logged
73: if (true === $this->_log_exception) {
74: $this->log();
75: }
76: }
77:
78: /**
79: * Logs this exception no matter if the log flag is set or not.
80: */
81: public function log() {
82: // construct the log message with all infos and write it via the logger
83: $logMessage = get_class($this) . ' thrown at line ' . $this->getLine() . ' of file ' . $this->getFile() . ".\r\n";
84: $logMessage .= 'Exception message: ' . $this->getMessage() . "\r\n";
85: $logMessage .= "Call stack:\r\n";
86: $logMessage .= $this->getTraceAsString();
87: $logMessage .= "\r\n";
88: $this->_logger->log($logMessage);
89: }
90:
91: }
92: