2 * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 ///////////////////////////////////////////////////////////////////////////////
18 /// \ingroup tag_SS_LoggerService
21 ///////////////////////////////////////////////////////////////////////////////
22 #include "readerWriter/writer/file_writer.h"
23 #include <sys/types.h>
28 namespace ReaderWriter {
29 CFileWriter::CFileWriter()
35 CFileWriter::~CFileWriter() {
36 if (this->m_FileHandle != -1) { // LCOV_EXCL_BR_LINE 200: it aways open, so m_FileHandle can not be -1
38 this->m_FileHandle = -1;
42 EFrameworkunifiedStatus CFileWriter::Initialize(CLoggerCfg* f_pLoggerCfg,
43 std::string f_Name1, UI_32 f_size1,
44 std::string f_Name2, UI_32 f_size2) {
45 EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusFail;
46 this->m_pLoggerCfg = f_pLoggerCfg;
47 this->m_maxFileSize = f_size1;
48 if ((f_Name1.length() != 0) && (this->m_FileHandle == -1)) { // LCOV_EXCL_BR_LINE 6:Due to the initial status
50 l_eStatus = this->Open();
52 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-: %d", l_eStatus);
56 EFrameworkunifiedStatus CFileWriter::Open(void) {
57 EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusFail;
58 if (this->m_FileHandle != -1) { // LCOV_EXCL_BR_LINE 6:Due to the initial status
59 // File already opened, no action required.
60 l_eStatus = eFrameworkunifiedStatusOK;
61 } else if (m_filename.length() != 0) { // LCOV_EXCL_BR_LINE 6:Because the applicable variable cannot be changed from the external API
62 this->m_FileHandle = open(m_filename.c_str(),
63 O_WRONLY | O_CREAT | O_TRUNC,
65 if (-1 != this->m_FileHandle) { // LCOV_EXCL_BR_LINE 5:The open cannot pass because it cannot be mock
66 l_eStatus = eFrameworkunifiedStatusOK;
67 m_fileposn = lseek(m_FileHandle, 0L, SEEK_CUR);
70 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-: %d", l_eStatus);
74 BOOL CFileWriter::IsOpen(void) {
75 return (-1 == m_FileHandle) ? FALSE : TRUE;
78 EFrameworkunifiedStatus CFileWriter::Write(UI_8* f_data, UI_32 f_length,
79 SI_32& f_bytesWritten) {
80 return (this->WriteData(f_data, f_length, f_bytesWritten));
83 EFrameworkunifiedStatus CFileWriter::WriteData(UI_8* f_data, UI_32 f_length,
84 SI_32& f_bytesWritten) {
85 EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
87 if (-1 == m_FileHandle) { // LCOV_EXCL_BR_LINE 200: m_FileHandle can not be null
88 l_eStatus = eFrameworkunifiedStatusFail;
91 while ((eFrameworkunifiedStatusOK == l_eStatus)
92 && ((f_bytesWritten >= 0) && ((UI_32) f_bytesWritten < f_length))) {
93 SI_32 l_bytesWritten = -1;
94 m_fileposn = lseek(m_FileHandle, 0L, SEEK_CUR);
95 if ((m_fileposn + (UI_32) f_length) >= this->m_maxFileSize) {
96 SI_32 l_deltaLength = static_cast<SI_32>(this->m_maxFileSize - m_fileposn);
97 l_bytesWritten = static_cast<SI_32>(write(m_FileHandle, &f_data[f_bytesWritten],
99 f_bytesWritten += l_bytesWritten;
100 if ((l_bytesWritten >= 0) && (l_deltaLength == l_bytesWritten)) {
101 m_fileposn = lseek(m_FileHandle, 0L, SEEK_SET);
104 l_bytesWritten = static_cast<SI_32>(write(m_FileHandle, &f_data[f_bytesWritten],
105 f_length - f_bytesWritten));
106 f_bytesWritten += l_bytesWritten;
108 l_eStatus = (l_bytesWritten > -1) ? eFrameworkunifiedStatusOK : eFrameworkunifiedStatusFail;
114 void CFileWriter::Close() { // LCOV_EXCL_START 6:Because the condition cannot be set
115 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
116 if (this->m_FileHandle != -1) {
117 (void) close(this->m_FileHandle);
118 this->m_FileHandle = -1;
122 } // namespace ReaderWriter