[easylog]Support multiple instance (#178)
This commit is contained in:
parent
89b535384f
commit
5e31529507
|
@ -17,10 +17,12 @@
|
||||||
#include "appender.hpp"
|
#include "appender.hpp"
|
||||||
|
|
||||||
namespace easylog {
|
namespace easylog {
|
||||||
|
|
||||||
|
template <size_t Id = 0>
|
||||||
class logger {
|
class logger {
|
||||||
public:
|
public:
|
||||||
static logger &instance() {
|
static logger<Id> &instance() {
|
||||||
static logger instance;
|
static logger<Id> instance;
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -110,44 +112,52 @@ class logger {
|
||||||
appender *appender_ = nullptr;
|
appender *appender_ = nullptr;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <size_t Id = 0>
|
||||||
inline void init_log(Severity min_severity, const std::string &filename = "",
|
inline void init_log(Severity min_severity, const std::string &filename = "",
|
||||||
bool enable_console = true, size_t max_file_size = 0,
|
bool enable_console = true, size_t max_file_size = 0,
|
||||||
size_t max_files = 0, bool flush_every_time = false) {
|
size_t max_files = 0, bool flush_every_time = false) {
|
||||||
logger::instance().init(min_severity, enable_console, filename, max_file_size,
|
logger<Id>::instance().init(min_severity, enable_console, filename,
|
||||||
max_files, flush_every_time);
|
max_file_size, max_files, flush_every_time);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void flush() { logger::instance().flush(); }
|
template <size_t Id = 0>
|
||||||
|
inline void flush() {
|
||||||
|
logger<Id>::instance().flush();
|
||||||
|
}
|
||||||
} // namespace easylog
|
} // namespace easylog
|
||||||
|
|
||||||
#define ELOG_IMPL(severity) \
|
#define ELOG_IMPL(severity, Id, ...) \
|
||||||
if (!easylog::logger::instance().check_severity(severity)) { \
|
if (!easylog::logger<Id>::instance().check_severity(severity)) { \
|
||||||
; \
|
; \
|
||||||
} \
|
} \
|
||||||
else \
|
else \
|
||||||
easylog::logger::instance() += \
|
easylog::logger<Id>::instance() += \
|
||||||
easylog::record_t(std::chrono::system_clock::now(), severity, \
|
easylog::record_t(std::chrono::system_clock::now(), severity, \
|
||||||
GET_STRING(__FILE__, __LINE__)) \
|
GET_STRING(__FILE__, __LINE__)) \
|
||||||
.ref()
|
.ref()
|
||||||
|
|
||||||
#define ELOG(severity) ELOG_IMPL(Severity::severity)
|
#define ELOG(severity, ...) ELOG_IMPL(Severity::severity, __VA_ARGS__, 0)
|
||||||
|
|
||||||
#define ELOGV_IMPL(severity, fmt, ...) \
|
#define ELOGV_IMPL(severity, Id, fmt, ...) \
|
||||||
if (!easylog::logger::instance().check_severity(severity)) { \
|
if (!easylog::logger<Id>::instance().check_severity(severity)) { \
|
||||||
; \
|
; \
|
||||||
} \
|
} \
|
||||||
else { \
|
else { \
|
||||||
easylog::logger::instance() += \
|
easylog::logger<Id>::instance() += \
|
||||||
easylog::record_t(std::chrono::system_clock::now(), severity, \
|
easylog::record_t(std::chrono::system_clock::now(), severity, \
|
||||||
GET_STRING(__FILE__, __LINE__)) \
|
GET_STRING(__FILE__, __LINE__)) \
|
||||||
.sprintf(fmt, __VA_ARGS__); \
|
.sprintf(fmt, __VA_ARGS__); \
|
||||||
if (severity == Severity::CRITICAL) { \
|
if (severity == Severity::CRITICAL) { \
|
||||||
easylog::flush(); \
|
easylog::flush<Id>(); \
|
||||||
std::exit(EXIT_FAILURE); \
|
std::exit(EXIT_FAILURE); \
|
||||||
} \
|
} \
|
||||||
}
|
}
|
||||||
|
|
||||||
#define ELOGV(severity, ...) ELOGV_IMPL(Severity::severity, __VA_ARGS__, "\n")
|
#define ELOGV(severity, ...) \
|
||||||
|
ELOGV_IMPL(Severity::severity, 0, __VA_ARGS__, "\n")
|
||||||
|
|
||||||
|
#define MELOGV(severity, Id, ...) \
|
||||||
|
ELOGV_IMPL(Severity::severity, Id, __VA_ARGS__, "\n")
|
||||||
|
|
||||||
#define ELOG_TRACE ELOG(INFO)
|
#define ELOG_TRACE ELOG(INFO)
|
||||||
#define ELOG_DEBUG ELOG(DEBUG)
|
#define ELOG_DEBUG ELOG(DEBUG)
|
||||||
|
@ -156,9 +166,16 @@ inline void flush() { logger::instance().flush(); }
|
||||||
#define ELOG_ERROR ELOG(ERROR)
|
#define ELOG_ERROR ELOG(ERROR)
|
||||||
#define ELOG_CRITICAL ELOG(CRITICAL)
|
#define ELOG_CRITICAL ELOG(CRITICAL)
|
||||||
|
|
||||||
|
#define MELOG_TRACE(id) ELOG(INFO, id)
|
||||||
|
#define MELOG_DEBUG(id) ELOG(DEBUG, id)
|
||||||
|
#define MELOG_INFO(id) ELOG(INFO, id)
|
||||||
|
#define MELOG_WARN(id) ELOG(WARN, id)
|
||||||
|
#define MELOG_ERROR(id) ELOG(ERROR, id)
|
||||||
|
#define MELOG_CRITICAL(id) ELOG(CRITICAL, id)
|
||||||
|
|
||||||
#define ELOGT ELOG_TRACE
|
#define ELOGT ELOG_TRACE
|
||||||
#define ELOGD ELOG_DEBUG
|
#define ELOGD ELOG_DEBUG
|
||||||
#define ELOGI ELOG_INFO
|
#define ELOGI ELOG_INFO
|
||||||
#define ELOGW ELOG_WARN
|
#define ELOGW ELOG_WARN
|
||||||
#define ELOGE ELOG_ERROR
|
#define ELOGE ELOG_ERROR
|
||||||
#define ELOGC ELOG_CRITICAL
|
#define ELOGC ELOG_CRITICAL
|
||||||
|
|
|
@ -61,4 +61,23 @@ TEST_CASE("test basic") {
|
||||||
ELOG_DEBUG << "debug log";
|
ELOG_DEBUG << "debug log";
|
||||||
ELOGD << "debug log";
|
ELOGD << "debug log";
|
||||||
CHECK(get_last_line(filename).rfind("debug log") != std::string::npos);
|
CHECK(get_last_line(filename).rfind("debug log") != std::string::npos);
|
||||||
|
|
||||||
|
// test multiple instance
|
||||||
|
std::string other_filename = "other.txt";
|
||||||
|
std::filesystem::remove(other_filename);
|
||||||
|
constexpr size_t InstanceId = 2;
|
||||||
|
easylog::init_log<InstanceId>(Severity::DEBUG, other_filename, false);
|
||||||
|
ELOG(INFO, InstanceId) << "ok in other txt";
|
||||||
|
easylog::flush<InstanceId>();
|
||||||
|
CHECK(get_last_line(other_filename).rfind("ok in other txt") !=
|
||||||
|
std::string::npos);
|
||||||
|
MELOG_INFO(InstanceId) << "test in other";
|
||||||
|
easylog::flush<InstanceId>();
|
||||||
|
CHECK(get_last_line(other_filename).rfind("test in other") !=
|
||||||
|
std::string::npos);
|
||||||
|
|
||||||
|
MELOGV(INFO, InstanceId, "it is a test %d", 42);
|
||||||
|
easylog::flush<InstanceId>();
|
||||||
|
CHECK(get_last_line(other_filename).rfind("it is a test 42") !=
|
||||||
|
std::string::npos);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue