Coverage Report

Created: 2025-07-01 06:08

/src/logging-log4cxx/src/main/cpp/bufferedwriter.cpp
Line
Count
Source (jump to first uncovered line)
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
#include <log4cxx/logstring.h>
18
#include <log4cxx/helpers/bufferedwriter.h>
19
#include <log4cxx/helpers/pool.h>
20
21
using namespace LOG4CXX_NS;
22
using namespace LOG4CXX_NS::helpers;
23
24
struct BufferedWriter::BufferedWriterPriv
25
{
26
  BufferedWriterPriv(WriterPtr& out1, size_t sz1) :
27
0
    out(out1),
28
0
    sz(sz1)
29
0
  {}
30
31
  WriterPtr out;
32
  size_t sz;
33
  LogString buf;
34
};
35
36
IMPLEMENT_LOG4CXX_OBJECT(BufferedWriter)
37
38
BufferedWriter::BufferedWriter(WriterPtr& out1)
39
0
  : BufferedWriter(out1, 1024)
40
0
{
41
0
}
42
43
BufferedWriter::BufferedWriter(WriterPtr& out1, size_t sz1)
44
0
  : m_priv(std::make_unique<BufferedWriterPriv>(out1, sz1))
45
0
{
46
0
}
47
48
BufferedWriter::~BufferedWriter()
49
0
{
50
0
}
51
52
void BufferedWriter::close(Pool& p)
53
0
{
54
0
  flush(p);
55
0
  m_priv->out->close(p);
56
0
}
57
58
void BufferedWriter::flush(Pool& p)
59
0
{
60
0
  if (m_priv->buf.length() > 0)
61
0
  {
62
0
    m_priv->out->write(m_priv->buf, p);
63
0
    m_priv->buf.erase(m_priv->buf.begin(), m_priv->buf.end());
64
0
  }
65
0
}
66
67
void BufferedWriter::write(const LogString& str, Pool& p)
68
0
{
69
0
  if (m_priv->buf.length() + str.length() > m_priv->sz)
70
0
  {
71
0
    m_priv->out->write(m_priv->buf, p);
72
0
    m_priv->buf.erase(m_priv->buf.begin(), m_priv->buf.end());
73
0
  }
74
75
0
  if (str.length() > m_priv->sz)
76
0
  {
77
0
    m_priv->out->write(str, p);
78
0
  }
79
0
  else
80
0
  {
81
0
    m_priv->buf.append(str);
82
0
  }
83
0
}
84
85
WriterPtr BufferedWriter::getWriter() const
86
0
{
87
0
  return m_priv->out;
88
0
}
89