root/trunk/common/mvblog_debug.php

Revision 776, 3.8 KB (checked in by michiel, 9 months ago)

update copyright year.

Closes #180

Line 
1<?php
2/**
3 * MvBlog -- An open source no-nosense blogtool
4 *
5 * Copyright (C) 2005-2008, Michiel van Baak
6 * Michiel van Baak <mvanbaak@users.sourceforge.net>
7 *
8 * See http://dev.mvblog.org for more information on MvBlog.
9 * That page also provides Bugtrackers, Filereleases etc.
10 *
11 * This program is free software, distributed under the terms of
12 * the GNU General Public License Version 2. See the LICENSE file
13 * at the top of the source tree.
14 *
15 * @package MvBlog
16 * @author Ferry Boender
17 * @version 2.0
18 * @copyright 2005-2008 Ferry Boender
19 */
20
21/**
22 * Debugging class that takes care of error reporting, etc.
23 * @package MvBlog
24 */
25Class MvBlog_debug {
26    /**
27     * @var array $development_hostnames Hostnames that will turn on strict errors.
28     */
29    public static $development_hostnames = array(
30        "dev",
31        "localhost",
32        "127.0.0.1"
33    );
34
35    /**
36     * @var array $error_names List of errors in int and string format.
37     */
38    public static $error_names = array(
39        1    => "E_ERROR",
40        2    => "E_WARNING",
41        4    => "E_PARSE",
42        8    => "E_NOTICE",
43        16   => "E_CORE_ERROR",
44        32   => "E_CORE_WARNING",
45        64   => "E_COMPILE_ERROR",
46        128  => "E_COMPILE_WARNING",
47        256  => "E_USER_ERROR",
48        512  => "E_USER_WARNING",
49        1024 => "E_USER_NOTICE",
50        2048 => "E_STRICT",
51        4096 => "E_RECOVERABLE_ERROR",
52        8191 => "E_ALL",
53    );
54
55    /**
56     * Starts the development error reporting, which is much stricter than
57     * normal. This is to ensure that developers fix errors and notices that
58     * occur. This will only be set when the current hostname exists in the
59     * MvBlog_debug::$development_hostnames array.
60     *
61     * @param bool $warn_strict (optional) Warn/stop on strict errors?
62     */
63    public static function start_development($warn_strict = True) {
64        /* Turn on heavy error reporting so that no errors go unnoticed, but
65           only for development hostnames. This way end-users won't accidentally get
66           errors they shouldn't be seeying. */
67        if (
68            array_key_exists("SERVER_NAME", $_SERVER) &&
69            in_array($_SERVER["SERVER_NAME"], MvBlog_debug::$development_hostnames)
70        ) {
71            MvBlog_debug::start_pendantic_errors($warn_strict);
72        }
73
74    }
75    /**
76     * Starts the pendantic error logging mode. Every error will stop execution
77     * of the script straight away. You can specify if you DON'T want Future
78     * errors to also stop the script.
79     *
80     * @param bool $warn_strict (optional) Warn/stop on strict errors?
81     */
82    public static function start_pendantic_errors($warn_strict = True) {
83        // Asserts can be used to test preconditions in, e.g. functions. They
84        // are off by default. Turn them on here.
85        assert_options(ASSERT_ACTIVE, 1);
86        assert_options(ASSERT_WARNING, 1);
87        assert_options(ASSERT_BAIL, 1);
88
89        // Turn on the highest level of error reporting.
90        if ($warn_strict && defined("E_STRICT")) {
91            error_reporting(E_ALL | E_STRICT);
92        } else {
93            error_reporting(E_ALL);
94        }
95
96        // Show errors in the source code (to annoy developers)
97        ini_set("display_errors", "1");
98
99        // Set a custom error handler that's stricter than the normal one.
100        set_error_handler(array("MvBlog_debug", "user_error_handler"));
101    }
102
103    /**
104     * The MvBlog custom error handler. This error handler will does basically
105     * the same as the normal error, except that it also exists on when an
106     * error or notice is encountered.
107     *
108     * @param int $errno The php error number
109     * @param string $errstr The php error message
110     * @param string $errfile The php file where the error is
111     * @param int $errline The line where the error is
112     */
113    public static function user_error_handler($errno, $errstr, $errfile, $errline) {
114        $error_reporting_level = error_reporting();
115
116        // Only show errors for which error reporting level is turned on.
117        if ($error_reporting_level & $errno) {
118            $errtype = MvBlog_debug::$error_names[$errno];
119            echo($errtype." error in <b>".$errfile."</b>:<b>".$errline."</b> - <i>".$errstr."</i>");
120            exit();
121        }
122    }
123}
Note: See TracBrowser for help on using the browser.