FlowArithmeticCalculator 1.0.0
Flow Arithmetic Calculator
Loading...
Searching...
No Matches
Loggable.h
Go to the documentation of this file.
1#ifndef LOGGABLE_H
2#define LOGGABLE_H
3
4// GCC-specific demangling API
5#ifdef __GNUC__
6#include <cxxabi.h>
7
8#include <cstdlib>
9#endif
10
11#include <regex>
12#include <string>
13
14#include "spdlog/sinks/stdout_color_sinks.h"
15#include "spdlog/spdlog.h"
16
17#ifdef _WIN32
18#include <dbghelp.h>
19#include <windows.h>
20
21#pragma comment(lib, "dbghelp.lib")
22#endif
23
27namespace business_logic {
36 template <typename Clazz>
37 class Loggable {
38 public:
40 // get the name of the given class; may be mangled
41 // NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
42 const auto *name = typeid(Clazz).name();
43
44 // if gnu libstdc++ available, demangle it
45#ifdef __GNUC__
46 int status = 0;
47 name = abi::__cxa_demangle(name, nullptr, nullptr, &status);
48#endif
49
50 std::string classNameStr(name);
51#ifdef _WIN32
52 char undecoratedName[1024];
53 UnDecorateSymbolName(name, undecoratedName, sizeof(undecoratedName), UNDNAME_COMPLETE);
54
55 classNameStr = std::string(undecoratedName);
56#endif
57
58 // extract just the class name to get rid of the namespace & possible template arguments
59 // using regex; e.g.
60 // 'business_logic::window::GLFWQtWindowImpl<business_logic::renderer::SkiaRendererImpl>'
61 // -> 'GLFWQtWindowImpl'
62 const std::regex regex(R"([^:<>]+(?=<|$))");
63 std::cmatch match;
64 if (std::regex_search(name, match, regex)) {
65 classNameStr = match[0].str();
66 }
67
68 logger = spdlog::get(classNameStr);
69 if (!logger) {
70 logger = spdlog::stdout_color_st(classNameStr);
71 }
72 }
73
74 protected:
75 std::shared_ptr<spdlog::logger> logger;
76 };
77} // namespace business_logic
78
79#endif // LOGGABLE_H
Class that provides a logger for the given class; automatically deduces the logger.
Definition Loggable.h:37
std::shared_ptr< spdlog::logger > logger
Definition Loggable.h:75
The business logic module.