Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
Log
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 10
420
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getLogLevel
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setLogLevel
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setLogCallback
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 v
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 d
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 w
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 e
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 log
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
 defaultLogCallback
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
90
1<?php
2namespace Ably;
3
4/**
5 * Ably logger, this is a static class
6 */
7class Log {
8    const NONE    = 0;
9    const ERROR   = 1;
10    const WARNING = 2;
11    const DEBUG   = 3;
12    const VERBOSE = 4;
13
14    protected static $logLevel;
15    protected static $logCallback;
16
17    private function __construct() {
18        // this is a static class!
19    }
20
21    /**
22     * @return integer currently set log level
23     */
24    public static function getLogLevel() {
25        return self::$logLevel;
26    }
27
28    /**
29     * Sets the log level
30     * @param integer $loglevel one of the log level constants, e.g. Log::DEBUG
31     */
32    public static function setLogLevel( $logLevel ) {
33        self::$logLevel = $logLevel;
34    }
35
36    /**
37     * Sets a custom logging function that will be called in place of self::log()
38     * @param function|null $function Custom function or leave empty to revert to default
39     */
40    public static function setLogCallback( $function = null ) {
41        self::$logCallback = $function;
42    }
43
44    /**
45     * Log verbose level information.
46     * @param mixed ... Any number of variables or messages to log
47     */
48    public static function v(/*...*/) {
49        self::log( Log::VERBOSE, func_get_args() );
50    }
51
52    /**
53     * Log debug level information.
54     * @param mixed ... Any number of variables or messages to log
55     */
56    public static function d(/*...*/) {
57        self::log( Log::DEBUG, func_get_args() );
58    }
59
60    /**
61     * Log warning level information.
62     * @param mixed ... Any number of variables or messages to log
63     */
64    public static function w(/*...*/) {
65        self::log( Log::WARNING, func_get_args() );
66    }
67
68    /**
69     * Log error level information.
70     * @param mixed ... Any number of variables or messages to log
71     */
72    public static function e(/*...*/) {
73        self::log( Log::ERROR, func_get_args() );
74    }
75
76    /**
77     * Logs provided message if log level matches requested level.
78     * Calls either built-in function or a custom callback if provided
79     * @param integer $level Log level
80     * @param array $args arguments to dump
81     */
82    public static function log( $level, $args ) {
83        if ( self::$logLevel >= $level ) {
84            $function = self::$logCallback;
85            return $function ? $function( $level, $args ) : self::defaultLogCallback( $level, $args );
86        }
87    }
88
89    /**
90     * The default logging function
91     */
92    private static function defaultLogCallback( $level, $args ) {
93        $last = count($args) - 1;
94
95        $timestamp = date( "Y-m-d H:i:s\t" );
96
97        foreach ($args as $i => $arg) {
98            if (is_string($arg)) {
99                file_put_contents( 'php://stdout', $timestamp . $arg . ($i == $last ? "\n" : "\t") );
100            }
101            else if (is_bool($arg)) {
102                file_put_contents( 'php://stdout', $timestamp . ($arg ? 'true' : 'false') . ($i == $last ? "\n" : "\t") );
103            }
104            else if (is_scalar($arg)) {
105                file_put_contents( 'php://stdout', $timestamp . $arg . ($i == $last ? "\n" : "\t") );
106            }
107            else {
108                file_put_contents( 'php://stdout', $timestamp . print_r( $arg, true ). "\n" );
109            }
110        }
111    }
112}