/*
 * @Author: yushuoqi yushuoqi@kylinos.cn
 * @Date: 2024-03-27 17:57:36
 * @LastEditors: yushuoqi yushuoqi@kylinos.cn
 * @LastEditTime: 2024-04-18 10:14:33
 * @FilePath: /kaiming/common/kmlogger.cpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
/* kmlogger.cpp
 *
 * Copyright (C) 2023 OpenKylin, Inc
 */

#include "KMConfig.h"

#include <spdlog/sinks/rotating_file_sink.h>

#include <iostream>

#include "KMLogger.h"

static bool isSudoOrRoot()
{
    uid_t euid = geteuid();
    if (euid == 0)
    {
        return true;
    }

    const char *user = std::getenv("USER");
    if (user != nullptr && std::string(user) == "root")
    {
        return true;
    }
    return false;
}

/**
 * @brief 根据不同权限将日志输出到不同目录
 *
 * sudo, root 权限（安装、升级需要）日志路径： /var/log/kaiming.log
 * 普通用户可获取 $HOME 环境变量： $HOME/.log/kaiming.log
 * 普通用户不可获取 $HOME 环境变量： /tmp/kaiming.log
 *
 * @return string
 */
static std::string getLogPath()
{
    if (isSudoOrRoot())
    {
        return std::string("/var/log/kaiming.log");
    }

    const char *homeDir = std::getenv("HOME");

    if (homeDir == nullptr || *homeDir == '0')
    {
        return std::string("/tmp/kaiming.log");
    }

    return std::string(homeDir).append("/.log/kaiming.log");
}

KMLogger::KMLogger(token)
{
    m_logger = spdlog::rotating_logger_mt("file_logger", getLogPath(), LOG_FILE_MAX_SIZE, LOG_FILE_MAX_COUNT);
    setLogLevel(spdlog::level::debug);
}

KMLogger::~KMLogger() { }

void KMLogger::setLogLevel(spdlog::level::level_enum level)
{
    m_logLevel = level;
    m_logger->set_level(m_logLevel);
}

void KMLogger::setLogLevel(const std::string &level)
{
    setLogLevel(spdlog::level::from_str(level));
}

std::string KMLogger::format2String(const char *fmt, ...)
{
    char tmp[1024];
    memset(tmp, 0x00, 1024);
    va_list arg_list;
    va_start(arg_list, fmt);
    // sprintf(tmp, fmt, arg_list);
    vsprintf(tmp, fmt, arg_list);
    va_end(arg_list);
    std::cout << "llllog : " << tmp << std::endl;
    return std::string(tmp);
}

void KMLogger::traceLog(const std::string &msg)
{
    if (m_logLevel > spdlog::level::trace)
    {
        return;
    }

    std::cout << "[trace]: " << msg << std::endl;
    m_logger->trace(msg);
}

void KMLogger::debugLog(const std::string &msg)
{
    if (m_logLevel > spdlog::level::debug)
    {
        return;
    }

    std::cout << "[debug]: " << msg << std::endl;
    m_logger->debug(msg);
}

void KMLogger::infoLog(const std::string &msg)
{
    if (m_logLevel > spdlog::level::info)
    {
        return;
    }

    std::cout << "[info]: " << msg << std::endl;
    m_logger->info(msg);
}

void KMLogger::warnLog(const std::string &msg)
{
    if (m_logLevel > spdlog::level::warn)
    {
        return;
    }

    std::cout << COLOR_YELLOW << "[warn]: " << COLOR_RESET << msg << std::endl;
    m_logger->warn(msg);
}

void KMLogger::errorLog(const std::string &msg)
{
    if (m_logLevel > spdlog::level::err)
    {
        return;
    }

    std::cerr << COLOR_RED << "[error]: " << COLOR_RESET << msg << std::endl;
    m_logger->error(msg);
}

void KMLogger::criticalLog(const std::string &msg)
{
    if (m_logLevel > spdlog::level::critical)
    {
        return;
    }

    std::cerr << COLOR_RED << "[critical]: " << COLOR_RESET << msg << std::endl;
    m_logger->critical(msg);
}
