logging.cpp 942 Bytes
Newer Older
lyz's avatar
lyz committed
1 2 3 4 5 6 7 8 9 10
#include "logging.hpp"

void LogWriter::operator<(const LogStream &stream) {
    std::ostringstream msg;
    msg << stream.sstream_.rdbuf();
    output_log(msg);
}

void LogWriter::output_log(const std::ostringstream &msg) {
    if (log_level_ >= env_log_level)
lxq's avatar
lxq committed
11 12 13
        std::cout << "[" << level2string(log_level_) << "] "
                  << "(" << location_.file_ << ":" << location_.line_ << "L  "
                  << location_.func_ << ")" << msg.str() << std::endl;
lyz's avatar
lyz committed
14 15
}
std::string level2string(LogLevel level) {
lxq's avatar
lxq committed
16 17 18 19 20 21
    switch (level) {
    case DEBUG:
        return "DEBUG";

    case INFO:
        return "INFO";
lyz's avatar
lyz committed
22

lxq's avatar
lxq committed
23 24
    case WARNING:
        return "WARNING";
lyz's avatar
lyz committed
25

lxq's avatar
lxq committed
26 27
    case ERROR:
        return "ERROR";
lyz's avatar
lyz committed
28

lxq's avatar
lxq committed
29 30
    default:
        return "";
lyz's avatar
lyz committed
31 32
    }
}
lxq's avatar
lxq committed
33
std::string get_short_name(const char *file_path) {
lyz's avatar
lyz committed
34 35 36
    std::string short_file_path = file_path;
    int index = short_file_path.find_last_of('/');

lxq's avatar
lxq committed
37
    return short_file_path.substr(index + 1);
lyz's avatar
lyz committed
38
}