F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
ComLogger.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------
2 //
3 // ComLogger.cpp
4 //
5 // ----------------------------------------------------------------------
6 
10 #include <Os/ValidateFile.hpp>
12 #include <cstdio>
13 
14 namespace Svc {
15 static_assert(std::numeric_limits<U16>::max() <= std::numeric_limits<FwSizeType>::max(),
16  "U16 must fit in the positive range of FwSizeType");
17 // ----------------------------------------------------------------------
18 // Construction, initialization, and destruction
19 // ----------------------------------------------------------------------
20 
21 ComLogger ::ComLogger(const char* compName, const char* incomingFilePrefix, U32 maxFileSize, bool storeBufferLength)
22  : ComLoggerComponentBase(compName),
23  m_maxFileSize(maxFileSize),
24  m_fileMode(CLOSED),
25  m_byteCount(0),
26  m_writeErrorOccurred(false),
27  m_openErrorOccurred(false),
28  m_storeBufferLength(storeBufferLength),
29  m_initialized(true) {
30  this->init_log_file(incomingFilePrefix, maxFileSize, storeBufferLength);
31 }
32 
33 ComLogger ::ComLogger(const char* compName)
34  : ComLoggerComponentBase(compName),
35  m_filePrefix(),
36  m_maxFileSize(0),
37  m_fileMode(CLOSED),
38  m_fileName(),
39  m_hashFileName(),
40  m_byteCount(0),
41  m_writeErrorOccurred(false),
42  m_openErrorOccurred(false),
43  m_storeBufferLength(),
44  m_initialized(false) {}
45 
46 void ComLogger ::init_log_file(const char* incomingFilePrefix, U32 maxFileSize, bool storeBufferLength) {
47  FW_ASSERT(incomingFilePrefix != nullptr);
48  this->m_maxFileSize = maxFileSize;
49  this->m_storeBufferLength = storeBufferLength;
50  if (this->m_storeBufferLength) {
51  FW_ASSERT(maxFileSize > sizeof(U16), static_cast<FwAssertArgType>(maxFileSize));
52  }
53  // Assign the prefix checking if it is too big
54  Fw::FormatStatus formatStatus = this->m_filePrefix.format("%s", incomingFilePrefix);
55  FW_ASSERT(formatStatus == Fw::FormatStatus::SUCCESS);
56  this->m_initialized = true;
57 }
58 
60  // Close file:
61  // this->closeFile();
62  // NOTE: the above did not work because we don't want to issue an event
63  // in the destructor. This can cause "virtual method called" segmentation
64  // faults.
65  // So I am copying part of that function here.
66  if (OPEN == this->m_fileMode) {
67  // Close file:
68  this->m_file.close();
69 
70  // Write out the hash file to disk:
71  this->writeHashFile();
72 
73  // Update mode:
74  this->m_fileMode = CLOSED;
75 
76  // Send event:
77  // Fw::LogStringArg logStringArg((char*) fileName);
78  // this->log_DIAGNOSTIC_FileClosed(logStringArg);
79  }
80 }
81 
82 // ----------------------------------------------------------------------
83 // Handler implementations
84 // ----------------------------------------------------------------------
85 
86 void ComLogger ::comIn_handler(FwIndexType portNum, Fw::ComBuffer& data, U32 context) {
87  FW_ASSERT(portNum == 0);
88 
89  // Get length of buffer:
90  FwSizeType sizeNative = data.getBuffLength();
91  // ComLogger only writes 16-bit sizes to save space
92  // on disk:
93  FW_ASSERT(sizeNative < 65536, static_cast<FwAssertArgType>(sizeNative));
94  U16 size = sizeNative & 0xFFFF;
95 
96  // Close the file if it will be too big:
97  if (OPEN == this->m_fileMode) {
98  U32 projectedByteCount = this->m_byteCount + size;
99  if (this->m_storeBufferLength) {
100  projectedByteCount += static_cast<U32>(sizeof(size));
101  }
102  if (projectedByteCount > this->m_maxFileSize) {
103  this->closeFile();
104  }
105  }
106 
107  // Open the file if it there is not one open:
108  if (CLOSED == this->m_fileMode) {
109  this->openFile();
110  }
111 
112  // Write to the file if it is open:
113  if (OPEN == this->m_fileMode) {
114  this->writeComBufferToFile(data, size);
115  }
116 }
117 
118 void ComLogger ::CloseFile_cmdHandler(FwOpcodeType opCode, U32 cmdSeq) {
119  this->closeFile();
120  this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::OK);
121 }
122 
123 void ComLogger ::pingIn_handler(const FwIndexType portNum, U32 key) {
124  // return key
125  this->pingOut_out(0, key);
126 }
127 
128 void ComLogger ::openFile() {
129  FW_ASSERT(CLOSED == this->m_fileMode);
130 
131  if (!this->m_initialized) {
133  return;
134  }
135 
136  // Create filename:
137  Fw::Time timestamp = getTime();
138  Fw::FormatStatus formatStatus = this->m_fileName.format(
139  "%s_%" PRI_FwTimeBaseStoreType "_%" PRIu32 "_%06" PRIu32 ".com", this->m_filePrefix.toChar(),
140  static_cast<FwTimeBaseStoreType>(timestamp.getTimeBase()), timestamp.getSeconds(), timestamp.getUSeconds());
141  FW_ASSERT(formatStatus == Fw::FormatStatus::SUCCESS);
142  this->m_hashFileName.format("%s%s", this->m_fileName.toChar(), Utils::Hash::getFileExtensionString());
143  FW_ASSERT(formatStatus == Fw::FormatStatus::SUCCESS);
144 
145  Os::File::Status ret = m_file.open(this->m_fileName.toChar(), Os::File::OPEN_WRITE);
146  if (Os::File::OP_OK != ret) {
147  if (!this->m_openErrorOccurred) { // throttle this event, otherwise a positive
148  // feedback event loop can occur!
149  this->log_WARNING_HI_FileOpenError(ret, this->m_fileName);
150  }
151  this->m_openErrorOccurred = true;
152  } else {
153  // Reset event throttle:
154  this->m_openErrorOccurred = false;
155 
156  // Reset byte count:
157  this->m_byteCount = 0;
158 
159  // Set mode:
160  this->m_fileMode = OPEN;
161  }
162 }
163 
164 void ComLogger ::closeFile() {
165  if (OPEN == this->m_fileMode) {
166  // Close file:
167  this->m_file.close();
168 
169  // Write out the hash file to disk:
170  this->writeHashFile();
171 
172  // Update mode:
173  this->m_fileMode = CLOSED;
174 
175  // Send event:
176  this->log_DIAGNOSTIC_FileClosed(this->m_fileName);
177  }
178 }
179 
180 void ComLogger ::writeComBufferToFile(Fw::ComBuffer& data, U16 size) {
181  if (this->m_storeBufferLength) {
182  U8 buffer[sizeof(size)];
183  Fw::SerialBuffer serialLength(&buffer[0], sizeof(size));
184  serialLength.serialize(size);
185  if (this->writeToFile(serialLength.getBuffAddr(), static_cast<U16>(serialLength.getBuffLength()))) {
186  this->m_byteCount += static_cast<U32>(serialLength.getBuffLength());
187  } else {
188  return;
189  }
190  }
191 
192  // Write buffer to file:
193  if (this->writeToFile(data.getBuffAddr(), size)) {
194  this->m_byteCount += size;
195  }
196 }
197 
198 bool ComLogger ::writeToFile(void* data, U16 length) {
199  FwSizeType size = length;
200  Os::File::Status ret = m_file.write(reinterpret_cast<const U8*>(data), size);
201  if ((Os::File::OP_OK != ret) || (size != length)) {
202  if (!this->m_writeErrorOccurred) { // throttle this event, otherwise a positive
203  // feedback event loop can occur!
204  this->log_WARNING_HI_FileWriteError(ret, static_cast<U32>(size), length, this->m_fileName);
205  }
206  this->m_writeErrorOccurred = true;
207  return false;
208  }
209 
210  this->m_writeErrorOccurred = false;
211  return true;
212 }
213 
214 void ComLogger ::writeHashFile() {
215  Os::ValidateFile::Status validateStatus;
216  validateStatus = Os::ValidateFile::createValidation(this->m_fileName.toChar(), this->m_hashFileName.toChar());
217  if (Os::ValidateFile::VALIDATION_OK != validateStatus) {
218  this->log_WARNING_LO_FileValidationError(this->m_fileName, this->m_hashFileName, validateStatus);
219  }
220 }
221 } // namespace Svc
A variable-length serializable buffer.
FwIdType FwOpcodeType
The type of a command opcode.
PlatformSizeType FwSizeType
void log_WARNING_LO_FileNotInitialized()
Log event FileNotInitialized.
void log_DIAGNOSTIC_FileClosed(const Fw::StringBase &file) const
Open file for writing.
Definition: File.hpp:33
TimeBase getTimeBase() const
Definition: Time.cpp:94
Os::FileInterface::Status open(const char *path, Mode mode)
open file with supplied path and mode
void init_log_file(const char *filePrefix, U32 maxFileSize, bool storeBufferLength=true)
Definition: ComLogger.cpp:46
void pingOut_out(FwIndexType portNum, U32 key)
Invoke output port pingOut.
The validation of the file passed.
Serializable::SizeType getBuffLength() const
returns current buffer size
const char * toChar() const
void log_WARNING_HI_FileOpenError(U32 errornum, const Fw::StringBase &file) const
U32 getSeconds() const
Definition: Time.cpp:86
void cmdResponse_out(FwOpcodeType opCode, U32 cmdSeq, Fw::CmdResponse response)
Emit command response.
U8 * getBuffAddr()
gets buffer address for data filling
Definition: ComBuffer.cpp:38
void log_WARNING_HI_FileWriteError(U32 errornum, U32 bytesWritten, U32 bytesToWrite, const Fw::StringBase &file) const
void close() override
close the file, if not opened then do nothing
Definition: File.cpp:70
Status write(const U8 *buffer, FwSizeType &size)
write data to this file from the supplied buffer bounded by size
Definition: File.cpp:187
static const char * getFileExtensionString()
Definition: HashCommon.cpp:6
#define PRI_FwTimeBaseStoreType
FormatStatus format(const CHAR *formatString,...)
write formatted string to buffer
Definition: StringBase.cpp:55
Command successfully executed.
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:53
Status createValidation(const char *fileName, const char *hash, Utils::HashBuffer &hashBuffer)
void log_WARNING_LO_FileValidationError(const Fw::StringBase &validationFile, const Fw::StringBase &file, U32 status) const
Operation was successful.
Definition: File.hpp:40
U32 getUSeconds() const
Definition: Time.cpp:90
Defines a file class to validate files or generate a file validator file.
PlatformIndexType FwIndexType
RateGroupDivider component implementation.
U16 FwTimeBaseStoreType
The type used to serialize a time base value.
ComLogger(const char *compName, const char *filePrefix, U32 maxFileSize, bool storeBufferLength=true)
Definition: ComLogger.cpp:21
#define FW_ASSERT(...)
Definition: Assert.hpp:14
Auto-generated base for ComLogger component.
FormatStatus
status of string format calls
Definition: format.hpp:18