dimanche 25 août 2019

Boost Log severity level output in upper case

I try to setup a logger with Boost Log V2. The output should look like (file blubb.log):

2019-08-24 23:24:08 - ERROR: Hi from source code.

I'm currently struggling with the uppercase of the severity level. The rest works so far.

severity level provides a ::to_string() conversation, but I didn't get it working in severity_type. So I tried to put the data into a stringstream.

logger.cpp (without includes)

namespace logging = boost::log;
namespace src = boost::log::sources;
namespace expr = boost::log::expressions;
namespace sinks = boost::log::sinks;
namespace keywords = boost::log::keywords;

BOOST_LOG_ATTRIBUTE_KEYWORD(att_channel, "Channel", std::string);

typedef src::severity_channel_logger<logging::trivial::severity_level, std::string> severity_channel_logger_t;
typedef sinks::synchronous_sink< sinks::text_ostream_backend > synchronous_sink_t;

static std::map<std::string, severity_channel_logger_t> map_logger;
static std::unordered_set<std::string> profiles = { "blubb",  "configreader", "connector", "downloader", "mlog", "output", "provider", "rest" };

static std::unordered_map<std::string, logging::trivial::severity_level> levels = {
    { "trace"   , logging::trivial::trace},
    { "debug"   , logging::trivial::debug},
    { "info"    , logging::trivial::info},
    { "warning" , logging::trivial::warning},
    { "error"   , logging::trivial::error},
    { "fatal"   , logging::trivial::fatal}
};

Logger::Logger()
{
    for (std::unordered_set<std::string>::iterator it = profiles.begin(); it != profiles.end(); it++)
    {
        map_logger[*it] = severity_channel_logger_t(keywords::channel = *it);
        create_channel_logger(*it);
    }
    logging::add_common_attributes();
}

std::stringstream Logger::sev_toupper(logging::trivial::severity_type severity)
{
    std::stringstream in;
    std::stringstream out;
    char c;
    in << severity;
    while (in >> c)
    {
        out << toupper(c);
    }
    return out;
}

void Logger::logmsg(std::string channel, const std::string level, const std::string message)
{
    // fallback channel, if not in list
    if (profiles.find(channel) == profiles.end())
        channel = "rest";
    BOOST_LOG_SEV(map_logger[channel], levels[level]) << message;
}

void Logger::create_channel_logger(std::string channel)
{
    logging::add_file_log
    (
        keywords::file_name = channel+".log",
        keywords::open_mode = std::ios_base::app,           
        keywords::filter = att_channel == channel,
        keywords::auto_flush = true,                        
        keywords::format =
        (
            expr::stream
                << expr::format_date_time<boost::posix_time::ptime>("TimeStamp", "%Y-%m-%d %H:%M:%S")
                << " - "
                << std::right << std::setw(7) << sev_toupper(logging::trivial::severity).str().c_str()
                << std::left << ": " << expr::smessage
        )
    );
}

main.cpp

int main(int, char*[])
{
    Logger * lg = new Logger();
    lg->logmsg("blubb", "error", "Hi, from Source Code!");
    delete lg;
}

My function sev_toupper() produces gargabe. expr::stream seems to handle logging::trivial::severity in a different way than std::stringstream.

Aucun commentaire:

Enregistrer un commentaire