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 std::string func_{};
63
64 log_duration(const std::string& func, hpp::source_location loc = hpp::source_location::current())
65 : func_(func)
66 {
67
68 }
69
71 {
72 auto end = clock_t::now();
73 auto dur = std::chrono::duration_cast<T>(end - start);
74
75 SPDLOG_LOGGER_CALL(spdlog::get(APPLOG), lvl, "{} : {}", func_, dur);
76 }
77
78};
79
80template<spdlog::level::level_enum lvl = spdlog::level::debug, typename T = std::chrono::microseconds>
82{
83 using clock_t = std::chrono::high_resolution_clock;
84 using timepoint_t = clock_t::time_point;
85
86 timepoint_t start = clock_t::now();
87 const char* func_{};
88 hpp::source_location location_;
89
90 log_stopwatch(const char* func, hpp::source_location loc = hpp::source_location::current())
91 : func_(func)
92 , location_(loc)
93 {
94
95 }
96
98 {
99 auto end = clock_t::now();
100 auto dur = std::chrono::duration_cast<T>(end - start);
101
102 SPDLOG_LOGGER_CALL_LOC(spdlog::get(APPLOG), location_.file_name(), int(location_.line()), func_, lvl, "{} : {}", func_, dur);
103 }
104
105};
106
107// Helper macros to concatenate tokens
108#define APPLOG_CONCATENATE_DETAIL(x, y) x##y
109#define APPLOG_CONCATENATE(x, y) APPLOG_CONCATENATE_DETAIL(x, y)
110
111// Macro to create a unique variable name
112#define APPLOG_UNIQUE_VAR(prefix) APPLOG_CONCATENATE(prefix, __LINE__)
113
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__);
117
118
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);
122
123
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__);
127
128
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);
132
133
134} // 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
std::chrono::high_resolution_clock clock_t
Definition logging.h:58
clock_t::time_point timepoint_t
Definition logging.h:59
std::string func_
Definition logging.h:62
log_duration(const std::string &func, hpp::source_location loc=hpp::source_location::current())
Definition logging.h:64
timepoint_t start
Definition logging.h:61
log_stopwatch(const char *func, hpp::source_location loc=hpp::source_location::current())
Definition logging.h:90
timepoint_t start
Definition logging.h:86
hpp::source_location location_
Definition logging.h:88
std::chrono::high_resolution_clock clock_t
Definition logging.h:83
clock_t::time_point timepoint_t
Definition logging.h:84
const char * func_
Definition logging.h:87
logging(const std::string &output_file="Log.txt")
Definition logging.cpp:37