1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace ManiaLive\Application;
13:
14: use ManiaLive\Utilities\Logger;
15: use ManiaLive\Utilities\Console;
16:
17: abstract class ErrorHandling
18: {
19: static $errorCount = 0;
20:
21: 22: 23: 24:
25: public static function increaseErrorCount()
26: {
27: self::$errorCount++;
28:
29:
30: $config = \ManiaLive\Config\Config::getInstance();
31: if($config->maxErrorCount !== false && self::$errorCount > $config->maxErrorCount)
32: die();
33: }
34:
35: 36: 37: 38: 39: 40: 41: 42:
43: public static function createExceptionFromError($errno, $errstr, $errfile, $errline)
44: {
45: if(error_reporting() & $errno)
46: throw new \ErrorException($errstr, $errno, 0, $errfile, $errline);
47: }
48:
49: 50: 51: 52:
53: public static function processRuntimeException(\Exception $e)
54: {
55: self::increaseErrorCount();
56: self::displayAndLogError($e);
57: }
58:
59: 60: 61: 62:
63: public static function processModuleException(\Exception $e)
64: {
65:
66:
67: if($e instanceof FatalException || $e instanceof CriticalEventException)
68: throw $e;
69:
70: else
71: {
72: self::increaseErrorCount();
73: self::displayAndLogError($e);
74: }
75: }
76:
77: 78: 79: 80:
81: public static function processEventException(\Exception $e)
82: {
83: if($e instanceof CriticalEventException)
84: {
85: if(!($e instanceof SilentCriticalEventException))
86: {
87: self::increaseErrorCount();
88: self::displayAndLogError($e);
89: }
90: }
91:
92:
93: else
94: throw $e;
95: }
96:
97: 98: 99: 100: 101:
102: public static function displayAndLogError(\Exception $e)
103: {
104: $log = PHP_EOL.' Occured on '.date("d.m.Y").' at '.date("H:i:s").' at process with ID #'.getmypid().PHP_EOL
105: .' ---------------------------------'.PHP_EOL;
106: Console::println('');
107: foreach (self::computeMessage($e) as $line)
108: {
109: $log .= $line.PHP_EOL;
110: Console::println(wordwrap($line, 73, PHP_EOL.' ', true));
111: }
112: Console::println('');
113:
114: Logger::getLog('error')->write($log);
115:
116:
117: if(\ManiaLive\Config\Config::getInstance()->globalErrorLog)
118: error_log($log, 3, APP_ROOT.'logs'.DIRECTORY_SEPARATOR.'GlobalErrorLog.txt');
119: }
120:
121: 122: 123: 124:
125: public static function processStartupException(\Exception $e)
126: {
127: $message = PHP_EOL.'Critical startup error!'.PHP_EOL;
128: foreach(self::computeMessage($e) as $line)
129: $message .= wordwrap($line, 73, PHP_EOL.' ', true).PHP_EOL;
130: $message .= PHP_EOL;
131:
132:
133: error_log($message, 3, APP_ROOT.'logs'.DIRECTORY_SEPARATOR.'ErrorLog_'.getmypid().'.txt');
134:
135: die($message);
136: }
137:
138: 139: 140:
141: static protected function computeMessage(\Exception $e)
142: {
143: $line = $e->getLine();
144: $code = $e->getCode();
145: $file = $e->getFile();
146: $message = $e->getMessage();
147: $trace = $e->getTraceAsString();
148:
149: $buffer = array();
150: $buffer[] = ' -> ' . get_class($e) . ' with code ' . $code;
151: $buffer[] = ' ' . $message;
152: $buffer[] = ' - in ' . $file . ' on line ' . $line;
153: $buffer[] = ' - Stack: ';
154:
155: $lines = explode("\n", $trace);
156: foreach($lines as $i => $line)
157: {
158: if($i == 0)
159: $buffer[count($buffer)-1] .= $line;
160: else
161: $buffer[] = ' '.$line;
162: }
163:
164: return $buffer;
165: }
166: }
167:
168: class FatalException extends \Exception {}
169: class CriticalEventException extends \Exception {}
170: class SilentCriticalEventException extends CriticalEventException {}
171: ?>