F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
LogFile.cpp
Go to the documentation of this file.
1 // \copyright
2 // Copyright 2009-2015, by the California Institute of Technology.
3 // ALL RIGHTS RESERVED. United States Government Sponsorship
4 // acknowledged.
5 
7 #include <Fw/Types/Assert.hpp>
8 #include <Os/File.hpp>
9 #include <Os/FileSystem.hpp>
10 #include <limits>
11 #include <cstring>
12 #include <cstdio>
13 #include <Fw/Types/StringUtils.hpp>
14 
15 
16 namespace Svc {
17 
18  // ----------------------------------------------------------------------
19  // Initialization/Exiting
20  // ----------------------------------------------------------------------
21 
23  m_fileName(), m_file(), m_maxFileSize(0), m_openFile(false), m_currentFileSize(0)
24  {
25 
26  }
27 
29  {
30  // Close the file if needed:
31  if (this->m_openFile) {
32  this->m_file.close();
33  }
34  }
35 
36  // ----------------------------------------------------------------------
37  // Member Functions
38  // ----------------------------------------------------------------------
39 
40  bool LogFile::write_to_log(const char *const buf, const U32 size)
41  {
42 
43  FW_ASSERT(buf != nullptr);
44 
45  bool status = true;
46 
47  // Print to file if there is one, and given a valid size:
48  if (this->m_openFile && size > 0) {
49 
50  // Make sure we won't exceed the maximum size:
51  // Note: second condition in if statement is true if there is overflow
52  // in the addition below
53  U32 projectedSize = this->m_currentFileSize + size;
54  if ( projectedSize > this->m_maxFileSize ||
55  (this->m_currentFileSize > (std::numeric_limits<U32>::max() - size)) ) {
56 
57  status = false;
58  this->m_openFile = false;
59  this->m_file.close();
60  }
61  // Won't exceed max size, so write to file:
62  else {
63 
64  FwSignedSizeType writeSize = size;
65  Os::File::Status stat = this->m_file.write(reinterpret_cast<const U8*>(buf),writeSize,Os::File::WAIT);
66 
67  // Assert that we are not trying to write to a file we never opened:
69 
70  // Only return a good status if the write was valid
71  status = (writeSize > 0);
72 
73  this->m_currentFileSize += static_cast<U32>(writeSize);
74  }
75  }
76 
77  return status;
78  }
79 
80 
81  bool LogFile::set_log_file(const char* fileName, const U32 maxSize, const U32 maxBackups)
82  {
83  FW_ASSERT(fileName != nullptr);
84 
85  // If there is already a previously open file then close it:
86  if (this->m_openFile) {
87  this->m_openFile = false;
88  this->m_file.close();
89  }
90  Fw::FileNameString searchFilename;
91  Fw::FormatStatus formatStatus = searchFilename.format("%s", fileName);
92 
93  // If file name is too large, return failure:
94  if (formatStatus != Fw::FormatStatus::SUCCESS) {
95  return false;
96  }
97 
98  // Check if file already exists, and if it does try to tack on a suffix.
99  // Quit after maxBackups suffix addition tries (first try is w/ the original name).
100  U32 suffix = 0;
101  bool failedSuffix = false;
102  FwSignedSizeType fileSize = 0;
103  while (Os::FileSystem::getFileSize(searchFilename.toChar(), fileSize) == Os::FileSystem::OP_OK) {
104  // Not able to create a new non-existing file in maxBackups tries, then mark that it failed:
105  if (suffix >= maxBackups) {
106  failedSuffix = true;
107  break;
108  }
109  // Format and check for error and overflows
110  formatStatus = searchFilename.format("%s%" PRIu32, fileName, suffix);
111  if (formatStatus != Fw::FormatStatus::SUCCESS) {
112  return false;
113  }
114  ++suffix;
115  }
116 
117  // If failed trying to make a new file, just use the original file
118  if (failedSuffix) {
119  searchFilename = fileName;
120  }
121 
122  // Open the file (using CREATE so that it truncates an already existing file):
123  Os::File::Status stat = this->m_file.open(searchFilename.toChar(), Os::File::OPEN_CREATE, Os::File::OverwriteType::OVERWRITE);
124 
125  // Bad status when trying to open the file:
126  if (stat != Os::File::OP_OK) {
127  return false;
128  }
129 
130  this->m_currentFileSize = 0;
131  this->m_maxFileSize = maxSize;
132  this->m_fileName = searchFilename;
133  this->m_openFile = true;
134 
135  return true;
136  }
137 
138 
139 } // namespace Svc
LogFile()
Constructor.
Definition: LogFile.cpp:22
Os::FileInterface::Status open(const char *path, Mode mode)
open file with supplied path and mode
bool m_openFile
Definition: LogFile.hpp:67
const char * toChar() const
U32 m_currentFileSize
Definition: LogFile.hpp:70
Do wait for read/write operation to finish.
Definition: File.hpp:58
U32 m_maxFileSize
Definition: LogFile.hpp:64
void close() override
close the file, if not opened then do nothing
Definition: File.cpp:70
Os::File m_file
Definition: LogFile.hpp:61
FormatStatus format(const CHAR *formatString,...)
write formatted string to buffer
Definition: StringBase.cpp:55
bool set_log_file(const char *fileName, const U32 maxSize, const U32 maxBackups=10)
Set log file and max size.
Definition: LogFile.cpp:81
file hasn&#39;t been opened yet
Definition: File.hpp:35
Operation was successful.
Definition: File.hpp:30
PlatformSignedSizeType FwSignedSizeType
Definition: FpConfig.h:30
static Status getFileSize(const char *path, FwSignedSizeType &size)
Get the size of the file (in bytes) at the specified path.
Definition: FileSystem.cpp:227
Operation was successful.
Definition: FileSystem.hpp:25
~LogFile()
Destructor.
Definition: LogFile.cpp:28
bool write_to_log(const char *const buf, const U32 size)
Write the passed buf to the log if possible.
Definition: LogFile.cpp:40
Status write(const U8 *buffer, FwSignedSizeType &size)
write data to this file from the supplied buffer bounded by size
Definition: File.cpp:163
#define FW_ASSERT(...)
Definition: Assert.hpp:14
Fw::FileNameString m_fileName
Definition: LogFile.hpp:58
FormatStatus
status of string format calls
Definition: format.hpp:18
Open file for writing and truncates file if it exists, ie same flags as creat()
Definition: File.hpp:22