/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 | 3.02k |   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 | 3.02k |   bool append) : m_priv(std::make_unique<FileOutputStreamPrivate>()) | 
| 40 | 3.02k | { | 
| 41 | 3.02k |   m_priv->fileptr = open(filename, append, m_priv->pool); | 
| 42 | 3.02k | } log4cxx::helpers::FileOutputStream::FileOutputStream(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, bool)| Line | Count | Source |  | 39 | 764 |   bool append) : m_priv(std::make_unique<FileOutputStreamPrivate>()) |  | 40 | 764 | { |  | 41 | 764 |   m_priv->fileptr = open(filename, append, m_priv->pool); |  | 42 | 764 | } | 
log4cxx::helpers::FileOutputStream::FileOutputStream(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&, bool)| Line | Count | Source |  | 39 | 2.25k |   bool append) : m_priv(std::make_unique<FileOutputStreamPrivate>()) |  | 40 | 2.25k | { |  | 41 | 2.25k |   m_priv->fileptr = open(filename, append, m_priv->pool); |  | 42 | 2.25k | } | 
 | 
| 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 | } Unexecuted instantiation: log4cxx::helpers::FileOutputStream::FileOutputStream(char const*, bool)Unexecuted instantiation: log4cxx::helpers::FileOutputStream::FileOutputStream(wchar_t const*, bool) | 
| 49 |  |  | 
| 50 |  | apr_file_t* FileOutputStream::open(const LogString& filename, | 
| 51 |  |   bool append, Pool& pool) | 
| 52 | 3.02k | { | 
| 53 | 3.02k |   apr_fileperms_t perm = APR_OS_DEFAULT; | 
| 54 | 3.02k |   apr_int32_t flags = APR_WRITE | APR_CREATE; | 
| 55 |  |  | 
| 56 | 3.02k |   if (append) | 
| 57 | 1.16k |   { | 
| 58 | 1.16k |     flags |= APR_APPEND; | 
| 59 | 1.16k |   } | 
| 60 | 1.86k |   else | 
| 61 | 1.86k |   { | 
| 62 | 1.86k |     flags |= APR_TRUNCATE; | 
| 63 | 1.86k |   } | 
| 64 |  |  | 
| 65 | 3.02k |   File fn; | 
| 66 | 3.02k |   fn.setPath(filename); | 
| 67 | 3.02k |   apr_file_t* fileptr = 0; | 
| 68 | 3.02k |   apr_status_t stat = fn.open(&fileptr, flags, perm, pool); | 
| 69 |  |  | 
| 70 | 3.02k |   if (stat != APR_SUCCESS) | 
| 71 | 0 |   { | 
| 72 | 0 |     throw IOException(filename, stat); | 
| 73 | 0 |   } | 
| 74 |  |  | 
| 75 | 3.02k |   return fileptr; | 
| 76 | 3.02k | } log4cxx::helpers::FileOutputStream::open(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, bool, log4cxx::helpers::Pool&)| Line | Count | Source |  | 52 | 764 | { |  | 53 | 764 |   apr_fileperms_t perm = APR_OS_DEFAULT; |  | 54 | 764 |   apr_int32_t flags = APR_WRITE | APR_CREATE; |  | 55 |  |  |  | 56 | 764 |   if (append) |  | 57 | 320 |   { |  | 58 | 320 |     flags |= APR_APPEND; |  | 59 | 320 |   } |  | 60 | 444 |   else |  | 61 | 444 |   { |  | 62 | 444 |     flags |= APR_TRUNCATE; |  | 63 | 444 |   } |  | 64 |  |  |  | 65 | 764 |   File fn; |  | 66 | 764 |   fn.setPath(filename); |  | 67 | 764 |   apr_file_t* fileptr = 0; |  | 68 | 764 |   apr_status_t stat = fn.open(&fileptr, flags, perm, pool); |  | 69 |  |  |  | 70 | 764 |   if (stat != APR_SUCCESS) |  | 71 | 0 |   { |  | 72 | 0 |     throw IOException(filename, stat); |  | 73 | 0 |   } |  | 74 |  |  |  | 75 | 764 |   return fileptr; |  | 76 | 764 | } | 
log4cxx::helpers::FileOutputStream::open(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&, bool, log4cxx::helpers::Pool&)| Line | Count | Source |  | 52 | 2.25k | { |  | 53 | 2.25k |   apr_fileperms_t perm = APR_OS_DEFAULT; |  | 54 | 2.25k |   apr_int32_t flags = APR_WRITE | APR_CREATE; |  | 55 |  |  |  | 56 | 2.25k |   if (append) |  | 57 | 840 |   { |  | 58 | 840 |     flags |= APR_APPEND; |  | 59 | 840 |   } |  | 60 | 1.41k |   else |  | 61 | 1.41k |   { |  | 62 | 1.41k |     flags |= APR_TRUNCATE; |  | 63 | 1.41k |   } |  | 64 |  |  |  | 65 | 2.25k |   File fn; |  | 66 | 2.25k |   fn.setPath(filename); |  | 67 | 2.25k |   apr_file_t* fileptr = 0; |  | 68 | 2.25k |   apr_status_t stat = fn.open(&fileptr, flags, perm, pool); |  | 69 |  |  |  | 70 | 2.25k |   if (stat != APR_SUCCESS) |  | 71 | 0 |   { |  | 72 | 0 |     throw IOException(filename, stat); |  | 73 | 0 |   } |  | 74 |  |  |  | 75 | 2.25k |   return fileptr; |  | 76 | 2.25k | } | 
 | 
