File size: 1,705 Bytes
07c3cdd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<?php
namespace Maveriks\Util;

/**
 * Singleton Class Logger
 *
 * This Utility is useful to log local messages
 * @package ProcessMaker\Util
 * @author Erik Amaru Ortiz <aortiz.erik@gmail.com, erik@colosa.com>
 */
class Logger
{
    private static $instance;
    private $logFile;
    private $fp;

    protected function __construct()
    {
        $this->logFile = rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'processmaker.log';
        if (! file_exists($this->logFile)) {
            if (! touch($this->logFile)) {
                error_log("ProcessMaker Log file can't be created!");
            }
            chmod($this->logFile, 0777);
        }

        $this->fp = fopen($this->logFile, "a+");
    }

    public static function getInstance()
    {
        if (is_null(self::$instance)) {
            self::$instance = new Logger();
        }

        return self::$instance;
    }

    public function setLogLine()
    {
        $args = func_get_args();

        $this->setLog(date('Y-m-d H:i:s') . " ");

        foreach ($args as $str) {
            $this->setLog((is_string($str) ? $str : var_export($str, true)) . PHP_EOL);
        }
    }

    public function setLogInline()
    {
        $args = func_get_args();
        $this->setLog(date('Y-m-d H:i:s') . " ");

        foreach ($args as $str) {
            $this->setLog((is_string($str) ? $str : var_export($str, true)) . " ");
        }
    }

    public function setLog($str)
    {
        fwrite($this->fp, $str);
    }

    public static function log()
    {
        $me = Logger::getInstance();
        $args = func_get_args();

        call_user_func_array(array($me, 'setLogLine'), $args);
    }
}