logging.cpp 1.02 KB
Newer Older
刘睿博's avatar
刘睿博 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
#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)
        std::cout << "[" << level2string(log_level_) << "] " 
                    << "(" <<  location_.file_ 
                    << ":" << location_.line_ 
                    << "L  "<< location_.func_<<")"
                    << msg.str() << std::endl;

}
std::string level2string(LogLevel level) {
    switch (level)
    {
        case DEBUG:
            return "DEBUG";
            
        case INFO:
            return "INFO";

        case WARNING:
            return "WARNING";

        case ERROR:
            return "ERROR";

        default:
            return "";
    }
}
std::string get_short_name(const char * file_path) {
    std::string short_file_path = file_path;
    int index = short_file_path.find_last_of('/');

    return short_file_path.substr(index+1);
}