src/c++/lib/common/Error.cpp
/**
 * Initialize a logger.
 * @param name   logger name
 * @param async  log asynchronously (use true when there are many messages)
 * @param level  minimum level to log at
 */
Initialize a logger.
@param name   logger name
@param async  log asynchronously (use true when there are many messages)
@param level  minimum level to log at
Initialize a logger . @param name logger name @param async log asynchronously ( use true when there are many messages ) @param level minimum level to log at
void initLogging(const char* name, const char* filename, bool async, const char* level)
{
    if (async)
    {
        auto flush_interval = std::chrono::seconds(10);
        spdlog::set_async_mode(8192, spdlog::async_overflow_policy::block_retry, nullptr, flush_interval);
    }
    else
    {
        spdlog::set_sync_mode();
    }
    if (std::string(level) == "trace")
    {
        spdlog::set_level(spdlog::level::trace);
    }
    else if (std::string(level) == "debug")
    {
        spdlog::set_level(spdlog::level::debug);
    }
    else if (std::string(level) == "info")
    {
        spdlog::set_level(spdlog::level::info);
    }
    else if (std::string(level) == "warning")
    {
        spdlog::set_level(spdlog::level::warn);
    }
    else if (std::string(level) == "error")
    {
        spdlog::set_level(spdlog::level::err);
    }
    else
    {
        error("Unknown log level: %s", level);
    }
    std::shared_ptr<spdlog::logger> logger;
    try
    {
        if (filename == nullptr || strlen(filename) == 0)
        {
            logger = spdlog::stderr_color_mt(name);
        }
        else
        {
            logger = spdlog::basic_logger_mt(name, filename);
        }
    }
    catch (const spdlog::spdlog_ex& ex)
    {
        error("Log initialization failed: %s", ex.what());
    }
    DEFAULT_LOGGER_NAME = name;
    spdlog::set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%n] [%t] [%l] %v");
    std::atexit(spdlog::drop_all);
}
void initLogging ( const char * name , const char * filename , bool async , const char * level ) { if ( async ) { auto flush_interval = std :: chrono :: seconds ( 10 ) ; spdlog :: set_async_mode ( 8192 , spdlog :: async_overflow_policy :: block_retry , nullptr , flush_interval ) ; } else { spdlog :: set_sync_mode ( ) ; } if ( std :: string ( level ) == " " ) { spdlog :: set_level ( spdlog :: level :: trace ) ; } else if ( std :: string ( level ) == " " ) { spdlog :: set_level ( spdlog :: level :: debug ) ; } else if ( std :: string ( level ) == " " ) { spdlog :: set_level ( spdlog :: level :: info ) ; } else if ( std :: string ( level ) == " " ) { spdlog :: set_level ( spdlog :: level :: warn ) ; } else if ( std :: string ( level ) == " " ) { spdlog :: set_level ( spdlog :: level :: err ) ; } else { error ( " " , level ) ; } std :: shared_ptr < spdlog :: logger > logger ; try { if ( filename == nullptr || strlen ( filename ) == 0 ) { logger = spdlog :: stderr_color_mt ( name ) ; } else { logger = spdlog :: basic_logger_mt ( name , filename ) ; } } catch ( const spdlog :: spdlog_ex & ex ) { error ( " " , ex . what ( ) ) ; } DEFAULT_LOGGER_NAME = name ; spdlog :: set_pattern ( " " ) ; std :: atexit ( spdlog :: drop_all ) ; }
Initialize a logger.
Initialize a logger .
// LOG creates + registers the logger, volatile so // it doesn't get optimized out // necessary for MSVC, also seems to avoid a memory leak