F´ Flight Software - C/C++ Documentation  devel
A framework for building embedded system applications to NASA flight quality standards.
ComLogger.hpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------
2 //
3 // ComLogger.hpp
4 //
5 // ----------------------------------------------------------------------
6 
7 #ifndef Svc_ComLogger_HPP
8 #define Svc_ComLogger_HPP
9 
11 #include <Os/File.hpp>
12 #include <Os/Mutex.hpp>
13 #include <Fw/Types/Assert.hpp>
14 #include <Utils/Hash/Hash.hpp>
15 
16 #include <limits.h>
17 #include <cstdio>
18 #include <cstdarg>
19 
20 // some limits.h don't have PATH_MAX
21 #ifdef PATH_MAX
22 #define COMLOGGER_PATH_MAX PATH_MAX
23 #else
24 #define COMLOGGER_PATH_MAX 255
25 #endif
26 
27 // some limits.h don't have NAME_MAX
28 #ifdef NAME_MAX
29 #define COMLOGGER_NAME_MAX NAME_MAX
30 #else
31 #define COMLOGGER_NAME_MAX 255
32 #endif
33 
34 namespace Svc {
35 
36  class ComLogger :
38  {
39  // ----------------------------------------------------------------------
40  // Construction, initialization, and destruction
41  // ----------------------------------------------------------------------
42 
43  public:
44 
45  // CONSTRUCTOR:
46  // filePrefix: string to prepend the file name with, ie. "thermal_telemetry"
47  // maxFileSize: the maximum size a file should reach before being closed and a new one opened
48  // storeBufferLength: if true, store the length of each com buffer before storing the buffer itself,
49  // otherwise just store the com buffer. false might be advantageous in a system
50  // where you can ensure that all buffers given to the ComLogger are the same size
51  // in which case you do not need the overhead. Or you store an id which you can
52  // match to an expected size on the ground during post processing.
53  ComLogger(const char* compName, const char* filePrefix, U32 maxFileSize, bool storeBufferLength=true);
54 
55  // CONSTRUCTOR:
56  ComLogger(const char* compName);
57 
58  // filePrefix: string to prepend the file name with, ie. "thermal_telemetry"
59  // maxFileSize: the maximum size a file should reach before being closed and a new one opened
60  // storeBufferLength: if true, store the length of each com buffer before storing the buffer itself,
61  // otherwise just store the com buffer. false might be advantageous in a system
62  // where you can ensure that all buffers given to the ComLogger are the same size
63  // in which case you do not need the overhead. Or you store an id which you can
64  // match to an expected size on the ground during post processing.
65  void init_log_file(const char* filePrefix, U32 maxFileSize, bool storeBufferLength=true);
66 
67  ~ComLogger();
68 
69  // ----------------------------------------------------------------------
70  // Handler implementations
71  // ----------------------------------------------------------------------
72 
73  PRIVATE:
74 
75  void comIn_handler(
76  NATIVE_INT_TYPE portNum,
77  Fw::ComBuffer &data,
78  U32 context
79  );
80 
81  void CloseFile_cmdHandler(
82  FwOpcodeType opCode,
83  U32 cmdSeq
84  );
85 
88  void pingIn_handler(
89  const NATIVE_INT_TYPE portNum,
90  U32 key
91  );
92 
93  // ----------------------------------------------------------------------
94  // Constants:
95  // ----------------------------------------------------------------------
96  // The maximum size of a filename
97  enum {
98  MAX_FILENAME_SIZE = COMLOGGER_NAME_MAX,
99  MAX_PATH_SIZE = COMLOGGER_PATH_MAX
100  };
101 
102  // The filename data:
103  CHAR m_filePrefix[MAX_FILENAME_SIZE + MAX_PATH_SIZE];
104  U32 m_maxFileSize;
105 
106  // ----------------------------------------------------------------------
107  // Internal state:
108  // ----------------------------------------------------------------------
109  enum FileMode {
110  CLOSED = 0,
111  OPEN = 1
112  };
113 
114  FileMode m_fileMode;
115  Os::File m_file;
116  CHAR m_fileName[MAX_FILENAME_SIZE + MAX_PATH_SIZE];
117  CHAR m_hashFileName[MAX_FILENAME_SIZE + MAX_PATH_SIZE];
118  U32 m_byteCount;
119  bool m_writeErrorOccurred;
120  bool m_openErrorOccurred;
121  bool m_storeBufferLength;
122  bool m_initialized;
123 
124  // ----------------------------------------------------------------------
125  // File functions:
126  // ----------------------------------------------------------------------
127  void openFile(
128  );
129 
130  void closeFile(
131  );
132 
133  void writeComBufferToFile(
134  Fw::ComBuffer &data,
135  U16 size
136  );
137 
138  // ----------------------------------------------------------------------
139  // Helper functions:
140  // ----------------------------------------------------------------------
141 
142  bool writeToFile(
143  void* data,
144  U16 length
145  );
146 
147  void writeHashFile(
148  );
149  };
150 }
151 
152 #endif
PlatformIntType NATIVE_INT_TYPE
Definition: BasicTypes.h:55
char CHAR
Definition: BasicTypes.h:32
#define COMLOGGER_PATH_MAX
Definition: ComLogger.hpp:24
#define COMLOGGER_NAME_MAX
Definition: ComLogger.hpp:31
U32 FwOpcodeType
Definition: FpConfig.h:91
Auto-generated base for ComLogger component.
void init_log_file(const char *filePrefix, U32 maxFileSize, bool storeBufferLength=true)
Definition: ComLogger.cpp:51
ComLogger(const char *compName, const char *filePrefix, U32 maxFileSize, bool storeBufferLength=true)
Definition: ComLogger.cpp:21