Apache Log4cxx  Version 1.6.0
Loading...
Searching...
No Matches
asyncbuffer.h
Go to the documentation of this file.
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#ifndef LOG4CXX_ASYNC_BUFFER_H
19#define LOG4CXX_ASYNC_BUFFER_H
20
22#include <functional>
23#include <vector>
24#if LOG4CXX_ASYNC_BUFFER_SUPPORTS_FMT
25#include <fmt/args.h>
26#if LOG4CXX_WCHAR_T_API || LOG4CXX_LOGCHAR_IS_WCHAR
27#include <fmt/xchar.h>
28#endif // LOG4CXX_WCHAR_T_API || LOG4CXX_LOGCHAR_IS_WCHAR
29#endif // LOG4CXX_ASYNC_BUFFER_SUPPORTS_FMT
30
31namespace LOG4CXX_NS
32{
33
34namespace helpers
35{
36
42class LOG4CXX_EXPORT AsyncBuffer
43{
44public: // ...structors
48
52
56
57public: // Operators
62 template<typename T>
63 AsyncBuffer& operator<<(const T& value)
64 {
65 append([value](LogCharMessageBuffer& msgBuf)
66 {
67 msgBuf << value;
68 });
69 return *this;
70 }
71
72#ifdef __cpp_init_captures // C++ >= 14
77 template<typename T>
78 AsyncBuffer& operator<<(const T&& rvalue)
79 {
80 append([value = std::move(rvalue)](LogCharMessageBuffer& msgBuf)
81 {
82 msgBuf << value;
83 });
84 return *this;
85 }
86#endif
87
88public: // Accessors
92 bool empty() const;
93
97 void renderMessage(LogCharMessageBuffer& msg) const;
98
99public: // Modifiers
103 void clear();
104
105#if LOG4CXX_ASYNC_BUFFER_SUPPORTS_FMT
106 using StringViewType = fmt::basic_string_view<char>;
107 using FmtArgStore = fmt::dynamic_format_arg_store<fmt::format_context>;
108 template <typename... Args>
109 void setMessage(fmt::format_string<Args...> fmt_str, Args&&... args)
110 {
111 auto store = FmtArgStore();
112 ( store.push_back(std::forward<Args>(args)), ...);
113 initializeForFmt(std::move(fmt_str), std::move(store));
114 }
115
116#if LOG4CXX_WCHAR_T_API || LOG4CXX_LOGCHAR_IS_WCHAR
117 using WideStringViewType = fmt::basic_string_view<wchar_t>;
118 using WideFmtArgStore = fmt::dynamic_format_arg_store<fmt::wformat_context>;
119 template <typename... Args>
120 void setMessage(fmt::wformat_string<Args...> fmt_str, Args&&... args)
121 {
122 auto store = WideFmtArgStore();
123 ( store.push_back(std::forward<Args>(args)), ...);
124 initializeForFmt(std::move(fmt_str), std::move(store));
125 }
126#endif // LOG4CXX_WCHAR_T_API || LOG4CXX_LOGCHAR_IS_WCHAR
127#endif // LOG4CXX_ASYNC_BUFFER_SUPPORTS_FMT
128
129private:
130 AsyncBuffer(const AsyncBuffer&) = delete;
131 AsyncBuffer& operator=(const AsyncBuffer&) = delete;
132
133 LOG4CXX_DECLARE_PRIVATE_MEMBER_PTR(Private, m_priv)
134 using MessageBufferAppender = std::function<void(LogCharMessageBuffer&)>;
135
139 void append(const MessageBufferAppender& f);
140
141#if LOG4CXX_ASYNC_BUFFER_SUPPORTS_FMT
142 void initializeForFmt(StringViewType&& format_string, FmtArgStore&& args);
143
144#if LOG4CXX_WCHAR_T_API || LOG4CXX_LOGCHAR_IS_WCHAR
145 void initializeForFmt(WideStringViewType&& format_string, WideFmtArgStore&& args);
146#endif // LOG4CXX_WCHAR_T_API || LOG4CXX_LOGCHAR_IS_WCHAR
147#endif // LOG4CXX_ASYNC_BUFFER_SUPPORTS_FMT
148};
149
150} // namespace helpers
151} // namespace LOG4CXX_NS
152
156
157#if !defined(LOG4CXX_THRESHOLD) || LOG4CXX_THRESHOLD <= 10000
177#define LOG4CXX_DEBUG_ASYNC(logger, message) do { \
178 if (LOG4CXX_UNLIKELY(::LOG4CXX_NS::Logger::isDebugEnabledFor(logger))) {\
179 ::LOG4CXX_NS::helpers::AsyncBuffer buf; \
180 logger->addDebugEvent(std::move(buf << message), LOG4CXX_LOCATION); }} while (0)
181
182#if LOG4CXX_ASYNC_BUFFER_SUPPORTS_FMT
202#define LOG4CXX_DEBUG_FMT_ASYNC(logger, fmt, ...) do { \
203 if (LOG4CXX_UNLIKELY(::LOG4CXX_NS::Logger::isDebugEnabledFor(logger))) {\
204 ::LOG4CXX_NS::helpers::AsyncBuffer buf;\
205 buf.setMessage(fmt LOG4CXX_FMT_VA_ARG(__VA_ARGS__));\
206 logger->addDebugEvent(std::move(buf), LOG4CXX_LOCATION); }} while (0)
207#endif // LOG4CXX_ASYNC_BUFFER_SUPPORTS_FMT
208#else
209#define LOG4CXX_DEBUG_ASYNC(logger, message)
210#define LOG4CXX_DEBUG_FMT_ASYNC(logger, message)
211#endif
212
213#if !defined(LOG4CXX_THRESHOLD) || LOG4CXX_THRESHOLD <= 5000
225#define LOG4CXX_TRACE_ASYNC(logger, message) do { \
226 if (LOG4CXX_UNLIKELY(::LOG4CXX_NS::Logger::isTraceEnabledFor(logger))) {\
227 ::LOG4CXX_NS::helpers::AsyncBuffer buf; \
228 logger->addTraceEvent(std::move(buf << message), LOG4CXX_LOCATION); }} while (0)
229
230#if LOG4CXX_ASYNC_BUFFER_SUPPORTS_FMT
243#define LOG4CXX_TRACE_FMT_ASYNC(logger, fmt, ...) do { \
244 if (LOG4CXX_UNLIKELY(::LOG4CXX_NS::Logger::isTraceEnabledFor(logger))) {\
245 ::LOG4CXX_NS::helpers::AsyncBuffer buf;\
246 buf.setMessage(fmt LOG4CXX_FMT_VA_ARG(__VA_ARGS__));\
247 logger->addTraceEvent(std::move(buf), LOG4CXX_LOCATION); }} while (0)
248#endif // LOG4CXX_ASYNC_BUFFER_SUPPORTS_FMT
249
250#else
251#define LOG4CXX_TRACE_ASYNC(logger, message)
252#define LOG4CXX_TRACE_FMT_ASYNC(logger, message)
253#endif
254
255#if !defined(LOG4CXX_THRESHOLD) || LOG4CXX_THRESHOLD <= 20000
272#define LOG4CXX_INFO_ASYNC(logger, message) do { \
273 if (::LOG4CXX_NS::Logger::isInfoEnabledFor(logger)) {\
274 ::LOG4CXX_NS::helpers::AsyncBuffer buf;\
275 logger->addInfoEvent(std::move(buf << message), LOG4CXX_LOCATION);\
276 }} while (0)
277
278#endif
279
280#if LOG4CXX_ASYNC_BUFFER_SUPPORTS_FMT
297#define LOG4CXX_INFO_FMT_ASYNC(logger, fmt, ...) do { \
298 if (::LOG4CXX_NS::Logger::isInfoEnabledFor(logger)) {\
299 ::LOG4CXX_NS::helpers::AsyncBuffer buf;\
300 buf.setMessage(fmt LOG4CXX_FMT_VA_ARG(__VA_ARGS__));\
301 logger->addInfoEvent(std::move(buf), LOG4CXX_LOCATION); }} while (0)
302#endif // LOG4CXX_ASYNC_BUFFER_SUPPORTS_FMT
303
304#else
305#define LOG4CXX_INFO_ASYNC(logger, message)
306#define LOG4CXX_INFO_FMT_ASYNC(logger, message)
307#endif
308
309#if !defined(LOG4CXX_THRESHOLD) || LOG4CXX_THRESHOLD <= 30000
324#define LOG4CXX_WARN_ASYNC(logger, message) do { \
325 if (::LOG4CXX_NS::Logger::isWarnEnabledFor(logger)) {\
326 ::LOG4CXX_NS::helpers::AsyncBuffer buf; \
327 logger->addWarnEvent(std::move(buf << message), LOG4CXX_LOCATION); }} while (0)
328
329#if LOG4CXX_ASYNC_BUFFER_SUPPORTS_FMT
345#define LOG4CXX_WARN_FMT_ASYNC(logger, fmt, ...) do { \
346 if (::LOG4CXX_NS::Logger::isWarnEnabledFor(logger)) {\
347 ::LOG4CXX_NS::helpers::AsyncBuffer buf;\
348 buf.setMessage(fmt LOG4CXX_FMT_VA_ARG(__VA_ARGS__));\
349 logger->addWarnEvent(std::move(buf), LOG4CXX_LOCATION); }} while (0)
350#endif // LOG4CXX_ASYNC_BUFFER_SUPPORTS_FMT
351
352#else
353#define LOG4CXX_WARN_ASYNC(logger, message)
354#define LOG4CXX_WARN_FMT_ASYNC(logger, message)
355#endif
356
357#if !defined(LOG4CXX_THRESHOLD) || LOG4CXX_THRESHOLD <= 40000
372#define LOG4CXX_ERROR_ASYNC(logger, message) do { \
373 if (::LOG4CXX_NS::Logger::isErrorEnabledFor(logger)) {\
374 ::LOG4CXX_NS::helpers::AsyncBuffer buf; \
375 logger->addErrorEvent(std::move(buf << message), LOG4CXX_LOCATION); }} while (0)
376
377#if LOG4CXX_ASYNC_BUFFER_SUPPORTS_FMT
393#define LOG4CXX_ERROR_FMT_ASYNC(logger, fmt, ...) do { \
394 if (::LOG4CXX_NS::Logger::isErrorEnabledFor(logger)) {\
395 ::LOG4CXX_NS::helpers::AsyncBuffer buf;\
396 buf.setMessage(fmt LOG4CXX_FMT_VA_ARG(__VA_ARGS__));\
397 logger->addErrorEvent(std::move(buf), LOG4CXX_LOCATION); }} while (0)
398#endif // LOG4CXX_ASYNC_BUFFER_SUPPORTS_FMT
399
407#define LOG4CXX_ASSERT_ASYNC(logger, condition, message) do { \
408 if (!(condition) && ::LOG4CXX_NS::Logger::isErrorEnabledFor(logger)) {\
409 ::LOG4CXX_NS::helpers::AsyncBuffer buf; \
410 LOG4CXX_STACKTRACE \
411 logger->addErrorEvent(std::move(buf << message), LOG4CXX_LOCATION); }} while (0)
412
413#if LOG4CXX_ASYNC_BUFFER_SUPPORTS_FMT
424#define LOG4CXX_ASSERT_FMT_ASYNC(logger, condition, fmt, ...) do { \
425 if (!(condition) && ::LOG4CXX_NS::Logger::isErrorEnabledFor(logger)) {\
426 LOG4CXX_STACKTRACE \
427 ::LOG4CXX_NS::helpers::AsyncBuffer buf;\
428 buf.setMessage(fmt LOG4CXX_FMT_VA_ARG(__VA_ARGS__));\
429 logger->addErrorEvent(std::move(buf), LOG4CXX_LOCATION); }} while (0)
430#endif // LOG4CXX_ASYNC_BUFFER_SUPPORTS_FMT
431
432#else
433#define LOG4CXX_ERROR_ASYNC(logger, message)
434#define LOG4CXX_ERROR_FMT_ASYNC(logger, message)
435#define LOG4CXX_ASSERT_ASYNC(logger, condition, message)
436#define LOG4CXX_ASSERT_FMT_ASYNC(logger, condition, message)
437#endif
438
439#if !defined(LOG4CXX_THRESHOLD) || LOG4CXX_THRESHOLD <= 50000
451#define LOG4CXX_FATAL_ASYNC(logger, message) do { \
452 if (::LOG4CXX_NS::Logger::isFatalEnabledFor(logger)) {\
453 ::LOG4CXX_NS::helpers::AsyncBuffer buf; \
454 logger->addFatalEvent(std::move(buf << message), LOG4CXX_LOCATION); }} while (0)
455
456#if LOG4CXX_ASYNC_BUFFER_SUPPORTS_FMT
469#define LOG4CXX_FATAL_FMT_ASYNC(logger, fmt, ...) do { \
470 if (::LOG4CXX_NS::Logger::isFatalEnabledFor(logger)) {\
471 ::LOG4CXX_NS::helpers::AsyncBuffer buf;\
472 buf.setMessage(fmt LOG4CXX_FMT_VA_ARG(__VA_ARGS__));\
473 logger->addFatalEvent(std::move(buf), LOG4CXX_LOCATION); }} while (0)
474#endif // LOG4CXX_ASYNC_BUFFER_SUPPORTS_FMT
475
476#else
477#define LOG4CXX_FATAL_ASYNC(logger, message)
478#define LOG4CXX_FATAL_FMT_ASYNC(logger, message)
479#endif
480
This class is used by the LOG4CXX_INFO_ASYNC and similar macros to support insertion operators.
Definition asyncbuffer.h:43
bool empty() const
Has no item been added to this?
fmt::dynamic_format_arg_store< fmt::format_context > FmtArgStore
Definition asyncbuffer.h:107
~AsyncBuffer()
Release resources.
fmt::basic_string_view< wchar_t > WideStringViewType
Definition asyncbuffer.h:117
fmt::dynamic_format_arg_store< fmt::wformat_context > WideFmtArgStore
Definition asyncbuffer.h:118
AsyncBuffer(AsyncBuffer &&other)
A new buffer with the content of other.
void setMessage(fmt::format_string< Args... > fmt_str, Args &&... args)
Definition asyncbuffer.h:109
void renderMessage(LogCharMessageBuffer &msg) const
Add text version of buffered values to msg.
AsyncBuffer()
An empty buffer.
void setMessage(fmt::wformat_string< Args... > fmt_str, Args &&... args)
Definition asyncbuffer.h:120
AsyncBuffer & operator<<(const T &value)
Append a function to this buffer that will convert value to text.
Definition asyncbuffer.h:63
void clear()
Remove all message appenders.
fmt::basic_string_view< char > StringViewType
Definition asyncbuffer.h:106
log4cxx::helpers::UniCharMessageBuffer & operator<<(log4cxx::helpers::UniCharMessageBuffer &mb, const QString &msg)
Definition log4cxx-qt/messagebuffer.h:24