oh_my_loam/common/log/log.cc

77 lines
2.2 KiB
C++
Raw Normal View History

2020-10-16 18:08:31 +08:00
#include "log.h"
2021-01-04 21:26:09 +08:00
#include <filesystem>
namespace common {
2020-10-16 18:08:31 +08:00
2021-01-22 16:33:55 +08:00
void InitG3Logging(bool log_to_file, const std::string &prefix,
const std::string &path) {
2021-02-02 21:06:45 +08:00
static std::unique_ptr<g3::LogWorker> worker(
g3::LogWorker::createLogWorker());
2020-10-16 18:08:31 +08:00
worker->addSink(std::make_unique<g3::CustomSink>(),
&g3::CustomSink::StdLogMessage);
if (log_to_file) {
2020-12-31 20:59:08 +08:00
std::filesystem::create_directories(path);
2020-10-16 18:08:31 +08:00
std::ostringstream oss;
oss << path;
if (*path.rbegin() != '/') oss << '/';
if (prefix.empty()) oss << "g3log";
oss << prefix << ".";
auto now = std::chrono::system_clock::now();
oss << g3::localtime_formatted(now, "%Y%m%d-%H%M%S");
oss << ".log";
worker->addSink(std::make_unique<g3::CustomSink>(oss.str()),
&g3::CustomSink::FileLogMessage);
}
g3::initializeLogging(worker.get());
}
2021-01-04 21:26:09 +08:00
2021-02-02 21:06:45 +08:00
} // namespace common
namespace g3 {
CustomSink::~CustomSink() {
std::ostringstream oss;
oss << "\ng3log " << (log_to_file_ ? "FileSink" : "StdSink")
<< " shutdown at: ";
auto now = std::chrono::system_clock::now();
oss << g3::localtime_formatted(now, "%Y%m%d %H:%M:%S.%f3");
if (log_to_file_) {
(*ofs_) << oss.str() << std::endl;
} else {
std::clog << oss.str() << std::endl;
}
};
std::string CustomSink::FormatedMessage(const g3::LogMessage &msg) const {
std::ostringstream oss;
oss << "[" << msg.level()[0] << msg.timestamp("%Y%m%d %H:%M:%S.%f3") << " "
<< msg.file() << ":" << msg.line() << "] " << msg.message();
return oss.str();
}
std::string CustomSink::ColorFormatedMessage(const g3::LogMessage &msg) const {
std::ostringstream oss;
oss << GetColorCode(msg._level) << FormatedMessage(msg) << "\033[m";
return oss.str();
}
std::string CustomSink::GetColorCode(const LEVELS &level) const {
if (level.value == WARNING.value) {
return "\033[33m"; // yellow
}
if (level.value == DEBUG.value) {
return "\033[32m"; // green
}
if (level.value == ERROR.value) {
return "\033[31m"; // red
}
if (level.value == USER.value) {
return "\033[1m\033[34m"; // bold blue
}
if (g3::internal::wasFatal(level)) {
return "\033[1m\033[31m"; // red
}
return "\033[97m"; // white
}
} // namespace g3