API
logPriority.hpp
Go to the documentation of this file.
1 /** \file logPriority.hpp
2  * \brief The MagAO-X logger log priority levels.
3  * \author Jared R. Males (jaredmales@gmail.com)
4  *
5  * \ingroup flatlogs_files
6  *
7  * History:
8  * - 2017-07-22 created by JRM
9  * - 2018-08-17 renamed and moved to flatlogs
10  */
11 #ifndef flatlogs_logPriority_hpp
12 #define flatlogs_logPriority_hpp
13 
14 #include <mx/ioutils/stringUtils.hpp>
15 
16 #include "logDefs.hpp"
17 
18 namespace flatlogs
19 {
20 
21 /// The log priority codes. These control if logs are stored on disk and how they are presented to users.
22 /** This is a scoping namespace for log priority codes.
23  * We do not use the enum class feature since it does not have automatic integer conversion.
24  * \ingroup logPrio
25  */
26 namespace logPrio
27 {
28 
29 
30  /// Normal operations of the entire system should be shut down immediately.
31  constexpr static logPrioT LOG_EMERGENCY = 0;
32 
33  /// This should only be used if some action is required by operators to keep the system safe.
34  constexpr static logPrioT LOG_ALERT = 1;
35 
36  /// The process can not continue and will shut down (fatal)
37  constexpr static logPrioT LOG_CRITICAL = 2;
38 
39  /// An error has occured which the software will attempt to correct.
40  constexpr static logPrioT LOG_ERROR = 3;
41 
42  /// A condition has occurred which may become an error, but the process continues.
43  constexpr static logPrioT LOG_WARNING = 4;
44 
45  /// A normal but significant condition
46  constexpr static logPrioT LOG_NOTICE = 5;
47 
48  /// Informational. The info log level is the lowest level recorded during normal operations.
49  constexpr static logPrioT LOG_INFO = 6;
50 
51  /// Used for debugging
52  constexpr static logPrioT LOG_DEBUG = 7;
53 
54  /// Used for debugging, providing a 2nd level.
55  constexpr static logPrioT LOG_DEBUG2 = 8;
56 
57  /// A telemetry recording
58  constexpr static logPrioT LOG_TELEM = 64;
59 
60  /// Used to denote "use the default level for this log type".
61  constexpr static logPrioT LOG_DEFAULT = std::numeric_limits<logPrioT>::max() - 1;
62 
63  /// Used to denote an unkown log type for internal error handling.
64  constexpr static logPrioT LOG_UNKNOWN = std::numeric_limits<logPrioT>::max();
65 
66 };
67 
68 ///Get the string representation of a log priority
69 /** \ingroup logPrio
70  */
71 inline
72 std::string priorityString( logPrioT & prio /**< [in] the log priority */)
73 {
74  switch( prio )
75  {
77  return "EMER";
78  case logPrio::LOG_ALERT:
79  return "ALRT";
81  return "CRIT";
82  case logPrio::LOG_ERROR:
83  return "ERR ";
85  return "WARN";
87  return "NOTE";
88  case logPrio::LOG_INFO:
89  return "INFO";
90  case logPrio::LOG_DEBUG:
91  return "DBG ";
93  return "DBG2";
95  return "DEF?";
96  case logPrio::LOG_TELEM:
97  return "TELM";
98  default:
99  return "UNK?";
100  }
101 }
102 
103 ///Get the log priority from a string, which might have the number or the name
104 /** Evaluates the input string to find the closest match to the log level names.
105  * If the first non-whitespace character is a digit, then the string is treated as an integer
106  * and converted. If the first non-whitespace character is not a digit, then the string is
107  * converted to upper case and the log level is
108  * determined using the minimum number of characters. That is
109  * - EM = EMERGENCY
110  * - A = ALERT
111  * - C = CRITICAL
112  * - ER = ERROR
113  * - W = WARNING
114  * - N = NOTICE
115  * - I = INFO, returns 3
116  * - D, D1, DBG, DBG1, DEBUG = DEBUG
117  * - D2, DBG2, DEBUG2 = DEBUG2
118  * - DEF = DEFAULT
119  * - T = TELEM
120  *
121  * \returns the log priority value if parsing is successful.
122  * \returns logPrio::LOG_UNKNOWN if parsing is not successful.
123  *
124  * \ingroup loglevels
125  */
126 inline
127 logPrioT logLevelFromString( const std::string & str )
128 {
129  std::string s = str;
130 
131  //Remove all whitespace
132  s.erase(std::remove_if(s.begin(), s.end(), ::isspace), s.end());
133 
134  if(s.size()==0) return logPrio::LOG_UNKNOWN;
135 
136  if( isdigit(s[0]) )
137  {
138  logPrioT l = atoi( s.c_str());
139 
140  return l;
141  }
142 
143  //Convert to upper case
144  for(size_t i=0; i< s.size(); ++i) s[i] = ::toupper(s[i]);
145 
146  if(s[0] == 'A') return logPrio::LOG_ALERT;
147  if(s[0] == 'C') return logPrio::LOG_CRITICAL;
148  if(s[0] == 'W') return logPrio::LOG_WARNING;
149  if(s[0] == 'N') return logPrio::LOG_NOTICE;
150  if(s[0] == 'I') return logPrio::LOG_INFO;
151 
152  if(s[0] == 'E')
153  {
154  if(s.size() == 1) return logPrio::LOG_UNKNOWN;
155 
156  if(s[1] == 'M') return logPrio::LOG_EMERGENCY;
157  if(s[2] == 'R') return logPrio::LOG_ERROR;
158 
159  return logPrio::LOG_UNKNOWN;
160  }
161 
162  if(s[0] == 'D')
163  {
164  //Accept D by itself for DEBUG
165  if(s.size()==1) return logPrio::LOG_DEBUG;
166 
167  //We accept D1 or D2
168  if(s[1] == '1') return logPrio::LOG_DEBUG;
169  if(s[1] == '2') return logPrio::LOG_DEBUG2;
170 
171  if(s.size()<3) return logPrio::LOG_UNKNOWN;
172 
173  //We accept DBG and DBG2
174  if( s[1] == 'B' && s[2] == 'G' )
175  {
176  if(s.size() == 3) return logPrio::LOG_DEBUG;
177 
178  if(s.size() > 3)
179  {
180  if( s[3] == '2') return logPrio::LOG_DEBUG2;
181  }
182 
183  return logPrio::LOG_UNKNOWN;
184  }
185 
186  //Anything that starts with DEF
187  if (s[1] == 'E' && s[2] == 'F') return logPrio::LOG_DEFAULT;
188 
189  //Now check for DEBUG and DEBUG2
190  if( s.size() >=5 )
191  {
192  if( s.substr(0,5) != "DEBUG" ) return logPrio::LOG_UNKNOWN;
193 
194  if(s.size() > 5 )
195  {
196  if( s[5] == '2') return logPrio::LOG_DEBUG2;
197 
198  return logPrio::LOG_UNKNOWN;
199  }
200 
201  return logPrio::LOG_DEBUG;
202  }
203 
204  }
205 
206  if(s[0] == 'T')
207  {
208  return logPrio::LOG_TELEM;
209  }
210 
211  return logPrio::LOG_UNKNOWN;
212 }
213 
214 }// namespace flatlogs
215 
216 #endif //flatlogs_logPriority_hpp
int8_t logPrioT
The type of the log priority code.
Definition: logDefs.hpp:21
std::string priorityString(logPrioT &prio)
Get the string representation of a log priority.
Definition: logPriority.hpp:72
Type definitions for the flatlogs format.
constexpr static logPrioT LOG_DEBUG
Used for debugging.
Definition: logPriority.hpp:52
constexpr static logPrioT LOG_CRITICAL
The process can not continue and will shut down (fatal)
Definition: logPriority.hpp:37
constexpr static logPrioT LOG_ERROR
An error has occured which the software will attempt to correct.
Definition: logPriority.hpp:40
constexpr static logPrioT LOG_INFO
Informational. The info log level is the lowest level recorded during normal operations.
Definition: logPriority.hpp:49
constexpr static logPrioT LOG_DEBUG2
Used for debugging, providing a 2nd level.
Definition: logPriority.hpp:55
constexpr static logPrioT LOG_ALERT
This should only be used if some action is required by operators to keep the system safe.
Definition: logPriority.hpp:34
constexpr static logPrioT LOG_UNKNOWN
Used to denote an unkown log type for internal error handling.
Definition: logPriority.hpp:64
constexpr static logPrioT LOG_EMERGENCY
Normal operations of the entire system should be shut down immediately.
Definition: logPriority.hpp:31
constexpr static logPrioT LOG_WARNING
A condition has occurred which may become an error, but the process continues.
Definition: logPriority.hpp:43
constexpr static logPrioT LOG_DEFAULT
Used to denote "use the default level for this log type".
Definition: logPriority.hpp:61
constexpr static logPrioT LOG_NOTICE
A normal but significant condition.
Definition: logPriority.hpp:46
constexpr static logPrioT LOG_TELEM
A telemetry recording.
Definition: logPriority.hpp:58
logPrioT logLevelFromString(const std::string &str)
Get the log priority from a string, which might have the number or the name.