#include "cdebug_logger.h"
#include <string>
bool lp::CDebugLogger::Init(bool bEnabledLog, int nSaveDay, int nSegmentSize)
{
    m_bEnabledLog = bEnabledLog;
    return true;
}

bool lp::CDebugLogger::UnInit()
{
    return true;
}

void lp::CDebugLogger::AddLog(ELogType type, const std::string& info)
{
    std::string sHeader = "I ";
    switch (type) {
    case lp::kDebug:
        sHeader = "D ";
        break;
    case lp::kWarn:
        sHeader = "W ";
        break;
    case lp::kError:
        sHeader = "E ";
        break;
    case lp::kFatal:
        sHeader = "T ";
        break;
    default:
        break;
    }

    sHeader += info;

    qDebug() << QString::fromLocal8Bit(sHeader.c_str());
}

void lp::CDebugLogger::SetLogLevel(ELogType eType)
{
    m_eLogType = eType;
}

void lp::CDebugLogger::AddLog(const std::string& info, const std::string& moduleName)
{
    qDebug() << moduleName.c_str() << " " << info.c_str();
}

void lp::CDebugLogger::AddLog(ILogST& stLog, const std::string& moduleName, const std::string& sFileName, int line, EWriteMode eWritype /*= kAsync*/)
{
    const char* base = strrchr(sFileName.c_str(), '\\');
    if (!base) {
        base = strrchr(sFileName.c_str(), '/');
    }
    
    AddLog(stLog._eType, fmt::format("[{} {}:{}] {}", base ? (base + 1) : sFileName, line, moduleName, stLog.str()));
}

void lp::CDebugLogger::AddLog(ELogType type, const std::string& info, const char* sFile, int iLine)
{
    if (type < m_eLogType) {
        return;
    }
    const char* base = strrchr(sFile, '\\');
    if (!base) {
        base = strrchr(sFile, '/');
    }
    AddLog(type, fmt::format("[{}:{}] {}", base ? (base + 1) : sFile, iLine, info));
}
