F´ Flight Software - C/C++ Documentation  NASA-v2.1.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
ActiveTextLoggerImpl.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 <time.h>
10 
11 namespace Svc {
12 
13  // ----------------------------------------------------------------------
14  // Initialization/Exiting
15  // ----------------------------------------------------------------------
16 
18  ActiveTextLoggerComponentBase(name),
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 ActiveTextLoggerComponentImpl::TextLogger_handler(NATIVE_INT_TYPE portNum,
39  FwEventIdType id,
40  Fw::Time &timeTag,
41  Fw::TextLogSeverity severity,
42  Fw::TextLogString &text)
43  {
44 
45  // Currently not doing any input filtering
46  // TKC - 5/3/2018 - remove diagnostic
47  if (Fw::TEXT_LOG_DIAGNOSTIC == severity) {
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) {
55  case Fw::TEXT_LOG_FATAL:
56  severityString = "FATAL";
57  break;
58  case Fw::TEXT_LOG_WARNING_HI:
59  severityString = "WARNING_HI";
60  break;
61  case Fw::TEXT_LOG_WARNING_LO:
62  severityString = "WARNING_LO";
63  break;
64  case Fw::TEXT_LOG_COMMAND:
65  severityString = "COMMAND";
66  break;
67  case Fw::TEXT_LOG_ACTIVITY_HI:
68  severityString = "ACTIVITY_HI";
69  break;
70  case Fw::TEXT_LOG_ACTIVITY_LO:
71  severityString = "ACTIVITY_LO";
72  break;
73  case Fw::TEXT_LOG_DIAGNOSTIC:
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  NATIVE_INT_TYPE stat;
84 
85  if (timeTag.getTimeBase() == TB_WORKSTATION_TIME) {
86 
87  time_t t = timeTag.getSeconds();
88  // Using localtime_r prevents any other calls to localtime (from another thread for example) from
89  // interfering with our time object before we use it. However, the null pointer check is still needed
90  // to ensure a successful call
91  tm tm;
92  if (localtime_r(&t, &tm) == NULL) {
93  return;
94  }
95 
96  stat = snprintf(textStr,
98  "EVENT: (%d) (%04d-%02d-%02dT%02d:%02d:%02d.%03u) %s: %s\n",
99  id, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour,
100  tm.tm_min,tm.tm_sec,timeTag.getUSeconds(),
101  severityString,text.toChar());
102  }
103  else {
104 
105  stat = snprintf(textStr,
107  "EVENT: (%d) (%d:%d,%d) %s: %s\n",
108  id,timeTag.getTimeBase(),timeTag.getSeconds(),timeTag.getUSeconds(),severityString,text.toChar());
109  }
110 
111  // If there was an error then just return:
112  if (stat <= 0) {
113  return;
114  }
115  // If there was string text truncation:
116  else if (stat >= FW_INTERNAL_INTERFACE_STRING_MAX_SIZE) {
117  // Do nothing
118  }
119 
120  // Call internal interface so that everything else is done on component thread,
121  // this helps ensure consistent ordering of the printed text:
122  Fw::InternalInterfaceString intText(textStr);
123  this->TextQueue_internalInterfaceInvoke(intText);
124  }
125 
126  // ----------------------------------------------------------------------
127  // Internal interface handlers
128  // ----------------------------------------------------------------------
129 
130  void ActiveTextLoggerComponentImpl::TextQueue_internalInterfaceHandler(const Fw::InternalInterfaceString& text)
131  {
132 
133  // Print to console:
134  Fw::Logger::logMsg(text.toChar(),0,0,0,0,0,0,0,0,0);
135 
136  // Print to file if there is one:
137  (void) this->m_log_file.write_to_log(text.toChar(), text.length()); // Ignoring return status
138 
139  }
140 
141  // ----------------------------------------------------------------------
142  // Helper Methods
143  // ----------------------------------------------------------------------
144 
145  bool ActiveTextLoggerComponentImpl::set_log_file(const char* fileName, const U32 maxSize, const U32 maxBackups)
146  {
147  FW_ASSERT(fileName != NULL);
148 
149  return this->m_log_file.set_log_file(fileName, maxSize, maxBackups);
150  }
151 
152 
153 } // namespace Svc
Fw::InternalInterfaceString
Definition: InternalInterfaceString.hpp:11
Fw::TextLogString
Definition: TextLogString.hpp:11
Fw::Time
Definition: Time.hpp:10
Fw::Time::getTimeBase
TimeBase getTimeBase(void) const
Definition: Time.cpp:142
Svc::LogFile::set_log_file
bool set_log_file(const char *fileName, const U32 maxSize, const U32 maxBackups=10)
Set log file and max size.
Definition: LogFile.cpp:80
Fw::Time::getUSeconds
U32 getUSeconds(void) const
Definition: Time.cpp:138
Svc::ActiveTextLoggerComponentImpl::ActiveTextLoggerComponentImpl
ActiveTextLoggerComponentImpl(const char *compName)
Component constructor.
Definition: ActiveTextLoggerImpl.cpp:17
Assert.hpp
Svc::ActiveTextLoggerComponentImpl::~ActiveTextLoggerComponentImpl
virtual ~ActiveTextLoggerComponentImpl()
Component destructor.
Definition: ActiveTextLoggerImpl.cpp:24
Fw::StringBase::length
NATIVE_UINT_TYPE length(void) const
Get length of string.
Definition: StringType.cpp:119
Fw::TextLogString::toChar
const char * toChar(void) const
Definition: TextLogString.cpp:39
FW_ASSERT
#define FW_ASSERT(...)
Definition: Assert.hpp:9
Svc::ActiveTextLoggerComponentImpl::set_log_file
bool set_log_file(const char *fileName, const U32 maxSize, const U32 maxBackups=10)
Set log file and max size.
Definition: ActiveTextLoggerImpl.cpp:145
ActiveTextLoggerImpl.hpp
Fw::Time::getSeconds
U32 getSeconds(void) const
Definition: Time.cpp:134
Svc::LogFile::write_to_log
bool write_to_log(const char *const buf, const U32 size)
Write the passed buf to the log if possible.
Definition: LogFile.cpp:39
Fw::Logger::logMsg
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
Svc
Definition: ActiveRateGroupImplCfg.hpp:18
TB_WORKSTATION_TIME
@ TB_WORKSTATION_TIME
Time as reported on workstation where software is running. For testing.
Definition: FpConfig.hpp:330
Fw::InternalInterfaceString::toChar
const char * toChar(void) const
gets char buffer
Definition: InternalInterfaceString.cpp:40
FwEventIdType
#define FwEventIdType
Type representation for an event id.
Definition: FpConfig.hpp:70
FW_INTERNAL_INTERFACE_STRING_MAX_SIZE
#define FW_INTERNAL_INTERFACE_STRING_MAX_SIZE
Max size of interface string parameter type.
Definition: FpConfig.hpp:285
NATIVE_INT_TYPE
int NATIVE_INT_TYPE
native integer type declaration
Definition: BasicTypes.hpp:29
NULL
#define NULL
NULL.
Definition: BasicTypes.hpp:100
Logger.hpp
Svc::ActiveTextLoggerComponentImpl::init
void init(NATIVE_INT_TYPE queueDepth, NATIVE_INT_TYPE instance=0)
Component initialization routine.
Definition: ActiveTextLoggerImpl.cpp:29