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 
29  // ----------------------------------------------------------------------
30  // Handlers to implement for typed input ports
31  // ----------------------------------------------------------------------
32 
33  void ActiveTextLogger::TextLogger_handler(NATIVE_INT_TYPE portNum,
34  FwEventIdType id,
35  Fw::Time &timeTag,
36  const Fw::LogSeverity& severity,
37  Fw::TextLogString &text)
38  {
39 
40  // Currently not doing any input filtering
41  // TKC - 5/3/2018 - remove diagnostic
42  if (Fw::LogSeverity::DIAGNOSTIC == severity.e) {
43  return;
44  }
45 
46  // Format the string here, so that it is done in the task context
47  // of the caller. Format doc borrowed from PassiveTextLogger.
48  const char *severityString = "UNKNOWN";
49  switch (severity.e) {
51  severityString = "FATAL";
52  break;
54  severityString = "WARNING_HI";
55  break;
57  severityString = "WARNING_LO";
58  break;
60  severityString = "COMMAND";
61  break;
63  severityString = "ACTIVITY_HI";
64  break;
66  severityString = "ACTIVITY_LO";
67  break;
69  severityString = "DIAGNOSTIC";
70  break;
71  default:
72  severityString = "SEVERITY ERROR";
73  break;
74  }
75 
76  // TODO: Add calling task id to format string
78 
79  if (timeTag.getTimeBase() == TB_WORKSTATION_TIME) {
80 
81  time_t t = timeTag.getSeconds();
82  // Using localtime_r prevents any other calls to localtime (from another thread for example) from
83  // interfering with our time object before we use it. However, the null pointer check is still needed
84  // to ensure a successful call
85  tm tm;
86  if (localtime_r(&t, &tm) == nullptr) {
87  return;
88  }
89 
90  (void) snprintf(textStr,
92  "EVENT: (%" PRI_FwEventIdType ") (%04d-%02d-%02dT%02d:%02d:%02d.%06" PRIu32 ") %s: %s\n",
93  id, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour,
94  tm.tm_min,tm.tm_sec,timeTag.getUSeconds(),
95  severityString,text.toChar());
96  }
97  else {
98 
99  (void) snprintf(textStr,
101  "EVENT: (%" PRI_FwEventIdType ") (%" PRI_FwTimeBaseStoreType ":%" PRId32 ",%" PRId32 ") %s: %s\n",
102  id, static_cast<FwTimeBaseStoreType>(timeTag.getTimeBase()),timeTag.getSeconds(),timeTag.getUSeconds(),severityString,text.toChar());
103  }
104 
105  // Call internal interface so that everything else is done on component thread,
106  // this helps ensure consistent ordering of the printed text:
107  Fw::InternalInterfaceString intText(textStr);
108  this->TextQueue_internalInterfaceInvoke(intText);
109  }
110 
111  // ----------------------------------------------------------------------
112  // Internal interface handlers
113  // ----------------------------------------------------------------------
114 
115  void ActiveTextLogger::TextQueue_internalInterfaceHandler(const Fw::InternalInterfaceString& text)
116  {
117 
118  // Print to console:
119  Fw::Logger::log(text);
120 
121  // Print to file if there is one:
122  (void) this->m_log_file.write_to_log(text.toChar(), text.length()); // Ignoring return status
123 
124  }
125 
126  // ----------------------------------------------------------------------
127  // Helper Methods
128  // ----------------------------------------------------------------------
129 
130  bool ActiveTextLogger::set_log_file(const char* fileName, const U32 maxSize, const U32 maxBackups)
131  {
132  FW_ASSERT(fileName != nullptr);
133 
134  return this->m_log_file.set_log_file(fileName, maxSize, maxBackups);
135  }
136 
137 
138 } // namespace Svc
#define FW_ASSERT(...)
Definition: Assert.hpp:14
PlatformIntType NATIVE_INT_TYPE
Definition: BasicTypes.h:55
@ TB_WORKSTATION_TIME
Time as reported on workstation where software is running. For testing.
Definition: FpConfig.h:72
U32 FwEventIdType
Definition: FpConfig.h:103
U16 FwTimeBaseStoreType
Definition: FpConfig.h:79
#define PRI_FwEventIdType
Definition: FpConfig.h:104
#define PRI_FwTimeBaseStoreType
Definition: FpConfig.h:80
#define FW_INTERNAL_INTERFACE_STRING_MAX_SIZE
Max size of interface string parameter type.
Definition: FpConfig.h:349
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 log(const char *format,...)
log a formated string with supplied arguments
Definition: Logger.cpp:21
SizeType length() const
Get length of string.
Definition: StringBase.cpp:125
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