F´ Flight Software - C/C++ Documentation
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 
6 #include <Fw/Logger/Logger.hpp>
7 #include <Fw/Types/Assert.hpp>
9 #include <ctime>
10 
11 namespace Svc {
12 static_assert(std::numeric_limits<FwSizeType>::max() >= ACTIVE_TEXT_LOGGER_ID_FILTER_SIZE,
13  "ACTIVE_TEXT_LOGGER_ID_FILTER_SIZE must fit within range of FwSizeType");
14 
15 // ----------------------------------------------------------------------
16 // Initialization/Exiting
17 // ----------------------------------------------------------------------
18 
20  : ActiveTextLoggerComponentBase(name), m_log_file(), m_numFilteredIDs(0), m_severityFilter() {
21  // Set severity filter defaults from config
28 }
29 
31 
32 void ActiveTextLogger::configure(const FwEventIdType* filteredIds, FwSizeType count) {
33  FW_ASSERT(filteredIds != nullptr || count == 0);
34  FW_ASSERT(count <= ACTIVE_TEXT_LOGGER_ID_FILTER_SIZE, static_cast<FwAssertArgType>(count),
36 
37  this->m_numFilteredIDs = count;
38  for (FwSizeType entry = 0; entry < count; entry++) {
39  this->m_filteredIDs[entry] = filteredIds[entry];
40  }
41 }
42 
44  this->m_severityFilter.setFilter(severity, enabled);
45 }
46 
47 // ----------------------------------------------------------------------
48 // Handlers to implement for typed input ports
49 // ----------------------------------------------------------------------
50 
51 void ActiveTextLogger::TextLogger_handler(FwIndexType portNum,
52  FwEventIdType id,
53  Fw::Time& timeTag,
54  const Fw::LogSeverity& severity,
55  Fw::TextLogString& text) {
56  // Check severity filter
57  if (this->m_severityFilter.isFiltered(severity)) {
58  return;
59  }
60 
61  // Check event ID filters
62  for (FwSizeType i = 0; i < this->m_numFilteredIDs; i++) {
63  if (this->m_filteredIDs[i] == id) {
64  return;
65  }
66  }
67 
68  // Format the string here, so that it is done in the task context
69  // of the caller. Format doc borrowed from PassiveTextLogger.
70  const char* severityString = nullptr;
71  switch (severity.e) {
73  severityString = "FATAL";
74  break;
76  severityString = "WARNING_HI";
77  break;
79  severityString = "WARNING_LO";
80  break;
82  severityString = "COMMAND";
83  break;
85  severityString = "ACTIVITY_HI";
86  break;
88  severityString = "ACTIVITY_LO";
89  break;
91  severityString = "DIAGNOSTIC";
92  break;
93  default:
94  severityString = "SEVERITY ERROR";
95  break;
96  }
97  FW_ASSERT(severityString != nullptr);
98  // Overflow is allowed and truncation accepted
100  (void)intText.format("EVENT: (%" PRI_FwEventIdType ") (%" PRI_FwTimeBaseStoreType ":%" PRIu32 ",%" PRIu32
101  ") %s: %s\n",
102  id, static_cast<FwTimeBaseStoreType>(timeTag.getTimeBase()), timeTag.getSeconds(),
103  timeTag.getUSeconds(), severityString, text.toChar());
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  this->TextQueue_internalInterfaceInvoke(intText);
108 }
109 
110 // ----------------------------------------------------------------------
111 // Internal interface handlers
112 // ----------------------------------------------------------------------
113 
114 void ActiveTextLogger::TextQueue_internalInterfaceHandler(const Fw::InternalInterfaceString& text) {
115  // Print to console:
116  Fw::Logger::log(text);
117 
118  // Print to file if there is one:
119  (void)this->m_log_file.write_to_log(text.toChar(), text.length()); // Ignoring return status
120 }
121 
122 // ----------------------------------------------------------------------
123 // Helper Methods
124 // ----------------------------------------------------------------------
125 
126 bool ActiveTextLogger::set_log_file(const char* fileName, const U32 maxSize, const U32 maxBackups) {
127  FW_ASSERT(fileName != nullptr);
128 
129  return this->m_log_file.set_log_file(fileName, maxSize, maxBackups);
130 }
131 
132 } // namespace Svc
bool write_to_log(const char *const buf, const FwSizeType size)
Write the passed buf to the log if possible.
Definition: LogFile.cpp:34
void configure(const FwEventIdType *filteredIds, FwSizeType count)
Configure component with event ID filters.
void setSeverityFilter(Fw::LogSeverity severity, bool enabled)
PlatformSizeType FwSizeType
static void log(const char *format,...)
log a formated string with supplied arguments
Definition: Logger.cpp:21
TimeBase getTimeBase() const
Definition: Time.cpp:136
ActiveTextLogger(const char *compName)
Component constructor.
void setFilter(Fw::LogSeverity severity, bool enabled)
Software diagnostic events.
#define PRI_FwEventIdType
FwIdType FwEventIdType
The type of an event identifier.
Less important informational events.
An activity related to commanding.
A less serious but recoverable event.
U32 getSeconds() const
Definition: Time.cpp:128
A serious but recoverable event.
virtual ~ActiveTextLogger()
Component destructor.
const char * toChar() const
Convert to a C-style char*.
bool set_log_file(const char *fileName, const FwSizeType maxSize, const FwSizeType maxBackups=10)
Set log file and max size.
Definition: LogFile.cpp:69
Enum representing event severity.
enum T e
The raw enum value.
Auto-generated base for ActiveTextLogger component.
#define PRI_FwTimeBaseStoreType
FormatStatus format(const CHAR *formatString,...)
write formatted string to buffer
Definition: StringBase.cpp:58
DIAGNOSTIC events are filtered out by default.
Important informational events.
U32 getUSeconds() const
Definition: Time.cpp:132
PlatformIndexType FwIndexType
bool set_log_file(const char *fileName, const U32 maxSize, const U32 maxBackups=10)
Set log file and max size.
A fatal non-recoverable event.
RateGroupDivider component implementation.
virtual SizeType length() const
Get the length of the string.
void TextQueue_internalInterfaceInvoke(const Fw::InternalInterfaceString &text)
Internal interface base-class function for TextQueue.
#define FW_ASSERT(...)
Definition: Assert.hpp:14
bool isFiltered(Fw::LogSeverity severity) const