<?php

namespace Sibs;

class SibsLog
{
    private static $INFO     = 1;

    private static $WARNING  = 2;

    private static $ERROR    = 3;

    private static $CRITICAL = 4;

    private static $BACKTRACE_OPTIONS         = (! DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS);

    private static $BACKTRACE_DEPTH           = 3;

    private static $BACKTRACE_CALLED_FUNCTION = 2;

    private static function called_function()
    {
        $backtrace = debug_backtrace(self::$BACKTRACE_OPTIONS, self::$BACKTRACE_DEPTH)[self::$BACKTRACE_CALLED_FUNCTION];

        //$file = $backtrace['file'];
        $line     = $backtrace['line'];
        $function = $backtrace['function'];

        return "[$function:$line]";
    }

    private static function log($message, $severity = 1, $error_code = null, $called_function = null)
    {
        $message = empty($called_function) ? $message : "$called_function - $message";

        $object_type     = 'Sibs';
        $object_id       = null;
        $allow_duplicate = true;
        $id_employee     = null;

        \PrestaShopLogger::addLog(
            $message,
            $severity,
            $error_code,
            $object_type,
            $object_id,
            $allow_duplicate,
            $id_employee
        );
    }

    public static function info($message)
    {
        $called_function = self::called_function();
        self::log($message, self::$INFO, null, $called_function);
    }

    public static function warning($message)
    {
        $called_function = self::called_function();
        self::log($message, self::$WARNING, null, $called_function);
    }

    public static function error($message, $error_code = 400)
    {
        $called_function = self::called_function();
        self::log($message, self::$ERROR, $error_code, $called_function);
    }

    public static function critical($message, $error_code = 500)
    {
        $called_function = self::called_function();
        self::log($message, self::$CRITICAL, $error_code, $called_function);
    }
}
