6 * Contains header-related functions.
8 * @author Tim Duesterhus
9 * @copyright 2001-2019 WoltLab GmbH
10 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
11 * @package WoltLabSuite\Core\Util
14 final class ExceptionLogUtil
{
16 * Splits the given string of Exceptions into an array.
18 * @param string $contents
21 public static function splitLog($contents) {
23 $contents = StringUtil
::unifyNewlines($contents);
26 $split = new Regex('(?:^|\n<<<<\n\n)(?:<<<<<<<<([a-f0-9]{40})<<<<\n|$)');
27 $contents = $split->split($contents, Regex
::SPLIT_NON_EMPTY_ONLY | Regex
::CAPTURE_SPLIT_DELIMITER
);
29 if (empty($contents)) {
33 // even items become keys, odd items become values
34 return array_merge(...array_map(
36 return [$v[0] => $v[1]];
38 array_chunk($contents, 2)
43 * Parses the given log entry.
45 * @param string $entry
48 public static function parseException($entry) {
50 static $chainRegex = null;
51 if ($regex === null ||
$chainRegex === null) {
52 $regex = new Regex("(?P<date>[MTWFS][a-z]{2}, \d{1,2} [JFMASOND][a-z]{2} \d{4} \d{2}:\d{2}:\d{2} [+-]\d{4})\s*\n".
53 "Message: (?P<message>.*?)\s*\n".
54 "PHP version: (?P<phpVersion>.*?)\s*\n".
55 "WoltLab Suite version: (?P<wcfVersion>.*?)\s*\n".
56 "Request URI: (?P<requestURI>.*?)\s*\n".
57 "Referrer: (?P<referrer>.*?)\s*\n".
58 "User Agent: (?P<userAgent>.*?)\s*\n".
59 "Peak Memory Usage: (?<peakMemory>\d+)/(?<maxMemory>(?:\d+|-1))\s*\n".
61 ".*)", Regex
::DOT_ALL
);
62 $chainRegex = new Regex("======\n".
63 "Error Class: (?P<class>.*?)\s*\n".
64 "Error Message: (?P<message>.*?)\s*\n".
65 "Error Code: (?P<code>[a-zA-Z0-9]+)\s*\n".
66 "File: (?P<file>.*?) \((?P<line>\d+)\)\s*\n".
67 "Extra Information: (?P<information>(?:-|[a-zA-Z0-9+/]+={0,2}))\s*\n".
68 "Stack Trace: (?P<stack>\[[^\n]+\])", Regex
::DOT_ALL
);
71 if (!$regex->match($entry)) {
72 throw new \
InvalidArgumentException('The given entry is malformed');
74 $matches = $regex->getMatches();
75 $chainRegex->match($matches['chain'], true, Regex
::ORDER_MATCH_BY_SET
);
77 $chainMatches = array_map(function ($item) {
78 if ($item['information'] === '-') {
79 $item['information'] = null;
82 $item['information'] = unserialize(base64_decode($item['information']), ['allowed_classes' => false]);
85 $item['stack'] = JSON
::decode($item['stack']);
88 }, $chainRegex->getMatches());
90 $matches['date'] = strtotime($matches['date']);
91 $matches['chain'] = $chainMatches;
97 * Forbid creation of ExceptionLogUtil objects.
99 private function __construct() {