Coverage Report

Created: 2026-01-09 06:23

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/logging-log4cxx/src/main/cpp/fileoutputstream.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/logstring.h>
19
#include <log4cxx/helpers/fileoutputstream.h>
20
#include <log4cxx/helpers/pool.h>
21
#include <log4cxx/helpers/exception.h>
22
#include <log4cxx/helpers/bytebuffer.h>
23
#include <apr_file_io.h>
24
25
using namespace LOG4CXX_NS;
26
using namespace LOG4CXX_NS::helpers;
27
28
struct FileOutputStream::FileOutputStreamPrivate
29
{
30
771
  FileOutputStreamPrivate() : fileptr(nullptr) {}
31
32
  Pool pool;
33
  apr_file_t* fileptr;
34
};
35
36
IMPLEMENT_LOG4CXX_OBJECT(FileOutputStream)
37
38
FileOutputStream::FileOutputStream(const LogString& filename,
39
771
  bool append) : m_priv(std::make_unique<FileOutputStreamPrivate>())
40
771
{
41
771
  m_priv->fileptr = open(filename, append, m_priv->pool);
42
771
}
43
44
FileOutputStream::FileOutputStream(const logchar* filename,
45
0
  bool append) : m_priv(std::make_unique<FileOutputStreamPrivate>())
46
0
{
47
0
  m_priv->fileptr = open(filename, append, m_priv->pool);
48
0
}
49
50
apr_file_t* FileOutputStream::open(const LogString& filename,
51
  bool append, Pool& pool)
52
771
{
53
771
  apr_fileperms_t perm = APR_OS_DEFAULT;
54
771
  apr_int32_t flags = APR_WRITE | APR_CREATE;
55
56
771
  if (append)
57
298
  {
58
298
    flags |= APR_APPEND;
59
298
  }
60
473
  else
61
473
  {
62
473
    flags |= APR_TRUNCATE;
63
473
  }
64
65
771
  File fn;
66
771
  fn.setPath(filename);
67
771
  apr_file_t* fileptr = 0;
68
771
  apr_status_t stat = fn.open(&fileptr, flags, perm, pool);
69
70
771
  if (stat != APR_SUCCESS)
71
0
  {
72
0
    throw IOException(filename, stat);
73
0
  }
74
75
771
  return fileptr;
76
771
}
77
78
FileOutputStream::~FileOutputStream()
79
771
{
80
771
  if (m_priv->fileptr)
81
0
  {
82
0
    apr_file_close(m_priv->fileptr);
83
0
  }
84
771
}
85
86
void FileOutputStream::close(Pool& /* p */)
87
771
{
88
771
  if (m_priv->fileptr)
89
771
  {
90
771
    apr_status_t stat = apr_file_close(m_priv->fileptr);
91
92
771
    if (stat != APR_SUCCESS)
93
0
    {
94
0
      throw IOException(stat);
95
0
    }
96
97
771
    m_priv->fileptr = NULL;
98
771
  }
99
771
}
100
101
void FileOutputStream::flush(Pool& /* p */)
102
3.85k
{
103
3.85k
}
104
105
void FileOutputStream::write(ByteBuffer& buf, Pool& /* p */ )
106
3.85k
{
107
3.85k
  if (m_priv->fileptr == NULL)
108
0
  {
109
0
    throw NullPointerException(LOG4CXX_STR("FileOutputStream"));
110
0
  }
111
112
3.85k
  size_t nbytes = buf.remaining();
113
3.85k
  size_t pos = buf.position();
114
3.85k
  const char* data = buf.data();
115
116
7.70k
  while (nbytes > 0)
117
3.85k
  {
118
3.85k
    apr_status_t stat = apr_file_write(
119
3.85k
        m_priv->fileptr, data + pos, &nbytes);
120
121
3.85k
    if (stat != APR_SUCCESS)
122
0
    {
123
0
      throw IOException(stat);
124
0
    }
125
126
3.85k
    pos += nbytes;
127
3.85k
    buf.position(pos);
128
3.85k
    nbytes = buf.remaining();
129
3.85k
  }
130
3.85k
}
131
132
0
apr_file_t* FileOutputStream::getFilePtr() const{
133
0
  return m_priv->fileptr;
134
0
}
135