<?php

namespace Api\PhpUtils\Log;

/**
 * @method JobLog info($signature, $detail_info = '', $with_access_log = false)
 * @method JobLog waring($signature, $detail_info = '', $exception = null)
 * @method JobLog error($signature, $detail_info = '', $exception = null, $mail_to = '')
 */
class JobLog
{
    protected static $instance = null;
    public $log = null;

    protected function __construct()
    {
    }

    protected function __clone()
    {
    }

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

    public function __call($name, $arguments)
    {
        $this->setLog($arguments);
        if (in_array($name, get_class_methods(FileLog::class))) {
            call_user_func_array([FileLog::class, $name], $arguments);
        }
        return $this;
    }

    private function setLog($arguments)
    {
        if (!empty($arguments[0]) && !empty($arguments[1])) {
            $this->log = date('Y-m-d H:i:s') . ";$arguments[0]:$arguments[1]" . PHP_EOL;
        }
    }

    public function output()
    {
        echo $this->log;
    }
}
