Photon Common Library 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
logging.h
Go to the documentation of this file.
1#pragma once
2
9#include "Common/config.h"
10#include "Common/macro.h"
11
12#include <string>
13#include <vector>
14#include <cstddef>
15#include <format>
16
24namespace ph
25{
26
27struct LogGroup final
28{
29public:
30 std::string groupName;
31 std::string category;
32};
33
34class LogGroups final
35{
36public:
37 inline LogGroups() = default;
38 inline LogGroups(const LogGroups& other) = default;
39 inline LogGroups& operator = (const LogGroups& rhs) = default;
40
41 std::size_t addGroup(std::string_view groupName, std::string_view category = "");
42 std::size_t numGroups() const;
43 const LogGroup& getGroup(std::size_t index) const;
44
45private:
46 std::vector<LogGroup> m_groups;
47};
48
50
51}// end namespace ph
52
54{
55
59void init();
60
64void exit();
65
70
74std::size_t add_log_group(std::string_view groupName, std::string_view category = "");
75
79void log_to_logger(const Logger& logger, std::string_view groupName, ELogLevel logLevel, std::string_view logMessage);
80
81}// end namespace ph::detail::core_logging
82
87#define PH_DECLARE_LOG_GROUP(groupName)\
88 const ::ph::Logger& internal_impl_logger_access_##groupName()
89
92#define PH_DEFINE_LOG_GROUP(groupName, category)\
93 const ::ph::Logger& internal_impl_logger_access_##groupName()\
94 {\
95 static const std::size_t logGroupIndex = ::ph::detail::core_logging::add_log_group(#groupName, #category);\
96 \
97 return ::ph::detail::core_logging::get_logger();\
98 }
99
100#define PH_DEFINE_INLINE_LOG_GROUP(groupName, category)\
101 inline const ::ph::Logger& internal_impl_logger_access_##groupName()\
102 {\
103 static const std::size_t logGroupIndex = ::ph::detail::core_logging::add_log_group(#groupName, #category);\
104 \
105 return ::ph::detail::core_logging::get_logger();\
106 }
107
111#define PH_DEFINE_INTERNAL_LOG_GROUP(groupName, category)\
112 namespace\
113 {\
114 PH_DEFINE_INLINE_LOG_GROUP(groupName, category);\
115 }
116
120#define PH_DEFINE_EXTERNAL_LOG_GROUP(groupName, category) PH_DEFINE_INLINE_LOG_GROUP(groupName, category)
121
122#define PH_LOG_RAW_STRING_TO_CORE_LOGGER(groupName, level, rawStringExpr)\
123 do\
124 {\
125 constexpr auto logLevel = ::ph::ELogLevel::level;\
126 const auto rawString = rawStringExpr;\
127 if constexpr(::ph::is_once(logLevel))\
128 {\
129 static const bool PH_CONCAT_2(dummy, __LINE__) = [&]()\
130 {\
131 ::ph::detail::core_logging::log_to_logger(\
132 internal_impl_logger_access_##groupName(),\
133 #groupName,\
134 logLevel,\
135 rawString);\
136 return true;\
137 }();\
138 }\
139 else\
140 {\
141 ::ph::detail::core_logging::log_to_logger(\
142 internal_impl_logger_access_##groupName(),\
143 #groupName,\
144 logLevel,\
145 rawString);\
146 }\
147 } while(0)
148
149// TODO: it could be beneficial to determine when can we use std::vformat()
150// instead of always using std::format() for logging
151// PH_LOG_STRING() variant for directly logging a runtime string?
152
153#define PH_LOG_FORMAT_STRING_TO_CORE_LOGGER(groupName, level, formatString, ...)\
154 PH_LOG_RAW_STRING_TO_CORE_LOGGER(\
155 groupName,\
156 level,\
157 std::format(formatString __VA_OPT__(,) __VA_ARGS__))
158
159#if PH_ENABLE_DEBUG_LOG
160 #define PH_DEBUG_LOG_STRING(groupName, rawString) PH_LOG_RAW_STRING_TO_CORE_LOGGER(groupName, Debug, rawString)
161 #define PH_DEBUG_LOG_STRING_ONCE(groupName, rawString) PH_LOG_RAW_STRING_TO_CORE_LOGGER(groupName, DebugOnce, rawString)
162 #define PH_DEBUG_LOG(groupName, formatString, ...) PH_LOG_FORMAT_STRING_TO_CORE_LOGGER(groupName, Debug, formatString, __VA_ARGS__)
163 #define PH_DEBUG_LOG_ONCE(groupName, formatString, ...) PH_LOG_FORMAT_STRING_TO_CORE_LOGGER(groupName, DebugOnce, formatString, __VA_ARGS__)
164#else
165 #define PH_DEBUG_LOG_STRING(groupName, rawString) PH_NO_OP()
166 #define PH_DEBUG_LOG_STRING_ONCE(groupName, rawString) PH_NO_OP()
167 #define PH_DEBUG_LOG(groupName, formatString, ...) PH_NO_OP()
168 #define PH_DEBUG_LOG_ONCE(groupName, formatString, ...) PH_NO_OP()
169#endif
170
171#define PH_LOG_STRING(groupName, level, rawString) PH_LOG_RAW_STRING_TO_CORE_LOGGER(groupName, level, rawString)
172#define PH_LOG(groupName, level, formatString, ...) PH_LOG_FORMAT_STRING_TO_CORE_LOGGER(groupName, level, formatString, __VA_ARGS__)
173
174namespace ph
175{
176
177// Photon renderer's default log group
178PH_DECLARE_LOG_GROUP(PhotonRenderer);
179
185#if PH_ENABLE_DEBUG_LOG
186 #define PH_DEFAULT_DEBUG_LOG_STRING(rawString) PH_DEBUG_LOG_STRING(PhotonRenderer, rawString)
187 #define PH_DEFAULT_DEBUG_LOG_STRING_ONCE(rawString) PH_DEBUG_LOG_STRING_ONCE(PhotonRenderer, rawString)
188 #define PH_DEFAULT_DEBUG_LOG(formatString, ...) PH_DEBUG_LOG(PhotonRenderer, formatString, __VA_ARGS__)
189 #define PH_DEFAULT_DEBUG_LOG_ONCE(formatString, ...) PH_DEBUG_LOG_ONCE(PhotonRenderer, formatString, __VA_ARGS__)
190#else
191 #define PH_DEFAULT_DEBUG_LOG_STRING(rawString, ...) PH_NO_OP()
192 #define PH_DEFAULT_DEBUG_LOG_STRING_ONCE(rawString, ...) PH_NO_OP()
193 #define PH_DEFAULT_DEBUG_LOG(formatString, ...) PH_NO_OP()
194 #define PH_DEFAULT_DEBUG_LOG_ONCE(formatString, ...) PH_NO_OP()
195#endif
196
197#define PH_DEFAULT_LOG_STRING(level, rawString) PH_LOG_STRING(PhotonRenderer, level, rawString)
198#define PH_DEFAULT_LOG(level, formatString, ...) PH_LOG(PhotonRenderer, level, formatString, __VA_ARGS__)
200
201}// end namespace ph
Definition logging.h:35
LogGroups & operator=(const LogGroups &rhs)=default
LogGroups(const LogGroups &other)=default
LogGroups()=default
const LogGroup & getGroup(std::size_t index) const
Definition logging.cpp:167
std::size_t numGroups() const
Definition logging.cpp:162
std::size_t addGroup(std::string_view groupName, std::string_view category="")
Definition logging.cpp:156
Definition Logger.h:15
Configurations for the entire toolset.
#define PH_DECLARE_LOG_GROUP(groupName)
Declares a logger. The logger should be defined using PH_DEFINE_LOG_GROUP() somewhere in the source (...
Definition logging.h:87
Useful macro definitions for general operations.
Core logging functionalities. Most logs will output information (logs) via a main logger,...
Definition logging.h:54
std::size_t add_log_group(std::string_view groupName, std::string_view category="")
Add a log group to the core logger.
Definition logging.cpp:134
Logger & get_logger()
Get the core logger.
Definition logging.cpp:126
void log_to_logger(const Logger &logger, std::string_view groupName, ELogLevel logLevel, std::string_view logMessage)
Log information to the specified logger.
Definition logging.cpp:144
void init()
Initializes core logging functionalities. Any logging is only valid after calling init().
Definition logging.cpp:79
void exit()
Terminates core logging functionalities. Cleanup after logging is finished.
Definition logging.cpp:114
The root for all renderer implementations.
Definition assertion.h:9
LogGroups get_core_log_groups()
Definition logging.cpp:174
ELogLevel
Definition ELogLevel.h:9
Definition logging.h:28
std::string category
Definition logging.h:31
std::string groupName
Definition logging.h:30