Unravel Engine C++ Reference
Loading...
Searching...
No Matches
logging.h
Go to the documentation of this file.
1#pragma once
2
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>
8
9#include <hpp/source_location.hpp>
10namespace unravel
11{
12using namespace spdlog;
13
14
15#define APPLOG "Log"
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__)
22
23
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__)
27#else
28#define SPDLOG_LOGGER_CALL_LOC(logger, level, ...) (logger)->log(spdlog::source_loc{}, level, __VA_ARGS__)
29#endif
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__)
42
43#define APPLOG_FLUSH() spdlog::apply_all([](std::shared_ptr<spdlog::logger> const& lg){ lg->flush(); })
44
45
46auto get_mutable_logging_container() -> std::shared_ptr<spdlog::sinks::dist_sink_mt>;
47
48struct logging
49{
50 logging(const std::string& output_file = "Log.txt");
51 ~logging();
52};
53
54
55template<spdlog::level::level_enum lvl = spdlog::level::debug, typename T = std::chrono::microseconds>
57{
58 using clock_t = std::chrono::high_resolution_clock;
59 using timepoint_t = clock_t::time_point;
60
61 timepoint_t start = clock_t::now();
62 const char* func_{};
63 hpp::source_location location_;
64
65 log_stopwatch(const char* func, hpp::source_location loc = hpp::source_location::current())
66 : func_(func)
67 , location_(loc)
68 {
69
70 }
71
73 {
74 auto end = clock_t::now();
75 auto dur = std::chrono::duration_cast<T>(end - start);
76
77 SPDLOG_LOGGER_CALL_LOC(spdlog::get(APPLOG), location_.file_name(), int(location_.line()), func_, lvl, "{} : {}", func_, dur);
78 }
79
80};
81
82// Helper macros to concatenate tokens
83#define APPLOG_CONCATENATE_DETAIL(x, y) x##y
84#define APPLOG_CONCATENATE(x, y) APPLOG_CONCATENATE_DETAIL(x, y)
85
86// Macro to create a unique variable name
87#define APPLOG_UNIQUE_VAR(prefix) APPLOG_CONCATENATE(prefix, __LINE__)
88
89#define APPLOG_INFO_PERF(T) log_stopwatch<spdlog::level::info, T> APPLOG_UNIQUE_VAR(_test)(__func__);
90#define APPLOG_TRACE_PERF(T) log_stopwatch<spdlog::level::trace, T> APPLOG_UNIQUE_VAR(_test)(__func__);
91#define APPLOG_DEBUG_PERF(T) log_stopwatch<spdlog::level::debug, T> APPLOG_UNIQUE_VAR(_test)(__func__);
92
93
94#define APPLOG_INFO_PERF_NAMED(T, name) log_stopwatch<spdlog::level::info, T> APPLOG_UNIQUE_VAR(_test)(name);
95#define APPLOG_TRACE_PERF_NAMED(T, name) log_stopwatch<spdlog::level::trace, T> APPLOG_UNIQUE_VAR(_test)(name);
96#define APPLOG_DEBUG_PERF_NAMED(T, name) log_stopwatch<spdlog::level::trace, T> APPLOG_UNIQUE_VAR(_test)(name);
97
98
99} // namespace unravel
#define SPDLOG_LOGGER_CALL_LOC(logger, file, line, func, level,...)
Definition logging.h:25
#define APPLOG
Definition logging.h:15
auto get_mutable_logging_container() -> std::shared_ptr< spdlog::sinks::dist_sink_mt >
Definition logging.cpp:31
log_stopwatch(const char *func, hpp::source_location loc=hpp::source_location::current())
Definition logging.h:65
timepoint_t start
Definition logging.h:61
hpp::source_location location_
Definition logging.h:63
std::chrono::high_resolution_clock clock_t
Definition logging.h:58
clock_t::time_point timepoint_t
Definition logging.h:59
const char * func_
Definition logging.h:62
logging(const std::string &output_file="Log.txt")
Definition logging.cpp:37