#include "error_callback.h"
#include <iostream>
#include <mutex>
#include <string.h>

error_cbk_t g_error_callback = nullptr;

static std::mutex g_error_mutex;

const char *extract_filename_in_cbk(const char *full_path)
{
    if (!full_path)
        return "unknow_file";
#ifdef _WIN32
    const char *pos = strrchr(full_path, '\\');
#endif
#ifdef __linux__
    const char *pos = strrchr(full_path, '/');

#endif
    return (pos) ? (pos + 1) : full_path;
}
void error_callback_call(const char *file_name, const char *func_name, const char *err_msg)
{

    g_error_mutex.lock();
    if (g_error_callback == nullptr)
    {
        const char * pure_file = extract_filename_in_cbk(file_name);
        std::cout  << "[" << func_name << "]==>\t" << err_msg;
        //std::cout << "[默认打印]:" << "FILE:[" << pure_file << "]FUNC:[" << func_name << "]==>\t" << err_msg;
    }
    else
    {
        g_error_callback(file_name, func_name, err_msg);
    }
    g_error_mutex.unlock();
}

void set_error_cbk(error_cbk_t cbk)
{
    g_error_callback = cbk;
}
