summaryrefslogtreecommitdiffstats
path: root/codebase/Dhtmlx/Connector/Tools/LogMaster.php
blob: da4dd69ff12482ab574aa6fcdf3b01829d5ff767 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php

namespace Dhtmlx\Connector\Tools;
/*! Controls error and debug logging.
	Class designed to be used as static object.
**/
class LogMaster{
    private static $_log=false;//!< logging mode flag
    private static $_output=false;//!< output error infor to client flag
    private static $session="";//!< all messages generated for current request

    /*! convert array to string representation ( it is a bit more readable than var_dump )

        @param data
            data object
        @param pref
            prefix string, used for formating, optional
        @return
            string with array description
    */
    private static function log_details($data,$pref=""){
        if (is_array($data)){
            $str=array("");
            foreach($data as $k=>$v)
                array_push($str,$pref.$k." => ".LogMaster::log_details($v,$pref."\t"));
            return implode("\n",$str);
        }
        return $data;
    }
    /*! put record in log

        @param str
            string with log info, optional
        @param data
            data object, which will be added to log, optional
    */
    public static function log($str="",$data=""){
        if (LogMaster::$_log){
            $message = $str.LogMaster::log_details($data)."\n\n";
            LogMaster::$session.=$message;
            error_log($message,3,LogMaster::$_log);
        }
    }

    /*! get logs for current request
        @return
            string, which contains all log messages generated for current request
    */
    public static function get_session_log(){
        return LogMaster::$session;
    }

    /*! error handler, put normal php errors in log file

        @param errn
            error number
        @param errstr
            error description
        @param file
            error file
        @param line
            error line
        @param context
            error cntext
    */
    public static function error_log($errn,$errstr,$file,$line,$context){
        LogMaster::log($errstr." at ".$file." line ".$line);
    }

    /*! exception handler, used as default reaction on any error - show execution log and stop processing

        @param exception
            instance of Exception
    */
    public static function exception_log($exception){
        LogMaster::log("!!!Uncaught Exception\nCode: " . $exception->getCode() . "\nMessage: " . $exception->getMessage());
        if (LogMaster::$_output){
            echo "<pre><xmp>\n";
            echo LogMaster::get_session_log();
            echo "\n</xmp></pre>";
        }
        die();
    }

    /*! enable logging

        @param name
            path to the log file, if boolean false provided as value - logging will be disabled
        @param output
            flag of client side output, if enabled - session log will be sent to client side in case of an error.
    */
    public static function enable_log($name,$output=false){
        LogMaster::$_log=$name;
        LogMaster::$_output=$output;
        if ($name){
            set_error_handler(array("Dhtmlx\\Connector\\Tools\\LogMaster","error_log"),E_ALL);
            set_exception_handler(array("Dhtmlx\\Connector\\Tools\\LogMaster","exception_log"));
            LogMaster::log("\n\n====================================\nLog started, ".date("d/m/Y h:i:s")."\n====================================");
        }
    }
}