| 77 |  |  | 
| 78 |  | FileOutputStream::~FileOutputStream() | 
| 79 | 3.02k | { | 
| 80 | 3.02k |   if (m_priv->fileptr) | 
| 81 | 0 |   { | 
| 82 | 0 |     apr_file_close(m_priv->fileptr); | 
| 83 | 0 |   } | 
| 84 | 3.02k | } | 
| 85 |  |  | 
| 86 |  | void FileOutputStream::close(Pool& /* p */) | 
| 87 | 3.02k | { | 
| 88 | 3.02k |   if (m_priv->fileptr) | 
| 89 | 3.02k |   { | 
| 90 | 3.02k |     apr_status_t stat = apr_file_close(m_priv->fileptr); | 
| 91 |  |  | 
| 92 | 3.02k |     if (stat != APR_SUCCESS) | 
| 93 | 0 |     { | 
| 94 | 0 |       throw IOException(stat); | 
| 95 | 0 |     } | 
| 96 |  |  | 
| 97 | 3.02k |     m_priv->fileptr = NULL; | 
| 98 | 3.02k |   } | 
| 99 | 3.02k | } | 
| 100 |  |  | 
| 101 |  | void FileOutputStream::flush(Pool& /* p */) | 
| 102 | 15.0k | { | 
| 103 | 15.0k | } | 
| 104 |  |  | 
| 105 |  | void FileOutputStream::write(ByteBuffer& buf, Pool& /* p */ ) | 
| 106 | 91.9k | { | 
| 107 | 91.9k |   if (m_priv->fileptr == NULL) | 
| 108 | 0 |   { | 
| 109 | 0 |     throw NullPointerException(LOG4CXX_STR("FileOutputStream")); | 
| 110 | 0 |   } | 
| 111 |  |  | 
| 112 | 91.9k |   size_t nbytes = buf.remaining(); | 
| 113 | 91.9k |   size_t pos = buf.position(); | 
| 114 | 91.9k |   const char* data = buf.data(); | 
| 115 |  |  | 
| 116 | 172k |   while (nbytes > 0) | 
| 117 | 80.7k |   { | 
| 118 | 80.7k |     apr_status_t stat = apr_file_write( | 
| 119 | 80.7k |         m_priv->fileptr, data + pos, &nbytes); | 
| 120 |  |  | 
| 121 | 80.7k |     if (stat != APR_SUCCESS) | 
| 122 | 0 |     { | 
| 123 | 0 |       throw IOException(stat); | 
| 124 | 0 |     } | 
| 125 |  |  | 
| 126 | 80.7k |     pos += nbytes; | 
| 127 | 80.7k |     buf.position(pos); | 
| 128 | 80.7k |     nbytes = buf.remaining(); | 
| 129 | 80.7k |   } | 
| 130 | 91.9k | } | 
| 131 |  |  | 
| 132 | 0 | apr_file_t* FileOutputStream::getFilePtr() const{ | 
| 133 | 0 |   return m_priv->fileptr; | 
| 134 | 0 | } | 
| 135 |  |  |