NASA Logo
Ocean Color Science Software

ocssw V2022
Log.hpp
Go to the documentation of this file.
1 
2 #ifndef FOCS_LOG
3 #define FOCS_LOG
4 
5 #include "Configuration.hpp"
6 #include "log/Base.hpp"
7 #include "log/File.hpp"
8 #include "log/Stream.hpp"
10 #include "log/FormattedFile.hpp"
11 
12 #include <iostream>
13 #include <memory>
14 #include <sstream>
15 #include <string>
16 #include <vector>
17 
18 #include <gsl/gsl>
19 
20 #include <boost/format.hpp>
21 
22 #ifdef INTL_FOUND
23 #include <libintl.h>
24 #include <locale.h>
25 #endif
26 #define EXIT_LOG(...) \
27  { \
28  __VA_ARGS__; \
29  std::cerr << "Exiting. See " << __FILE__ << ":" << __LINE__ << std::endl; \
30  exit(EXIT_FAILURE); \
31  }
32 namespace focs {
33 
47 class Log {
48  public:
50  Log() : Log(false){ }
56  explicit Log(bool default_loggers){
57  if (default_loggers){
58  add(std::make_unique<focs::FileStreamLogger>());
59  }
60  }
61  // move constructors (facilities can't easily be moved, needs more work)
62  // Log(const Log&) = default;
63  // explicit Log(std::vector<std::unique_ptr<LogFacility>> targets) : targets_{std::move(targets)}{}
64 
70  inline static Log& get_default(){
71  static Log _instance{true};
72  return _instance;
73  }
74 
80  void add(std::unique_ptr<LogFacility> target){
81  targets_.emplace_back(std::move(target));
82  }
89  void add(int min_severity, std::unique_ptr<LogFacility> target){
90  targets_.emplace_back(min_severity, std::move(target));
91  }
99  void add(int min_severity, int max_severity, std::unique_ptr<LogFacility> target){
100  targets_.emplace_back(min_severity, max_severity, std::move(target));
101  }
102 
111  void log_raw(int severity, const std::string& s){
112  for (auto& t : targets_){
113  if (severity <= t.min_severity() && severity >= t.max_severity()){
114  t.log_facility().log(severity, s);
115  }
116  }
117  }
118 
125  void log(int severity, const std::string& s){
126 #ifdef INTL_FOUND
127  const auto& final = ::gettext(s.c_str());
128  log_raw(severity, final);
129 #else
130  log_raw(severity, s);
131 #endif
132  }
133 
134 
145  template<typename... Arguments>
146  void log(int severity, const std::string& fmt, const Arguments&... args){
147 #ifdef INTL_FOUND
148  boost::format f{::gettext(fmt.c_str())};
149 #else
150  boost::format f{fmt};
151 #endif
152  //C++17, fold expression: const auto& s = boost::str((boost::format(fmt) % ... % args));
153  using unroll = bool[];
154  (void)unroll{(f % std::forward<decltype(args)>(args), false)...}; // <- comma operator
155 
156  const auto& s = boost::str(f);
157  log_raw(severity, s);
158  }
166  log(static_cast<int>(severity), s);
167  }
168 
177  template<typename... Arguments>
178  void log(LogSeverity severity, const std::string& fmt, const Arguments&... args){
179  log(static_cast<int>(severity), fmt, args...);
180  }
181 
188  void add(LogSeverity min_severity, std::unique_ptr<LogFacility> target){
189  add(static_cast<int>(min_severity), std::move(target));
190  }
198  void add(LogSeverity min_severity, LogSeverity max_severity, std::unique_ptr<LogFacility> target){
199  add(static_cast<int>(min_severity), static_cast<int>(max_severity), std::move(target));
200  }
201 
211  log_raw(static_cast<int>(severity), s);
212  }
213 
225  template<typename... Arguments>
226  void debug(const std::string& s, const Arguments&... args){ log(LogSeverity::debug, s, args...); }
238  template<typename... Arguments>
239  void info(const std::string& s, const Arguments&... args){ log(LogSeverity::info, s, args...); }
251  template<typename... Arguments>
252  void notice(const std::string& s, const Arguments&... args){ log(LogSeverity::notice, s, args...); }
264  template<typename... Arguments>
265  void warning(const std::string& s, const Arguments&... args){ log(LogSeverity::warning, s, args...); }
277  template<typename... Arguments>
278  void error(const std::string& s, const Arguments&... args){ log(LogSeverity::error, s, args...); }
290  template<typename... Arguments>
291  void critical(const std::string& s, const Arguments&... args){ log(LogSeverity::critical, s, args...); }
303  template<typename... Arguments>
304  void alert(const std::string& s, const Arguments&... args){ log(LogSeverity::alert, s, args...); }
316  template<typename... Arguments>
317  void emergency(const std::string& s, const Arguments&... args){ log(LogSeverity::emergency, s, args...); }
318 
330  template<typename... Arguments>
331  void warn(const std::string& s, const Arguments&... args){ log(LogSeverity::warn, s, args...); }
336  void err(const std::string& s){ log(LogSeverity::err, s); }
343  template<typename... Arguments>
344  void err(const std::string& s, const Arguments&... args){ log(LogSeverity::err, s, args...); }
356  template<typename... Arguments>
357  void crit(const std::string& s, const Arguments&... args){ log(LogSeverity::crit, s, args...); }
369  template<typename... Arguments>
370  void emerg(const std::string& s, const Arguments&... args){ log(LogSeverity::emerg, s, args...); }
371 
377  size_t num_loggers() { return targets_.size(); }
383  LogSeverity severity() { return severity_; }
384 
394  me.severity_ = severity;
395  return me;
396  }
407  friend Log& operator<<(Log& me, const std::string& t){
408  me.log_raw(static_cast<int>(me.severity_), t);
409  return me;
410  }
422  template<typename T>
423  friend Log& operator<<(Log& me, const T& t){
424  std::stringbuf buf;
425  std::ostream out{&buf};
426  out << t;
427  me.log_raw(static_cast<int>(me.severity_), buf.str());
428  return me;
429  }
430 
431  private:
432  std::vector<LogTarget> targets_{};
433  LogSeverity severity_{LogSeverity::info};
434 
435 };
436 } // namespace focs
437 
438 #endif // FOCS_LOG
@ emergency
emergency severity
data_t t[NROOTS+1]
Definition: decode_rs.h:77
@ debug
debug severity
void debug(const std::string &s)
Log a message with debug severity.
Definition: Log.hpp:218
@ err
error severity (alias for error)
void error(const std::string &s)
Log a message with error severity.
Definition: Log.hpp:270
void add(int min_severity, int max_severity, std::unique_ptr< LogFacility > target)
Add new log facility with given minimum and maximum severities.
Definition: Log.hpp:99
no change in intended resolving MODur00064 Corrected handling of bad ephemeris attitude resolving resolving GSFcd00179 Corrected handling of fill values for[Sensor|Solar][Zenith|Azimuth] resolving MODxl01751 Changed to validate LUT version against a value retrieved from the resolving MODxl02056 Changed to calculate Solar Diffuser angles without adjustment for estimated post launch changes in the MODIS orientation relative to incidentally resolving defects MODxl01766 Also resolves MODxl01947 Changed to ignore fill values in SCI_ABNORM and SCI_STATE rather than treating them as resolving MODxl01780 Changed to use spacecraft ancillary data to recognise when the mirror encoder data is being set by side A or side B and to change calculations accordingly This removes the need for seperate LUTs for Side A and Side B data it makes the new LUTs incompatible with older versions of the and vice versa Also resolves MODxl01685 A more robust GRing algorithm is being which will create a non default GRing anytime there s even a single geolocated pixel in a granule Removed obsolete messages from seed as required for compatibility with version of the SDP toolkit Corrected test output file names to end in out
Definition: HISTORY.txt:422
LogSeverity severity()
Return the default severity (only used for stream operators)
Definition: Log.hpp:383
void critical(const std::string &s, const Arguments &... args)
Log a message with critical severity.
Definition: Log.hpp:291
void alert(const std::string &s)
Log a message with alert severity.
Definition: Log.hpp:296
void log(int severity, const std::string &fmt, const Arguments &... args)
Log a message with a given (numeric) severity, performing i18n in the process.
Definition: Log.hpp:146
void add(LogSeverity min_severity, LogSeverity max_severity, std::unique_ptr< LogFacility > target)
Add new log facility with given minimum and maximum severities.
Definition: Log.hpp:198
void add(LogSeverity min_severity, std::unique_ptr< LogFacility > target)
Add new log facility with a given minimum severity and no maximum.
Definition: Log.hpp:188
@ crit
critical severity (alias for critical)
void crit(const std::string &s, const Arguments &... args)
Log a message with critical severity.
Definition: Log.hpp:357
void log_raw(int severity, const std::string &s)
Log a message with a given (numeric) severity.
Definition: Log.hpp:111
void add(std::unique_ptr< LogFacility > target)
Add new log facility, using default severity filter (info severity and above)
Definition: Log.hpp:80
void warning(const std::string &s)
Log a message with warning severity.
Definition: Log.hpp:257
void emerg(const std::string &s, const Arguments &... args)
Log a message with emergency severity.
Definition: Log.hpp:370
void alert(const std::string &s, const Arguments &... args)
Log a message with alert severity.
Definition: Log.hpp:304
@ string
void warn(const std::string &s)
Log a message with warning severity.
Definition: Log.hpp:323
static Log & get_default()
Obtain a singleton of the default logger.
Definition: Log.hpp:70
void warn(const std::string &s, const Arguments &... args)
Log a message with warning severity.
Definition: Log.hpp:331
void emergency(const std::string &s)
Log a message with emergency severity.
Definition: Log.hpp:309
void log_raw(LogSeverity severity, const std::string &s)
Log a message with a given (numeric) severity.
Definition: Log.hpp:210
void emerg(const std::string &s)
Log a message with emergency severity.
Definition: Log.hpp:362
void info(const std::string &s)
Log a message with info severity.
Definition: Log.hpp:231
void critical(const std::string &s)
Log a message with critical severity.
Definition: Log.hpp:283
double precision function f(R1)
Definition: tmd.lp.f:1454
@ warning
warning severity
@ critical
critical severity
@ emerg
emergency severity (alias for emergency)
void error(const std::string &s, const Arguments &... args)
Log a message with error severity.
Definition: Log.hpp:278
Log()
Default constructor with no output facilities.
Definition: Log.hpp:50
void notice(const std::string &s, const Arguments &... args)
Log a message with notice severity.
Definition: Log.hpp:252
friend Log & operator<<(Log &me, LogSeverity severity)
set the default severity for stream operators
Definition: Log.hpp:393
void emergency(const std::string &s, const Arguments &... args)
Log a message with emergency severity.
Definition: Log.hpp:317
friend Log & operator<<(Log &me, const T &t)
log a non-string object via stream operator, using default log severity
Definition: Log.hpp:423
@ info
info severity
friend Log & operator<<(Log &me, const std::string &t)
log a message via stream operator, using default log severity
Definition: Log.hpp:407
@ alert
alert severity
void add(int min_severity, std::unique_ptr< LogFacility > target)
Add new log facility with a given minimum severity and no maximum.
Definition: Log.hpp:89
void log(int severity, const std::string &s)
Log a message with a given (numeric) severity, performing i18n in the process.
Definition: Log.hpp:125
void info(const std::string &s, const Arguments &... args)
Log a message with info severity.
Definition: Log.hpp:239
void warning(const std::string &s, const Arguments &... args)
Log a message with warning severity.
Definition: Log.hpp:265
@ notice
notice severity
data_t s[NROOTS]
Definition: decode_rs.h:75
Multiple output, versatile logger.
Definition: Log.hpp:47
void log(LogSeverity severity, const std::string &fmt, const Arguments &... args)
Log a message with a given severity, performing i18n in the process.
Definition: Log.hpp:178
void err(const std::string &s)
Log a message with error severity.
Definition: Log.hpp:336
void debug(const std::string &s, const Arguments &... args)
Log a message with debug severity.
Definition: Log.hpp:226
void log(LogSeverity severity, const std::string &s)
Log a message with a given severity.
Definition: Log.hpp:165
void crit(const std::string &s)
Log a message with critical severity.
Definition: Log.hpp:349
size_t num_loggers()
Check number of log facilities.
Definition: Log.hpp:377
@ warn
warning severity (alias for warning)
@ error
error severity
void err(const std::string &s, const Arguments &... args)
Log a message with error severity.
Definition: Log.hpp:344
Log(bool default_loggers)
Default constructor, optionally creating default facilities.
Definition: Log.hpp:56
#define str(s)
LogSeverity
Log severities, for use with focs::Log.
Definition: Base.hpp:45
void notice(const std::string &s)
Log a message with notice severity.
Definition: Log.hpp:244