F´ Flight Software - C/C++ Documentation  devel
A framework for building embedded system applications to NASA flight quality standards.
ActiveTextLogger.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 <Fw/Logger/Logger.hpp>
9 #include <ctime>
10 
11 namespace Svc {
12 
13  // ----------------------------------------------------------------------
14  // Initialization/Exiting
15  // ----------------------------------------------------------------------
16 
19  m_log_file()
20  {
21 
22  }
23 
25  {
26 
27  }
28 
30  {
31  ActiveTextLoggerComponentBase::init(queueDepth,instance);
32  }
33 
34  // ----------------------------------------------------------------------
35  // Handlers to implement for typed input ports
36  // ----------------------------------------------------------------------
37 
38  void ActiveTextLogger::TextLogger_handler(NATIVE_INT_TYPE portNum,
39  FwEventIdType id,
40  Fw::Time &timeTag,
41  const Fw::LogSeverity& severity,
42  Fw::TextLogString &text)
43  {
44 
45  // Currently not doing any input filtering
46  // TKC - 5/3/2018 - remove diagnostic
47  if (Fw::LogSeverity::DIAGNOSTIC == severity.e) {
48  return;
49  }
50 
51  // Format the string here, so that it is done in the task context
52  // of the caller. Format doc borrowed from PassiveTextLogger.
53  const char *severityString = "UNKNOWN";
54  switch (severity.e) {
56  severityString = "FATAL";
57  break;
59  severityString = "WARNING_HI";
60  break;
62  severityString = "WARNING_LO";
63  break;
65  severityString = "COMMAND";
66  break;
68  severityString = "ACTIVITY_HI";
69  break;
71  severityString = "ACTIVITY_LO";
72  break;
74  severityString = "DIAGNOSTIC";
75  break;
76  default:
77  severityString = "SEVERITY ERROR";
78  break;
79  }
80 
81  // TODO: Add calling task id to format string
83 
84  if (timeTag.getTimeBase() == TB_WORKSTATION_TIME) {
85 
86  time_t t = timeTag.getSeconds();
87  // Using localtime_r prevents any other calls to localtime (from another thread for example) from
88  // interfering with our time object before we use it. However, the null pointer check is still needed
89  // to ensure a successful call
90  tm tm;
91  if (localtime_r(&t, &tm) == nullptr) {
92  return;
93  }
94 
95  (void) snprintf(textStr,
97  "EVENT: (%" PRI_FwEventIdType ") (%04d-%02d-%02dT%02d:%02d:%02d.%06" PRIu32 ") %s: %s\n",
98  id, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour,
99  tm.tm_min,tm.tm_sec,timeTag.getUSeconds(),
100  severityString,text.toChar());
101  }
102  else {
103 
104  (void) snprintf(textStr,
106  "EVENT: (%" PRI_FwEventIdType ") (%" PRI_FwTimeBaseStoreType ":%" PRId32 ",%" PRId32 ") %s: %s\n",
107  id, static_cast<FwTimeBaseStoreType>(timeTag.getTimeBase()),timeTag.getSeconds(),timeTag.getUSeconds(),severityString,text.toChar());
108  }
109 
110  // Call internal interface so that everything else is done on component thread,
111  // this helps ensure consistent ordering of the printed text:
112  Fw::InternalInterfaceString intText(textStr);
113  this->TextQueue_internalInterfaceInvoke(intText);
114  }
115 
116  // ----------------------------------------------------------------------
117  // Internal interface handlers
118  // ----------------------------------------------------------------------
119 
120  void ActiveTextLogger::TextQueue_internalInterfaceHandler(const Fw::InternalInterfaceString& text)
121  {
122 
123  // Print to console:
124  Fw::Logger::logMsg(text.toChar(),0,0,0,0,0,0,0,0,0);
125 
126  // Print to file if there is one:
127  (void) this->m_log_file.write_to_log(text.toChar(), text.length()); // Ignoring return status
128 
129  }
130 
131  // ----------------------------------------------------------------------
132  // Helper Methods
133  // ----------------------------------------------------------------------
134 
135  bool ActiveTextLogger::set_log_file(const char* fileName, const U32 maxSize, const U32 maxBackups)
136  {
137  FW_ASSERT(fileName != nullptr);
138 
139  return this->m_log_file.set_log_file(fileName, maxSize, maxBackups);
140  }
141 
142 
143 } // namespace Svc
#define FW_ASSERT(...)
Definition: Assert.hpp:14
PlatformIntType NATIVE_INT_TYPE
Definition: BasicTypes.h:51
@ TB_WORKSTATION_TIME
Time as reported on workstation where software is running. For testing.
Definition: FpConfig.h:59
U32 FwEventIdType
Definition: FpConfig.h:86
U16 FwTimeBaseStoreType
Definition: FpConfig.h:66
#define PRI_FwEventIdType
Definition: FpConfig.h:87
#define PRI_FwTimeBaseStoreType
Definition: FpConfig.h:67
#define FW_INTERNAL_INTERFACE_STRING_MAX_SIZE
Max size of interface string parameter type.
Definition: FpConfig.h:330
Enum representing event severity.
T e
The raw enum value.
@ WARNING_HI
A serious but recoverable event.
@ ACTIVITY_HI
Important informational events.
@ FATAL
A fatal non-recoverable event.
@ WARNING_LO
A less serious but recoverable event.
@ DIAGNOSTIC
Software diagnostic events.
@ ACTIVITY_LO
Less important informational events.
@ COMMAND
An activity related to commanding.
static void logMsg(const char *fmt, POINTER_CAST a0=0, POINTER_CAST a1=0, POINTER_CAST a2=0, POINTER_CAST a3=0, POINTER_CAST a4=0, POINTER_CAST a5=0, POINTER_CAST a6=0, POINTER_CAST a7=0, POINTER_CAST a8=0, POINTER_CAST a9=0)
Definition: Logger.cpp:18
void init()
Object initializer.
Definition: ObjBase.cpp:27
SizeType length() const
Get length of string.
Definition: StringBase.cpp:116
const char * toChar() const
Definition: Time.hpp:9
U32 getUSeconds() const
Definition: Time.cpp:139
TimeBase getTimeBase() const
Definition: Time.cpp:143
U32 getSeconds() const
Definition: Time.cpp:135
Auto-generated base for ActiveTextLogger component.
void TextQueue_internalInterfaceInvoke(const Fw::InternalInterfaceString &text)
Internal interface base-class function for TextQueue.
ActiveTextLogger(const char *compName)
Component constructor.
virtual ~ActiveTextLogger()
Component destructor.
bool set_log_file(const char *fileName, const U32 maxSize, const U32 maxBackups=10)
Set log file and max size.
bool set_log_file(const char *fileName, const U32 maxSize, const U32 maxBackups=10)
Set log file and max size.
Definition: LogFile.cpp:81
bool write_to_log(const char *const buf, const U32 size)
Write the passed buf to the log if possible.
Definition: LogFile.cpp:40