Coverage Report

Created: 2025-07-01 06:08

/src/logging-log4cxx/src/main/cpp/outputstreamwriter.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
18
#include <log4cxx/logstring.h>
19
#include <log4cxx/helpers/outputstreamwriter.h>
20
#include <log4cxx/helpers/exception.h>
21
#include <log4cxx/helpers/charsetencoder.h>
22
#include <log4cxx/helpers/bytebuffer.h>
23
#include <log4cxx/helpers/stringhelper.h>
24
25
using namespace LOG4CXX_NS;
26
using namespace LOG4CXX_NS::helpers;
27
28
IMPLEMENT_LOG4CXX_OBJECT(OutputStreamWriter)
29
30
struct OutputStreamWriter::OutputStreamWriterPrivate{
31
0
  OutputStreamWriterPrivate(OutputStreamPtr& out1) : out(out1), enc(CharsetEncoder::getDefaultEncoder()){}
32
33
  OutputStreamWriterPrivate(OutputStreamPtr& out1,
34
                CharsetEncoderPtr& enc1)
35
0
    : out(out1), enc(enc1){}
36
37
  OutputStreamPtr out;
38
  CharsetEncoderPtr enc;
39
};
40
41
OutputStreamWriter::OutputStreamWriter(OutputStreamPtr& out1)
42
0
  : m_priv(std::make_unique<OutputStreamWriterPrivate>(out1))
43
0
{
44
0
  if (out1 == 0)
45
0
  {
46
0
    throw NullPointerException(LOG4CXX_STR("out parameter may not be null."));
47
0
  }
48
0
}
49
50
OutputStreamWriter::OutputStreamWriter(OutputStreamPtr& out1,
51
  CharsetEncoderPtr& enc1)
52
0
  : m_priv(std::make_unique<OutputStreamWriterPrivate>(out1, enc1))
53
0
{
54
0
  if (out1 == 0)
55
0
  {
56
0
    throw NullPointerException(LOG4CXX_STR("out parameter may not be null."));
57
0
  }
58
59
0
  if (enc1 == 0)
60
0
  {
61
0
    throw NullPointerException(LOG4CXX_STR("enc parameter may not be null."));
62
0
  }
63
0
}
64
65
OutputStreamWriter::~OutputStreamWriter()
66
0
{
67
0
}
68
69
void OutputStreamWriter::close(Pool& p)
70
0
{
71
0
  m_priv->out->close(p);
72
0
}
73
74
void OutputStreamWriter::flush(Pool& p)
75
0
{
76
0
  m_priv->out->flush(p);
77
0
}
78
79
void OutputStreamWriter::write(const LogString& str, Pool& p)
80
0
{
81
0
  if (str.empty())
82
0
    return;
83
0
  if (CharsetEncoder::isTriviallyCopyable(str, m_priv->enc))
84
0
  {
85
0
    ByteBuffer buf((char*)str.data(), str.size() * sizeof (logchar));
86
0
    m_priv->out->write(buf, p);
87
0
  }
88
0
  else
89
0
  {
90
0
    enum { BUFSIZE = 1024 };
91
0
    char stackData[BUFSIZE];
92
0
    char* rawbuf = stackData;
93
0
    size_t bufSize = BUFSIZE;
94
#ifdef LOG4CXX_MULTI_PROCESS
95
    std::vector<char> heapData;
96
    // Ensure the logging event is a single write system call to keep events from each process separate
97
    if (bufSize < str.length() * 2)
98
    {
99
      heapData.resize(bufSize = str.length() * 2);
100
      rawbuf = heapData.data();
101
    }
102
#endif
103
0
    ByteBuffer buf(rawbuf, bufSize);
104
0
    m_priv->enc->reset();
105
0
    LogString::const_iterator iter = str.begin();
106
107
0
    while (iter != str.end())
108
0
    {
109
0
      CharsetEncoder::encode(m_priv->enc, str, iter, buf);
110
0
      buf.flip();
111
0
      m_priv->out->write(buf, p);
112
0
      buf.clear();
113
0
    }
114
115
0
    CharsetEncoder::encode(m_priv->enc, str, iter, buf);
116
0
    m_priv->enc->flush(buf);
117
0
    buf.flip();
118
0
    m_priv->out->write(buf, p);
119
0
  }
120
0
}
121
122
OutputStreamPtr OutputStreamWriter::getOutputStreamPtr() const
123
0
{
124
0
  return m_priv->out;
125
0
}
126