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