Skip to content

File Logging.cpp

File List > api > cppSDK > SDKBase > Logging.cpp

Go to the documentation of this file

#ifdef UNIT_TEST_LOGS
#include "CppUnitTest.h"
#endif

#include "spdlog/spdlog.h"
#include "spdlog/sinks/rotating_file_sink.h"

#include "Logging.hpp"

#include "ManusSDK.h"

namespace ManusSDK
{
    static LoggingCallback_t s_RegisteredLoggingCallback = nullptr;
    static bool s_LogNormally = true;

    void Log::Debug(const char* p_Tag, const char* p_String)
    {
        std::string t_Log = fmt::format("{} - {}", p_Tag, p_String);
        if (s_LogNormally)
        {
#ifdef UNIT_TEST_LOGS
            Microsoft::VisualStudio::CppUnitTestFramework::Logger::WriteMessage(("DEBUG: " + t_Log).c_str());
#else
            spdlog::default_logger_raw()->debug(p_String);
#endif
        }
        if (s_RegisteredLoggingCallback != nullptr)
        {
            s_RegisteredLoggingCallback(LogSeverity_Debug, t_Log.c_str(), (uint32_t)t_Log.length());
        }
    }

    void Log::Info(const char* p_Tag, const char* p_String)
    {
        std::string t_Log = fmt::format("{} - {}", p_Tag, p_String);
        if (s_LogNormally)
        {
#ifdef UNIT_TEST_LOGS
            Microsoft::VisualStudio::CppUnitTestFramework::Logger::WriteMessage(("INFO: " + t_Log).c_str());
#else
            spdlog::default_logger_raw()->info(p_String);
#endif
        }
        if (s_RegisteredLoggingCallback != nullptr)
        {
            s_RegisteredLoggingCallback(LogSeverity_Info, t_Log.c_str(), (uint32_t)t_Log.length());
        }
    }

    void Log::Warn(const char* p_Tag, const char* p_String)
    {
        std::string t_Log = fmt::format("{} - {}", p_Tag, p_String);
        if (s_LogNormally)
        {
#ifdef UNIT_TEST_LOGS
            Microsoft::VisualStudio::CppUnitTestFramework::Logger::WriteMessage(("WARN: " + t_Log).c_str());
#else
            spdlog::default_logger_raw()->warn(p_String);
#endif
        }
        if (s_RegisteredLoggingCallback != nullptr)
        {
            s_RegisteredLoggingCallback(LogSeverity_Warn, t_Log.c_str(), (uint32_t)t_Log.length());
        }
    }

    void Log::Error(const char* p_Tag, const char* p_String)
    {
        std::string t_Log = fmt::format("{} - {}", p_Tag, p_String);
        if (s_LogNormally)
        {
#ifdef UNIT_TEST_LOGS
            Microsoft::VisualStudio::CppUnitTestFramework::Logger::WriteMessage(("ERROR: " + t_Log).c_str());
#else
            spdlog::default_logger_raw()->error(p_String);
#endif
        }
        if (s_RegisteredLoggingCallback != nullptr)
        {
            s_RegisteredLoggingCallback(LogSeverity_Error, t_Log.c_str(), (uint32_t)t_Log.length());
        }
    }

    void Log::RegisterCallback(void* p_Function)
    {
        s_RegisteredLoggingCallback = (LoggingCallback_t)p_Function;
        s_LogNormally = s_RegisteredLoggingCallback == nullptr;
    }
}