Coverage Report

Created: 2026-05-30 06:57

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
  FileOutputStreamPrivate(const LogString& filename)
31
0
    : path(filename)
32
0
    {}
33
34
  File path;
35
  Pool pool;
36
  apr_file_t* fileptr{ nullptr };
37
  void open(bool append);
38
};
39
40
IMPLEMENT_LOG4CXX_OBJECT(FileOutputStream)
41
42
FileOutputStream::FileOutputStream(const LogString& filename, bool append)
43
0
  : m_priv(std::make_unique<FileOutputStreamPrivate>(filename))
44
0
{
45
0
  m_priv->open(append);
46
0
}
47
48
FileOutputStream::FileOutputStream(const logchar* filename, bool append)
49
0
  : m_priv(std::make_unique<FileOutputStreamPrivate>(filename))
50
0
{
51
0
  m_priv->open(append);
52
0
}
53
54
void FileOutputStream::FileOutputStreamPrivate::open(bool append)
55
0
{
56
0
  apr_fileperms_t perm = APR_OS_DEFAULT;
57
0
  apr_int32_t flags = APR_WRITE | APR_CREATE;
58
59
0
  if (append)
60
0
  {
61
0
    flags |= APR_APPEND;
62
0
  }
63
0
  else
64
0
  {
65
0
    flags |= APR_TRUNCATE;
66
0
  }
67
68
0
  apr_status_t stat = apr_file_open(&this->fileptr, this->path.getAPRPath(), flags, perm, this->pool.getAPRPool());
69
70
0
  if (stat != APR_SUCCESS)
71
0
  {
72
0
    throw IOException(this->path.getAPRPath(), stat);
73
0
  }
74
0
}
75
76
FileOutputStream::~FileOutputStream()
77
0
{
78
0
  if (m_priv->fileptr)
79
0
  {
80
0
    apr_file_close(m_priv->fileptr);
81
0
  }
82
0
}
83
84
void FileOutputStream::close( LOG4CXX_CLOSE_OUTPUT_STREAM_FORMAL_PARAMETERS )
85
0
{
86
0
  if (m_priv->fileptr)
87
0
  {
88
0
    apr_status_t stat = apr_file_close(m_priv->fileptr);
89
90
0
    if (stat != APR_SUCCESS)
91
0
    {
92
0
      throw IOException(stat);
93
0
    }
94
95
0
    m_priv->fileptr = NULL;
96
0
  }
97
0
}
98
99
void FileOutputStream::flush( LOG4CXX_FLUSH_OUTPUT_STREAM_FORMAL_PARAMETERS )
100
0
{
101
0
}
102
103
void FileOutputStream::write( LOG4CXX_WRITE_OUTPUT_STREAM_FORMAL_PARAMETERS )
104
0
{
105
0
  if (m_priv->fileptr == NULL)
106
0
  {
107
0
    throw NullPointerException(LOG4CXX_STR("FileOutputStream"));
108
0
  }
109
110
0
  size_t nbytes = buf.remaining();
111
0
  while (nbytes > 0)
112
0
  {
113
0
    apr_status_t stat = apr_file_write(
114
0
        m_priv->fileptr, buf.current(), &nbytes);
115
116
0
    if (stat != APR_SUCCESS)
117
0
    {
118
0
      throw IOException(stat);
119
0
    }
120
121
0
    nbytes = buf.increment_position(nbytes);
122
0
  }
123
0
}
124
125
0
apr_file_t* FileOutputStream::getFilePtr() const{
126
0
  return m_priv->fileptr;
127
0
}
128