Coverage Report

Created: 2025-10-27 06:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/logging-log4cxx/src/main/cpp/asyncbuffer.cpp
Line
Count
Source
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
#include <log4cxx/helpers/asyncbuffer.h>
19
#include <log4cxx/helpers/transcoder.h>
20
21
namespace LOG4CXX_NS
22
{
23
24
namespace helpers
25
{
26
27
struct AsyncBuffer::Private
28
{
29
  std::vector<MessageBufferAppender> data;
30
31
  Private(const MessageBufferAppender& f)
32
0
    : data{ f }
33
0
  {}
34
35
#if LOG4CXX_ASYNC_BUFFER_SUPPORTS_FMT
36
  StringViewType fmt_string;
37
  FmtArgStore    fmt_args;
38
39
  Private(StringViewType&& format_string, FmtArgStore&& args)
40
    : fmt_string{ std::move(format_string) }
41
    , fmt_args{ std::move(args) }
42
  {}
43
44
#if LOG4CXX_WCHAR_T_API || LOG4CXX_LOGCHAR_IS_WCHAR
45
  WideStringViewType fmt_wstring;
46
  WideFmtArgStore    fmt_wargs;
47
48
  Private(WideStringViewType&& format_string, WideFmtArgStore&& args)
49
    : fmt_wstring{ std::move(format_string) }
50
    , fmt_wargs{ std::move(args) }
51
  {}
52
#endif // LOG4CXX_WCHAR_T_API || LOG4CXX_LOGCHAR_IS_WCHAR
53
#endif // LOG4CXX_ASYNC_BUFFER_SUPPORTS_FMT
54
55
};
56
57
#if LOG4CXX_ASYNC_BUFFER_SUPPORTS_FMT
58
void AsyncBuffer::initializeForFmt(StringViewType&& format_string, FmtArgStore&& args)
59
{
60
  if (!m_priv)
61
    m_priv = std::make_unique<Private>(std::move(format_string), std::move(args));
62
  else
63
  {
64
    m_priv->fmt_string = std::move(format_string);
65
    m_priv->fmt_args = std::move(args);
66
  }
67
}
68
69
#if LOG4CXX_WCHAR_T_API || LOG4CXX_LOGCHAR_IS_WCHAR
70
void AsyncBuffer::initializeForFmt(WideStringViewType&& format_string, WideFmtArgStore&& args)
71
{
72
  if (!m_priv)
73
    m_priv = std::make_unique<Private>(std::move(format_string), std::move(args));
74
  else
75
  {
76
    m_priv->fmt_wstring = std::move(format_string);
77
    m_priv->fmt_wargs = std::move(args);
78
  }
79
}
80
#endif // LOG4CXX_WCHAR_T_API || LOG4CXX_LOGCHAR_IS_WCHAR
81
#endif // LOG4CXX_ASYNC_BUFFER_SUPPORTS_FMT
82
83
/** An empty buffer.
84
*/
85
AsyncBuffer::AsyncBuffer()
86
12.3k
{}
87
88
/** A new buffer with the content of \c other
89
*/
90
AsyncBuffer::AsyncBuffer(AsyncBuffer&& other)
91
0
  : m_priv(std::move(other.m_priv))
92
0
{
93
0
}
94
95
/** Release resources.
96
*/
97
AsyncBuffer::~AsyncBuffer()
98
12.3k
{
99
12.3k
}
100
101
/**
102
* Has no item been added to this?
103
*/
104
bool AsyncBuffer::empty() const
105
24.6k
{
106
24.6k
  bool result{ true };
107
24.6k
  if (m_priv)
108
0
  {
109
0
    result = m_priv->data.empty();
110
#if LOG4CXX_ASYNC_BUFFER_SUPPORTS_FMT
111
    if (result)
112
      result = (0 == m_priv->fmt_string.size());
113
#if LOG4CXX_WCHAR_T_API || LOG4CXX_LOGCHAR_IS_WCHAR
114
    if (result)
115
      result = (0 == m_priv->fmt_wstring.size());
116
#endif // LOG4CXX_WCHAR_T_API || LOG4CXX_LOGCHAR_IS_WCHAR
117
#endif
118
0
  }
119
24.6k
  return result;
120
24.6k
}
121
122
/**
123
* Add text version of buffered values to \c msg
124
*/
125
void AsyncBuffer::renderMessage(LogCharMessageBuffer& msg) const
126
0
{
127
0
  if (m_priv)
128
0
  {
129
0
    for (auto& renderer : m_priv->data)
130
0
      renderer(msg);
131
#if LOG4CXX_ASYNC_BUFFER_SUPPORTS_FMT
132
#if LOG4CXX_LOGCHAR_IS_UTF8
133
    if (0 < m_priv->fmt_string.size())
134
      msg << fmt::vformat(m_priv->fmt_string, m_priv->fmt_args);
135
#if LOG4CXX_WCHAR_T_API
136
    if (0 < m_priv->fmt_wstring.size())
137
    {
138
      LOG4CXX_DECODE_WCHAR(lsMsg, fmt::vformat(m_priv->fmt_wstring, m_priv->fmt_wargs));
139
      msg << lsMsg;
140
    }
141
#endif // LOG4CXX_WCHAR_T_API
142
#endif // LOG4CXX_LOGCHAR_IS_UTF8
143
144
#if LOG4CXX_LOGCHAR_IS_WCHAR
145
    if (0 < m_priv->fmt_wstring.size())
146
      msg << fmt::vformat(m_priv->fmt_wstring, m_priv->fmt_wargs);
147
    if (0 < m_priv->fmt_string.size())
148
    {
149
      LOG4CXX_DECODE_CHAR(lsMsg, fmt::vformat(m_priv->fmt_string, m_priv->fmt_args));
150
      msg << lsMsg;
151
    }
152
#endif // LOG4CXX_LOGCHAR_IS_WCHAR
153
#endif // LOG4CXX_ASYNC_BUFFER_SUPPORTS_FMT
154
0
  }
155
0
}
156
157
/**
158
* Remove all message appenders
159
*/
160
void AsyncBuffer::clear()
161
0
{
162
0
  if (m_priv)
163
0
  {
164
0
    m_priv->data.clear();
165
#if LOG4CXX_ASYNC_BUFFER_SUPPORTS_FMT
166
    m_priv->fmt_string = {};
167
#if LOG4CXX_WCHAR_T_API || LOG4CXX_LOGCHAR_IS_WCHAR
168
    m_priv->fmt_wstring = {};
169
#endif // LOG4CXX_WCHAR_T_API || LOG4CXX_LOGCHAR_IS_WCHAR
170
#endif // LOG4CXX_ASYNC_BUFFER_SUPPORTS_FMT
171
0
  }
172
0
}
173
174
/**
175
 *   Append \c function to this buffer.
176
 */
177
void AsyncBuffer::append(const MessageBufferAppender& f)
178
0
{
179
0
  if (!m_priv)
180
0
    m_priv = std::make_unique<Private>(f);
181
0
  else
182
0
    m_priv->data.push_back(f);
183
0
}
184
185
} // namespace helpers
186
} // namespace LOG4CXX_NS
187