GpgFrontend Project
A Free, Powerful, Easy-to-Use, Compact, Cross-Platform, and Installation-Free OpenPGP(pgp) Crypto Tool.
LogUtils.h
1 
29 #pragma once
30 
31 // spdlog library configuration
32 #undef SPDLOG_ACTIVE_LEVEL
33 #define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_TRACE
34 #include <spdlog/spdlog.h>
35 
36 // logger fmt
37 #include "core/GpgFrontendCoreExport.h"
38 
39 template <>
40 struct fmt::formatter<QString> {
41  // Parses format specifications.
42  constexpr auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) {
43  return ctx.begin();
44  }
45 
46  // Formats the QString qstr and writes it to the output.
47  template <typename FormatContext>
48  auto format(const QString& qstr, FormatContext& ctx) const
49  -> decltype(ctx.out()) {
50  // Convert QString to UTF-8 QString (to handle Unicode characters
51  // correctly)
52  QByteArray utf8_array = qstr.toUtf8();
53  return fmt::format_to(ctx.out(), "{}", utf8_array.constData());
54  }
55 };
56 
57 template <>
58 struct fmt::formatter<QByteArray> {
59  // Parses format specifications.
60  constexpr auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) {
61  return ctx.begin();
62  }
63 
64  // Formats the QString qstr and writes it to the output.
65  template <typename FormatContext>
66  auto format(const QByteArray& qarray, FormatContext& ctx) const
67  -> decltype(ctx.out()) {
68  // Convert QString to UTF-8 QString (to handle Unicode characters
69  // correctly)
70  return fmt::format_to(ctx.out(), "{}", qarray.constData());
71  }
72 };
73 
74 namespace GpgFrontend {
75 
81 auto GPGFRONTEND_CORE_EXPORT GetDefaultLogger()
82  -> std::shared_ptr<spdlog::logger>;
83 
89 auto GPGFRONTEND_CORE_EXPORT GetCoreLogger() -> std::shared_ptr<spdlog::logger>;
90 
96 auto GPGFRONTEND_CORE_EXPORT GetLogger(const QString&)
97  -> std::shared_ptr<spdlog::logger>;
98 
104 void GPGFRONTEND_CORE_EXPORT SetDefaultLogLevel(spdlog::level::level_enum);
105 
111 void GPGFRONTEND_CORE_EXPORT RegisterAsyncLogger(const QString&,
112  spdlog::level::level_enum);
113 
119 void GPGFRONTEND_CORE_EXPORT RegisterSyncLogger(const QString&,
120  spdlog::level::level_enum);
121 
122 } // namespace GpgFrontend
123 
124 #define GF_DEFAULT_LOG_TRACE(...) \
125  SPDLOG_LOGGER_TRACE(GpgFrontend::GetDefaultLogger(), __VA_ARGS__)
126 #define GF_DEFAULT_LOG_DEBUG(...) \
127  SPDLOG_LOGGER_DEBUG(GpgFrontend::GetDefaultLogger(), __VA_ARGS__)
128 #define GF_DEFAULT_LOG_INFO(...) \
129  SPDLOG_LOGGER_INFO(GpgFrontend::GetDefaultLogger(), __VA_ARGS__)
130 #define GF_DEFAULT_LOG_WARN(...) \
131  SPDLOG_LOGGER_WARN(GpgFrontend::GetDefaultLogger(), __VA_ARGS__)
132 #define GF_DEFAULT_LOG_ERROR(...) \
133  SPDLOG_LOGGER_ERROR(GpgFrontend::GetDefaultLogger(), __VA_ARGS__)
134 
135 #define GF_CORE_LOG_TRACE(...) \
136  SPDLOG_LOGGER_TRACE(GpgFrontend::GetCoreLogger(), __VA_ARGS__)
137 #define GF_CORE_LOG_DEBUG(...) \
138  SPDLOG_LOGGER_DEBUG(GpgFrontend::GetCoreLogger(), __VA_ARGS__)
139 #define GF_CORE_LOG_INFO(...) \
140  SPDLOG_LOGGER_INFO(GpgFrontend::GetCoreLogger(), __VA_ARGS__)
141 #define GF_CORE_LOG_WARN(...) \
142  SPDLOG_LOGGER_WARN(GpgFrontend::GetCoreLogger(), __VA_ARGS__)
143 #define GF_CORE_LOG_ERROR(...) \
144  SPDLOG_LOGGER_ERROR(GpgFrontend::GetCoreLogger(), __VA_ARGS__)
145 
146 #define GF_LOG_TRACE(ID, ...) \
147  SPDLOG_LOGGER_TRACE(GpgFrontend::GetLogger(ID), __VA_ARGS__)
148 #define GF_LOG_DEBUG(ID, ...) \
149  SPDLOG_LOGGER_DEBUG(GpgFrontend::GetLogger(ID), __VA_ARGS__)
150 #define GF_LOG_INFO(ID, ...) \
151  SPDLOG_LOGGER_INFO(GpgFrontend::GetLogger(ID), __VA_ARGS__)
152 #define GF_LOG_WARN(ID, ...) \
153  SPDLOG_LOGGER_WARN(GpgFrontend::GetLogger(ID), __VA_ARGS__)
154 #define GF_LOG_ERROR(ID, ...) \
155  SPDLOG_LOGGER_ERROR(GpgFrontend::GetLogger(ID), __VA_ARGS__)
Definition: app.cpp:39
void RegisterAsyncLogger(const QString &id, spdlog::level::level_enum level)
Definition: LogUtils.cpp:51
auto GetLogger(const QString &id) -> std::shared_ptr< spdlog::logger >
Definition: LogUtils.cpp:43
auto GetDefaultLogger() -> std::shared_ptr< spdlog::logger >
Definition: LogUtils.cpp:35
void SetDefaultLogLevel(spdlog::level::level_enum level)
Set the Default Log Level object.
Definition: LogUtils.cpp:47
auto GetCoreLogger() -> std::shared_ptr< spdlog::logger >
Definition: LogUtils.cpp:39
void RegisterSyncLogger(const QString &id, spdlog::level::level_enum level)
Definition: LogUtils.cpp:55