• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef UTIL_PLUGIN_LOG_H
16 #define UTIL_PLUGIN_LOG_H
17 
18 #include <cassert>
19 #include <cstdarg>
20 
21 #include <core/implementation_uids.h>
22 #include <core/intf_logger.h>
23 #include <core/plugin/intf_class_register.h>
24 #include <render/namespace.h>
25 
26 #define PLUGIN_ONCE_RESET RENDER_NS::PluginCheckOnceReset
27 
28 #define PLUGIN_UNUSED(x) (void)(x)
29 #define PLUGIN_STATIC_ASSERT(expression) static_assert(!!(expression))
30 
31 #ifndef NDEBUG
32 #define PLUGIN_FILE_INFO __FILE__, __LINE__
33 #define PLUGIN_ASSERT(expression) \
34     assert(!!(expression) || RENDER_NS::PluginLogAssert(PLUGIN_FILE_INFO, !!(expression), #expression, ""))
35 #define PLUGIN_ASSERT_MSG(expression, ...) \
36     assert(!!(expression) || RENDER_NS::PluginLogAssert(PLUGIN_FILE_INFO, !!(expression), #expression, __VA_ARGS__))
37 #else
38 #define PLUGIN_FILE_INFO "", 0U
39 #define PLUGIN_ASSERT(...)
40 #define PLUGIN_ASSERT_MSG(...)
41 #endif
42 
43 #if defined(PLUGIN_LOG_DISABLED) && (PLUGIN_LOG_DISABLED == 1)
44 #define PLUGIN_LOG_V(...)
45 #define PLUGIN_LOG_D(...)
46 #define PLUGIN_LOG_I(...)
47 #define PLUGIN_LOG_W(...)
48 #define PLUGIN_LOG_E(...)
49 #define PLUGIN_LOG_F(...)
50 #define PLUGIN_LOG_ONCE_V(...)
51 #define PLUGIN_LOG_ONCE_D(...)
52 #define PLUGIN_LOG_ONCE_I(...)
53 #define PLUGIN_LOG_ONCE_W(...)
54 #define PLUGIN_LOG_ONCE_E(...)
55 #define PLUGIN_LOG_ONCE_F(...)
56 
57 #else
58 
59 // Strip all verbose and debug logging code if requested.
60 #if defined(PLUGIN_LOG_NO_DEBUG) && (PLUGIN_LOG_NO_DEBUG == 1)
61 #define PLUGIN_LOG_V(...)
62 #define PLUGIN_LOG_D(...)
63 #define PLUGIN_LOG_ONCE_V(...)
64 #define PLUGIN_LOG_ONCE_D(...)
65 #else
66 /** \addtogroup group_log
67  *  @{
68  */
69 /** Write message to log with verbose log level */
70 #define PLUGIN_LOG_V(...)             \
71     CHECK_FORMAT_STRING(__VA_ARGS__); \
72     RENDER_NS::PluginLog(CORE_NS::ILogger::LogLevel::LOG_VERBOSE, PLUGIN_FILE_INFO, __VA_ARGS__)
73 /** Write message to log with debug log level */
74 #define PLUGIN_LOG_D(...)             \
75     CHECK_FORMAT_STRING(__VA_ARGS__); \
76     RENDER_NS::PluginLog(CORE_NS::ILogger::LogLevel::LOG_DEBUG, PLUGIN_FILE_INFO, __VA_ARGS__)
77 /** \brief NOTE: CORE_ONCE is meant e.g. to prevent excessive flooding in the logs with repeating errors.
78             i.e. It's not meant for normal working code. */
79 /** Write message to log with verbose log level */
80 #define PLUGIN_LOG_ONCE_V(uniqueId, ...) \
81     CHECK_FORMAT_STRING(__VA_ARGS__);    \
82     RENDER_NS::PluginLogOnce(uniqueId, CORE_NS::ILogger::LogLevel::LOG_VERBOSE, PLUGIN_FILE_INFO, __VA_ARGS__)
83 /** \brief NOTE: CORE_ONCE is meant e.g. to prevent excessive flooding in the logs with repeating errors.
84             i.e. It's not meant for normal working code. */
85 /** Write message to log with debug log level */
86 #define PLUGIN_LOG_ONCE_D(uniqueId, ...) \
87     CHECK_FORMAT_STRING(__VA_ARGS__);    \
88     RENDER_NS::PluginLogOnce(uniqueId, CORE_NS::ILogger::LogLevel::LOG_DEBUG, PLUGIN_FILE_INFO, __VA_ARGS__)
89 #endif
90 
91 /** Write message to log with info log level */
92 #define PLUGIN_LOG_I(...)             \
93     CHECK_FORMAT_STRING(__VA_ARGS__); \
94     RENDER_NS::PluginLog(CORE_NS::ILogger::LogLevel::LOG_INFO, PLUGIN_FILE_INFO, __VA_ARGS__)
95 
96 /** Write message to log with warning log level */
97 #define PLUGIN_LOG_W(...)             \
98     CHECK_FORMAT_STRING(__VA_ARGS__); \
99     RENDER_NS::PluginLog(CORE_NS::ILogger::LogLevel::LOG_WARNING, PLUGIN_FILE_INFO, __VA_ARGS__)
100 
101 /** Write message to log with error log level */
102 #define PLUGIN_LOG_E(...)             \
103     CHECK_FORMAT_STRING(__VA_ARGS__); \
104     RENDER_NS::PluginLog(CORE_NS::ILogger::LogLevel::LOG_ERROR, PLUGIN_FILE_INFO, __VA_ARGS__)
105 
106 /** Write message to log with fatal log level */
107 #define PLUGIN_LOG_F(...)             \
108     CHECK_FORMAT_STRING(__VA_ARGS__); \
109     RENDER_NS::PluginLog(CORE_NS::ILogger::LogLevel::LOG_FATAL, PLUGIN_FILE_INFO, __VA_ARGS__)
110 
111 /** \brief NOTE: CORE_ONCE is meant e.g. to prevent excessive flooding in the logs with repeating errors.
112             i.e. It's not meant for normal working code. */
113 /** Write message to log with info log level */
114 #define PLUGIN_LOG_ONCE_I(uniqueId, ...) \
115     CHECK_FORMAT_STRING(__VA_ARGS__);    \
116     RENDER_NS::PluginLogOnce(uniqueId, CORE_NS::ILogger::LogLevel::LOG_INFO, PLUGIN_FILE_INFO, __VA_ARGS__)
117 
118 /** \brief NOTE: CORE_ONCE is meant e.g. to prevent excessive flooding in the logs with repeating errors.
119             i.e. It's not meant for normal working code. */
120 /** Write message to log with warning log level */
121 #define PLUGIN_LOG_ONCE_W(uniqueId, ...) \
122     CHECK_FORMAT_STRING(__VA_ARGS__);    \
123     RENDER_NS::PluginLogOnce(uniqueId, CORE_NS::ILogger::LogLevel::LOG_WARNING, PLUGIN_FILE_INFO, __VA_ARGS__)
124 
125 /** \brief NOTE: CORE_ONCE is meant e.g. to prevent excessive flooding in the logs with repeating errors.
126             i.e. It's not meant for normal working code. */
127 /** Write message to log with error log level */
128 #define PLUGIN_LOG_ONCE_E(uniqueId, ...) \
129     CHECK_FORMAT_STRING(__VA_ARGS__);    \
130     RENDER_NS::PluginLogOnce(uniqueId, CORE_NS::ILogger::LogLevel::LOG_ERROR, PLUGIN_FILE_INFO, __VA_ARGS__)
131 
132 /** \brief NOTE: CORE_ONCE is meant e.g. to prevent excessive flooding in the logs with repeating errors.
133             i.e. It's not meant for normal working code. */
134 /** Write message to log with fatal log level */
135 #define PLUGIN_LOG_ONCE_F(uniqueId, ...) \
136     CHECK_FORMAT_STRING(__VA_ARGS__);    \
137     RENDER_NS::PluginLogOnce(uniqueId, CORE_NS::ILogger::LogLevel::LOG_FATAL, PLUGIN_FILE_INFO, __VA_ARGS__)
138 #endif
139 
RENDER_BEGIN_NAMESPACE()140 RENDER_BEGIN_NAMESPACE()
141 inline CORE_NS::ILogger* GetPluginLogger()
142 {
143     static CORE_NS::ILogger* gPluginGlobalLogger { nullptr };
144     if (gPluginGlobalLogger == nullptr) {
145         gPluginGlobalLogger = CORE_NS::GetInstance<CORE_NS::ILogger>(CORE_NS::UID_LOGGER);
146     }
147     return gPluginGlobalLogger;
148 }
149 
PluginLog(CORE_NS::ILogger::LogLevel logLevel,const BASE_NS::string_view filename,int lineNumber,FORMAT_ATTRIBUTE const char * format,...)150 inline FORMAT_FUNC(4, 5) void PluginLog(CORE_NS::ILogger::LogLevel logLevel, const BASE_NS::string_view filename,
151     int lineNumber, FORMAT_ATTRIBUTE const char* format, ...)
152 {
153     // log manytimes
154     if (CORE_NS::ILogger* logger = GetPluginLogger(); logger) {
155         va_list vl;
156         va_start(vl, format);
157         logger->VLog(logLevel, filename, lineNumber, format, vl);
158         va_end(vl);
159     }
160 }
161 
PluginLogOnce(const BASE_NS::string_view id,CORE_NS::ILogger::LogLevel logLevel,const BASE_NS::string_view filename,int lineNumber,FORMAT_ATTRIBUTE const char * format,...)162 inline FORMAT_FUNC(5, 6) void PluginLogOnce(const BASE_NS::string_view id, CORE_NS::ILogger::LogLevel logLevel,
163     const BASE_NS::string_view filename, int lineNumber, FORMAT_ATTRIBUTE const char* format, ...)
164 {
165     // log once.
166     if (CORE_NS::ILogger* logger = GetPluginLogger(); logger) {
167         va_list vl;
168         va_start(vl, format);
169         logger->VLogOnce(id, logLevel, filename, lineNumber, format, vl);
170         va_end(vl);
171     }
172 }
173 
PluginLogAssert(const BASE_NS::string_view filename,int lineNumber,bool expression,const BASE_NS::string_view expressionString,FORMAT_ATTRIBUTE const char * format,...)174 inline FORMAT_FUNC(5, 6) bool PluginLogAssert(const BASE_NS::string_view filename, int lineNumber, bool expression,
175     const BASE_NS::string_view expressionString, FORMAT_ATTRIBUTE const char* format, ...)
176 {
177     if (!expression) {
178         if (CORE_NS::ILogger* logger = GetPluginLogger(); logger) {
179             va_list vl;
180             va_start(vl, format);
181             logger->VLogAssert(filename, lineNumber, expression, expressionString, format, vl);
182             va_end(vl);
183         }
184     }
185     return expression;
186 }
187 
PluginCheckOnceReset(const BASE_NS::string_view id)188 inline void PluginCheckOnceReset(const BASE_NS::string_view id)
189 {
190     // reset log once flag
191     if (CORE_NS::ILogger* logger = GetPluginLogger(); logger) {
192         logger->CheckOnceReset();
193     }
194 }
195 RENDER_END_NAMESPACE()
196 
197 #endif // UTIL_PLUGIN_LOG_H
198