F´ Flight Software - C/C++ Documentation  NASA-v1.5.0
A framework for building embedded system applications to NASA flight quality standards.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
BufferLoggerFile.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title BufferLoggerFile.cpp
3 // \author bocchino, dinkel, mereweth
4 // \brief Implementation for Svc::BufferLogger::BufferLoggerFile
5 //
6 // \copyright
7 // Copyright (C) 2015-2017 California Institute of Technology.
8 // ALL RIGHTS RESERVED. United States Government Sponsorship
9 // acknowledged.
10 //
11 // ======================================================================
12 
14 #include "Os/ValidateFile.hpp"
15 #include "Os/ValidatedFile.hpp"
16 
17 namespace Svc {
18 
19  // ----------------------------------------------------------------------
20  // Constructors and destructors
21  // ----------------------------------------------------------------------
22 
23  BufferLogger::File ::
24  File(
25  BufferLogger& bufferLogger
26  ) :
27  bufferLogger(bufferLogger),
28  prefix(""),
29  suffix(""),
30  baseName(""),
31  fileCounter(0),
32  maxSize(0),
33  sizeOfSize(0),
34  mode(Mode::CLOSED),
35  bytesWritten(0)
36  {
37  }
38 
39  BufferLogger::File ::
40  ~File(void)
41  {
42  this->close();
43  }
44 
45  // ----------------------------------------------------------------------
46  // Public functions
47  // ----------------------------------------------------------------------
48 
49  void BufferLogger::File ::
50  init(
51  const char *const logFilePrefix,
52  const char *const logFileSuffix,
53  const U32 maxFileSize,
54  const U8 sizeOfSize
55  )
56  {
57  //NOTE(mereweth) - only call this before opening the file
58  FW_ASSERT(this->mode == File::Mode::CLOSED);
59 
60  this->prefix = logFilePrefix;
61  this->suffix = logFileSuffix;
62  this->maxSize = maxFileSize;
63  this->sizeOfSize = sizeOfSize;
64 
65  FW_ASSERT(sizeOfSize <= sizeof(U32), sizeOfSize);
66  FW_ASSERT(maxSize > sizeOfSize, maxSize);
67  }
68 
69  void BufferLogger::File ::
70  setBaseName(
71  const Fw::EightyCharString& baseName
72  )
73  {
74  if (this->mode == File::Mode::OPEN) {
75  this->closeAndEmitEvent();
76  }
77  this->baseName = baseName;
78  this->fileCounter = 0;
79  this->open();
80  }
81 
82  void BufferLogger::File ::
83  logBuffer(
84  const U8 *const data,
85  const U32 size
86  )
87  {
88  // Close the file if it will be too big
89  if (this->mode == File::Mode::OPEN) {
90  const U32 projectedByteCount =
91  this->bytesWritten + this->sizeOfSize + size;
92  if (projectedByteCount > this->maxSize) {
93  this->closeAndEmitEvent();
94  }
95  }
96  // Open a file if necessary
97  if (this->mode == File::Mode::CLOSED) {
98  this->open();
99  }
100  // Write to the file if it is open
101  if (this->mode == File::Mode::OPEN) {
102  (void) this->writeBuffer(data, size);
103  }
104  }
105 
106  void BufferLogger::File ::
107  closeAndEmitEvent(void)
108  {
109  if (this->mode == File::Mode::OPEN) {
110  this->close();
111  Fw::LogStringArg logStringArg(this->name.toChar());
112  this->bufferLogger.log_DIAGNOSTIC_BL_LogFileClosed(logStringArg);
113  }
114  }
115 
116  // ----------------------------------------------------------------------
117  // Private functions
118  // ----------------------------------------------------------------------
119 
120  void BufferLogger::File ::
121  open(void)
122  {
123  FW_ASSERT(this->mode == File::Mode::CLOSED);
124 
125  // NOTE(mereweth) - check that file path has been set and that initLog has been called
126  if ((this->baseName.toChar()[0] == '\0') ||
127  (this->sizeOfSize > sizeof(U32)) ||
128  (this->maxSize <= this->sizeOfSize)) {
129  this->bufferLogger.log_WARNING_HI_BL_NoLogFileOpenInitError();
130  return;
131  }
132 
133  if (this->fileCounter == 0) {
134  this->name.format(
135  "%s%s%s",
136  this->prefix.toChar(),
137  this->baseName.toChar(),
138  this->suffix.toChar()
139  );
140  }
141  else {
142  this->name.format(
143  "%s%s%d%s",
144  this->prefix.toChar(),
145  this->baseName.toChar(),
146  this->fileCounter,
147  this->suffix.toChar()
148  );
149  }
150 
151  const Os::File::Status status = this->osFile.open(
152  this->name.toChar(),
154  );
155  if (status == Os::File::OP_OK) {
156  this->fileCounter++;
157  // Reset bytes written
158  this->bytesWritten = 0;
159  // Set mode
160  this->mode = File::Mode::OPEN;
161  }
162  else {
163  Fw::LogStringArg string(this->name.toChar());
164  this->bufferLogger.log_WARNING_HI_BL_LogFileOpenError(status, string);
165  }
166  }
167 
168  bool BufferLogger::File ::
169  writeBuffer(
170  const U8 *const data,
171  const U32 size
172  )
173  {
174  bool status = this->writeSize(size);
175  if (status) {
176  status = this->writeBytes(data, size);
177  }
178  return status;
179  }
180 
181  bool BufferLogger::File ::
182  writeSize(const U32 size)
183  {
184  U32 sizeRegister = size;
185  U8 sizeBuffer[this->sizeOfSize];
186  for (U8 i = 0; i < this->sizeOfSize; ++i) {
187  sizeBuffer[this->sizeOfSize - i - 1] = sizeRegister & 0xFF;
188  sizeRegister >>= 8;
189  }
190  const bool status = this->writeBytes(
191  sizeBuffer,
192  sizeof(sizeBuffer)
193  );
194  return status;
195  }
196 
197  bool BufferLogger::File ::
198  writeBytes(
199  const void *const data,
200  const NATIVE_UINT_TYPE length
201  )
202  {
203  FW_ASSERT(length > 0, length);
204  NATIVE_INT_TYPE size = length;
205  const Os::File::Status fileStatus = this->osFile.write(data, size);
206  bool status;
207  if (fileStatus == Os::File::OP_OK && size == static_cast<NATIVE_INT_TYPE>(length)) {
208  this->bytesWritten += length;
209  status = true;
210  }
211  else {
212  Fw::LogStringArg string(this->name.toChar());
213 
214  this->bufferLogger.log_WARNING_HI_BL_LogFileWriteError(fileStatus, size, length, string);
215  status = false;
216  }
217  return status;
218  }
219 
220  void BufferLogger::File ::
221  writeHashFile(void)
222  {
223  Os::ValidatedFile validatedFile(this->name.toChar());
224  const Os::ValidateFile::Status status =
225  validatedFile.createHashFile();
226  if (status != Os::ValidateFile::VALIDATION_OK) {
227  const Fw::EightyCharString &hashFileName = validatedFile.getHashFileName();
228  Fw::LogStringArg logStringArg(hashFileName.toChar());
229  this->bufferLogger.log_WARNING_HI_BL_LogFileValidationError(
230  logStringArg,
231  status
232  );
233  }
234  }
235 
236  bool BufferLogger::File ::
237  flush(void)
238  {
239  return true;
240  // NOTE(if your fprime uses buffered file I/O, re-enable this)
241  /*bool status = true;
242  if(this->mode == File::Mode::OPEN)
243  {
244  const Os::File::Status fileStatus = this->osFile.flush();
245  if(fileStatus == Os::File::OP_OK)
246  {
247  status = true;
248  }
249  else
250  {
251  status = false;
252  }
253  }
254  return status;*/
255  }
256 
257  void BufferLogger::File ::
258  close(void)
259  {
260  if (this->mode == File::Mode::OPEN) {
261  // Close file
262  this->osFile.close();
263  // Write out the hash file to disk
264  this->writeHashFile();
265  // Update mode
266  this->mode = File::Mode::CLOSED;
267  }
268  }
269 
270 }
Fw::EightyCharString::toChar
const char * toChar(void) const
gets char buffer
Definition: EightyCharString.cpp:34
BufferLogger.hpp
Os::ValidateFile::VALIDATION_OK
@ VALIDATION_OK
The validation of the file passed.
Definition: ValidateFile.hpp:29
Os::ValidateFile::Status
Status
Definition: ValidateFile.hpp:27
Fw::LogStringArg
Definition: LogString.hpp:11
U8
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.hpp:76
ValidatedFile.hpp
Fw::EightyCharString
Definition: EightyCharString.hpp:10
ValidateFile.hpp
Defines a file class to validate files or generate a file validator file.
Os::File::OPEN_WRITE
@ OPEN_WRITE
Open file for writing.
Definition: File.hpp:17
NATIVE_UINT_TYPE
unsigned int NATIVE_UINT_TYPE
native unsigned integer type declaration
Definition: BasicTypes.hpp:30
FW_ASSERT
#define FW_ASSERT(...)
Definition: Assert.hpp:9
Os::ValidatedFile
A validated file.
Definition: ValidatedFile.hpp:23
Svc
Definition: ActiveLoggerComponentAc.cpp:22
Os::File::Status
Status
Definition: File.hpp:24
Os::File::OP_OK
@ OP_OK
Operation was successful.
Definition: File.hpp:25
NATIVE_INT_TYPE
int NATIVE_INT_TYPE
native integer type declaration
Definition: BasicTypes.hpp:29