oh_my_loam/common/log/log.h

98 lines
2.9 KiB
C
Raw Normal View History

2020-10-13 21:31:01 +08:00
#pragma once
#include <chrono>
2020-12-31 20:59:08 +08:00
#include <filesystem>
2020-10-16 18:08:31 +08:00
#include <fstream>
2020-10-13 21:31:01 +08:00
#include <g3log/g3log.hpp>
#include <g3log/logworker.hpp>
2020-10-16 18:08:31 +08:00
#include <iostream>
2020-11-03 15:10:16 +08:00
// here we define G3LOG instead of use LOG directly, since LOG macro in g3log
2021-02-02 21:06:45 +08:00
// conflicts LOG macro in glog.
#define G3LOG(level) \
if (g3::logLevel(level)) INTERNAL_LOG_MESSAGE(level).stream()
#define VARIABLE_WITH_LINE_NUM(var_name, line) var_name##line
#define COUNT_VAR_WITH_LINE_NUM VARIABLE_WITH_LINE_NUM(_count_var_, __LINE__)
2020-11-02 17:16:42 +08:00
2021-02-02 21:06:45 +08:00
#define G3LOG_EVERY(level, n) \
static size_t COUNT_VAR_WITH_LINE_NUM = 0; \
if ((COUNT_VAR_WITH_LINE_NUM)++ % static_cast<size_t>(n) == 0 && \
g3::logLevel(level)) \
INTERNAL_LOG_MESSAGE(level).stream();
#define G3LOG_IF(level, boolean_expression) \
if ((boolean_expression) && g3::logLevel(level)) \
2020-11-02 17:16:42 +08:00
INTERNAL_LOG_MESSAGE(level).stream()
2021-02-02 21:06:45 +08:00
#define G3CHECK(boolean_expression) \
if (!(boolean_expression)) \
2020-11-02 17:16:42 +08:00
INTERNAL_CONTRACT_MESSAGE(#boolean_expression).stream()
2020-10-16 18:08:31 +08:00
const LEVELS ERROR{WARNING.value + 100, "ERROR"};
2020-10-18 01:14:43 +08:00
const LEVELS USER(ERROR.value + 100, "USER");
2020-10-13 21:31:01 +08:00
2021-02-02 21:06:45 +08:00
// LOG
2020-11-02 17:16:42 +08:00
#define ADEBUG G3LOG(DEBUG)
#define AINFO G3LOG(INFO)
#define AWARN G3LOG(WARNING)
#define AERROR G3LOG(ERROR)
#define AUSER G3LOG(USER)
#define AFATAL G3LOG(FATAL)
2020-10-13 21:31:01 +08:00
// LOG_IF
2020-11-02 17:16:42 +08:00
#define ADEBUG_IF(cond) G3LOG_IF(DEBUG, cond)
#define AINFO_IF(cond) G3LOG_IF(INFO, cond)
#define AWARN_IF(cond) G3LOG_IF(WARNING, cond)
#define AERROR_IF(cond) G3LOG_IF(ERROR, cond)
#define AUSER_IF(cond) G3LOG_IF(USER, cond)
#define AFATAL_IF(cond) G3LOG_IF(FATAL, cond)
#define ACHECK(cond) G3CHECK(cond)
2020-10-13 21:31:01 +08:00
2021-02-02 21:06:45 +08:00
// LOG_EVERY
#define ADEBUG_EVERY(n) G3LOG_EVERY(DEBUG, n)
#define AINFO_EVERY(n) G3LOG_EVERY(INFO, n)
#define AWARN_EVERY(n) G3LOG_EVERY(WARNING, n)
#define AERROR_EVERY(n) G3LOG_EVERY(ERROR, n)
#define AUSER_EVERY(n) G3LOG_EVERY(USER, n)
2021-01-04 21:26:09 +08:00
namespace common {
2021-01-22 16:33:55 +08:00
void InitG3Logging(bool log_to_file = false, const std::string &prefix = "",
const std::string &path = "./");
2021-01-04 21:26:09 +08:00
} // namespace common
2020-10-13 21:31:01 +08:00
namespace g3 {
class CustomSink {
public:
CustomSink() = default;
2020-10-16 18:08:31 +08:00
2021-01-22 16:33:55 +08:00
explicit CustomSink(const std::string &log_file_name)
2020-10-15 21:14:01 +08:00
: log_file_name_(log_file_name), log_to_file_(true) {
ofs_.reset(new std::ofstream(log_file_name));
}
2020-10-16 18:08:31 +08:00
2021-02-02 21:06:45 +08:00
~CustomSink();
2020-10-13 21:31:01 +08:00
2021-02-02 21:06:45 +08:00
void StdLogMessage(g3::LogMessageMover log_entry) {
std::clog << ColorFormatedMessage(log_entry.get()) << std::endl;
2020-10-13 21:31:01 +08:00
}
2021-02-02 21:06:45 +08:00
void FileLogMessage(g3::LogMessageMover log_entry) {
2020-10-18 01:14:43 +08:00
if (log_to_file_) {
2021-02-02 21:06:45 +08:00
(*ofs_) << FormatedMessage(log_entry.get()) << std::endl;
2020-10-15 21:14:01 +08:00
}
2020-10-13 21:31:01 +08:00
}
private:
std::string log_file_name_;
2020-10-15 21:14:01 +08:00
bool log_to_file_{false};
std::unique_ptr<std::ofstream> ofs_{nullptr};
2020-10-13 21:31:01 +08:00
2021-02-02 21:06:45 +08:00
std::string FormatedMessage(const g3::LogMessage &msg) const;
2020-10-13 21:31:01 +08:00
2021-02-02 21:06:45 +08:00
std::string ColorFormatedMessage(const g3::LogMessage &msg) const;
2020-10-13 21:31:01 +08:00
2021-02-02 21:06:45 +08:00
std::string GetColorCode(const LEVELS &level) const;
2020-10-13 21:31:01 +08:00
};
} // namespace g3