3#include <spdlog/sinks/callback_sink.h>
4#include <spdlog/sinks/dist_sink.h>
5#include <spdlog/spdlog.h>
6#include <spdlog/fmt/chrono.h>
7#include <spdlog/fmt/ranges.h>
9#include <hpp/source_location.hpp>
16#define APPLOG_DEBUG(...) SPDLOG_LOGGER_CALL(spdlog::get(APPLOG), spdlog::level::debug, __VA_ARGS__)
17#define APPLOG_TRACE(...) SPDLOG_LOGGER_CALL(spdlog::get(APPLOG), spdlog::level::trace, __VA_ARGS__)
18#define APPLOG_INFO(...) SPDLOG_LOGGER_CALL(spdlog::get(APPLOG), spdlog::level::info, __VA_ARGS__)
19#define APPLOG_WARNING(...) SPDLOG_LOGGER_CALL(spdlog::get(APPLOG), spdlog::level::warn, __VA_ARGS__)
20#define APPLOG_ERROR(...) SPDLOG_LOGGER_CALL(spdlog::get(APPLOG), spdlog::level::err, __VA_ARGS__)
21#define APPLOG_CRITICAL(...) SPDLOG_LOGGER_CALL(spdlog::get(APPLOG), spdlog::level::critical, __VA_ARGS__)
24#ifndef SPDLOG_NO_SOURCE_LOC
25#define SPDLOG_LOGGER_CALL_LOC(logger, file, line, func, level, ...) \
26 (logger)->log(spdlog::source_loc{file, line, func}, level, __VA_ARGS__)
28#define SPDLOG_LOGGER_CALL_LOC(logger, level, ...) (logger)->log(spdlog::source_loc{}, level, __VA_ARGS__)
30#define APPLOG_DEBUG_LOC(FILE_LOC, LINE_LOC, FUNC_LOC, ...) \
31SPDLOG_LOGGER_CALL_LOC(spdlog::get(APPLOG), FILE_LOC, LINE_LOC, FUNC_LOC, spdlog::level::debug, __VA_ARGS__)
32#define APPLOG_TRACE_LOC(FILE_LOC, LINE_LOC, FUNC_LOC, ...) \
33 SPDLOG_LOGGER_CALL_LOC(spdlog::get(APPLOG), FILE_LOC, LINE_LOC, FUNC_LOC, spdlog::level::trace, __VA_ARGS__)
34#define APPLOG_INFO_LOC(FILE_LOC, LINE_LOC, FUNC_LOC, ...) \
35 SPDLOG_LOGGER_CALL_LOC(spdlog::get(APPLOG), FILE_LOC, LINE_LOC, FUNC_LOC, spdlog::level::info, __VA_ARGS__)
36#define APPLOG_WARNING_LOC(FILE_LOC, LINE_LOC, FUNC_LOC, ...) \
37 SPDLOG_LOGGER_CALL_LOC(spdlog::get(APPLOG), FILE_LOC, LINE_LOC, FUNC_LOC, spdlog::level::warn, __VA_ARGS__)
38#define APPLOG_ERROR_LOC(FILE_LOC, LINE_LOC, FUNC_LOC, ...) \
39 SPDLOG_LOGGER_CALL_LOC(spdlog::get(APPLOG), FILE_LOC, LINE_LOC, FUNC_LOC, spdlog::level::err, __VA_ARGS__)
40#define APPLOG_CRITICAL_LOC(FILE_LOC, LINE_LOC, FUNC_LOC, ...) \
41 SPDLOG_LOGGER_CALL_LOC(spdlog::get(APPLOG), FILE_LOC, LINE_LOC, FUNC_LOC, spdlog::level::critical, __VA_ARGS__)
43#define APPLOG_FLUSH() spdlog::apply_all([](std::shared_ptr<spdlog::logger> const& lg){ lg->flush(); })
50 logging(
const std::string& output_file =
"Log.txt");
55template<spdlog::level::level_enum lvl = spdlog::level::debug,
typename T = std::chrono::microseconds>
58 using clock_t = std::chrono::high_resolution_clock;
64 log_duration(
const std::string& func, hpp::source_location loc = hpp::source_location::current())
72 auto end = clock_t::now();
73 auto dur = std::chrono::duration_cast<T>(end -
start);
75 SPDLOG_LOGGER_CALL(spdlog::get(
APPLOG), lvl,
"{} : {}",
func_, dur);
80template<spdlog::level::level_enum lvl = spdlog::level::debug,
typename T = std::chrono::microseconds>
83 using clock_t = std::chrono::high_resolution_clock;
90 log_stopwatch(
const char* func, hpp::source_location loc = hpp::source_location::current())
99 auto end = clock_t::now();
100 auto dur = std::chrono::duration_cast<T>(end -
start);
108#define APPLOG_CONCATENATE_DETAIL(x, y) x##y
109#define APPLOG_CONCATENATE(x, y) APPLOG_CONCATENATE_DETAIL(x, y)
112#define APPLOG_UNIQUE_VAR(prefix) APPLOG_CONCATENATE(prefix, __LINE__)
114#define APPLOG_INFO_PERF(T) log_stopwatch<spdlog::level::info, T> APPLOG_UNIQUE_VAR(_test)(__func__);
115#define APPLOG_TRACE_PERF(T) log_stopwatch<spdlog::level::trace, T> APPLOG_UNIQUE_VAR(_test)(__func__);
116#define APPLOG_DEBUG_PERF(T) log_stopwatch<spdlog::level::debug, T> APPLOG_UNIQUE_VAR(_test)(__func__);
119#define APPLOG_INFO_PERF_NAMED(T, name) log_stopwatch<spdlog::level::info, T> APPLOG_UNIQUE_VAR(_test)(name);
120#define APPLOG_TRACE_PERF_NAMED(T, name) log_stopwatch<spdlog::level::trace, T> APPLOG_UNIQUE_VAR(_test)(name);
121#define APPLOG_DEBUG_PERF_NAMED(T, name) log_stopwatch<spdlog::level::trace, T> APPLOG_UNIQUE_VAR(_test)(name);
124#define APPLOG_INFO_PERF_ALLOC(T) log_duration<spdlog::level::info, T> APPLOG_UNIQUE_VAR(_test)(__func__);
125#define APPLOG_TRACE_PERF_ALLOC(T) log_duration<spdlog::level::trace, T> APPLOG_UNIQUE_VAR(_test)(__func__);
126#define APPLOG_DEBUG_PERF_ALLOC(T) log_duration<spdlog::level::debug, T> APPLOG_UNIQUE_VAR(_test)(__func__);
129#define APPLOG_INFO_PERF_NAMED_ALLOC(T, name) log_duration<spdlog::level::info, T> APPLOG_UNIQUE_VAR(_test)(name);
130#define APPLOG_TRACE_PERF_NAMED_ALLOC(T, name) log_duration<spdlog::level::trace, T> APPLOG_UNIQUE_VAR(_test)(name);
131#define APPLOG_DEBUG_PERF_NAMED_ALLOC(T, name) log_duration<spdlog::level::trace, T> APPLOG_UNIQUE_VAR(_test)(name);
#define SPDLOG_LOGGER_CALL_LOC(logger, file, line, func, level,...)
auto get_mutable_logging_container() -> std::shared_ptr< spdlog::sinks::dist_sink_mt >
std::chrono::high_resolution_clock clock_t
clock_t::time_point timepoint_t
log_duration(const std::string &func, hpp::source_location loc=hpp::source_location::current())
log_stopwatch(const char *func, hpp::source_location loc=hpp::source_location::current())
hpp::source_location location_
std::chrono::high_resolution_clock clock_t
clock_t::time_point timepoint_t
logging(const std::string &output_file="Log.txt")