OB.DAAC Logo
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 
27 namespace focs {
28 
42 class Log {
43  public:
45  Log() : Log(false){ }
51  explicit Log(bool default_loggers){
52  if (default_loggers){
53  add(std::make_unique<focs::FileStreamLogger>());
54  }
55  }
56  // move constructors (facilities can't easily be moved, needs more work)
57  // Log(const Log&) = default;
58  // explicit Log(std::vector<std::unique_ptr<LogFacility>> targets) : targets_{std::move(targets)}{}
59 
65  inline static Log& get_default(){
66  static Log _instance{true};
67  return _instance;
68  }
69 
75  void add(std::unique_ptr<LogFacility> target){
76  targets_.emplace_back(std::move(target));
77  }
84  void add(int min_severity, std::unique_ptr<LogFacility> target){
85  targets_.emplace_back(min_severity, std::move(target));
86  }
94  void add(int min_severity, int max_severity, std::unique_ptr<LogFacility> target){
95  targets_.emplace_back(min_severity, max_severity, std::move(target));
96  }
97 
106  void log_raw(int severity, const std::string& s){
107  for (auto& t : targets_){
108  if (severity <= t.min_severity() && severity >= t.max_severity()){
109  t.log_facility().log(severity, s);
110  }
111  }
112  }
113 
120  void log(int severity, const std::string& s){
121 #ifdef INTL_FOUND
122  const auto& final = ::gettext(s.c_str());
123  log_raw(severity, final);
124 #else
125  log_raw(severity, s);
126 #endif
127  }
128 
129 
140  template<typename... Arguments>
141  void log(int severity, const std::string& fmt, const Arguments&... args){
142 #ifdef INTL_FOUND
143  boost::format f{::gettext(fmt.c_str())};
144 #else
145  boost::format f{fmt};
146 #endif
147  //C++17, fold expression: const auto& s = boost::str((boost::format(fmt) % ... % args));
148  using unroll = bool[];
149  (void)unroll{(f % std::forward<decltype(args)>(args), false)...}; // <- comma operator
150 
151  const auto& s = boost::str(f);
152  log_raw(severity, s);
153  }
161  log(static_cast<int>(severity), s);
162  }
163 
172  template<typename... Arguments>
173  void log(LogSeverity severity, const std::string& fmt, const Arguments&... args){
174  log(static_cast<int>(severity), fmt, args...);
175  }
176 
183  void add(LogSeverity min_severity, std::unique_ptr<LogFacility> target){
184  add(static_cast<int>(min_severity), std::move(target));
185  }
193  void add(LogSeverity min_severity, LogSeverity max_severity, std::unique_ptr<LogFacility> target){
194  add(static_cast<int>(min_severity), static_cast<int>(max_severity), std::move(target));
195  }
196 
206  log_raw(static_cast<int>(severity), s);
207  }
208 
220  template<typename... Arguments>
221  void debug(const std::string& s, const Arguments&... args){ log(LogSeverity::debug, s, args...); }
233  template<typename... Arguments>
234  void info(const std::string& s, const Arguments&... args){ log(LogSeverity::info, s, args...); }
246  template<typename... Arguments>
247  void notice(const std::string& s, const Arguments&... args){ log(LogSeverity::notice, s, args...); }
259  template<typename... Arguments>
260  void warning(const std::string& s, const Arguments&... args){ log(LogSeverity::warning, s, args...); }
272  template<typename... Arguments>
273  void error(const std::string& s, const Arguments&... args){ log(LogSeverity::error, s, args...); }
285  template<typename... Arguments>
286  void critical(const std::string& s, const Arguments&... args){ log(LogSeverity::critical, s, args...); }
298  template<typename... Arguments>
299  void alert(const std::string& s, const Arguments&... args){ log(LogSeverity::alert, s, args...); }
311  template<typename... Arguments>
312  void emergency(const std::string& s, const Arguments&... args){ log(LogSeverity::emergency, s, args...); }
313 
325  template<typename... Arguments>
326  void warn(const std::string& s, const Arguments&... args){ log(LogSeverity::warn, s, args...); }
331  void err(const std::string& s){ log(LogSeverity::err, s); }
338  template<typename... Arguments>
339  void err(const std::string& s, const Arguments&... args){ log(LogSeverity::err, s, args...); }
351  template<typename... Arguments>
352  void crit(const std::string& s, const Arguments&... args){ log(LogSeverity::crit, s, args...); }
364  template<typename... Arguments>
365  void emerg(const std::string& s, const Arguments&... args){ log(LogSeverity::emerg, s, args...); }
366 
372  size_t num_loggers() { return targets_.size(); }
378  LogSeverity severity() { return severity_; }
379 
389  me.severity_ = severity;
390  return me;
391  }
402  friend Log& operator<<(Log& me, const std::string& t){
403  me.log_raw(static_cast<int>(me.severity_), t);
404  return me;
405  }
417  template<typename T>
418  friend Log& operator<<(Log& me, const T& t){
419  std::stringbuf buf;
420  std::ostream out{&buf};
421  out << t;
422  me.log_raw(static_cast<int>(me.severity_), buf.str());
423  return me;
424  }
425 
426  private:
427  std::vector<LogTarget> targets_{};
428  LogSeverity severity_{LogSeverity::info};
429 
430 };
431 } // namespace focs
432 
433 #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:213
@ err
error severity (alias for error)
void error(const std::string &s)
Log a message with error severity.
Definition: Log.hpp:265
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:94
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:378
void critical(const std::string &s, const Arguments &... args)
Log a message with critical severity.
Definition: Log.hpp:286
void alert(const std::string &s)
Log a message with alert severity.
Definition: Log.hpp:291
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:141
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:193
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:183
@ crit
critical severity (alias for critical)
void crit(const std::string &s, const Arguments &... args)
Log a message with critical severity.
Definition: Log.hpp:352
void log_raw(int severity, const std::string &s)
Log a message with a given (numeric) severity.
Definition: Log.hpp:106
void add(std::unique_ptr< LogFacility > target)
Add new log facility, using default severity filter (info severity and above)
Definition: Log.hpp:75
void warning(const std::string &s)
Log a message with warning severity.
Definition: Log.hpp:252
void emerg(const std::string &s, const Arguments &... args)
Log a message with emergency severity.
Definition: Log.hpp:365
void alert(const std::string &s, const Arguments &... args)
Log a message with alert severity.
Definition: Log.hpp:299
@ string
void warn(const std::string &s)
Log a message with warning severity.
Definition: Log.hpp:318
static Log & get_default()
Obtain a singleton of the default logger.
Definition: Log.hpp:65
void warn(const std::string &s, const Arguments &... args)
Log a message with warning severity.
Definition: Log.hpp:326
void emergency(const std::string &s)
Log a message with emergency severity.
Definition: Log.hpp:304
void log_raw(LogSeverity severity, const std::string &s)
Log a message with a given (numeric) severity.
Definition: Log.hpp:205
void emerg(const std::string &s)
Log a message with emergency severity.
Definition: Log.hpp:357
void info(const std::string &s)
Log a message with info severity.
Definition: Log.hpp:226
void critical(const std::string &s)
Log a message with critical severity.
Definition: Log.hpp:278
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:273
Log()
Default constructor with no output facilities.
Definition: Log.hpp:45
void notice(const std::string &s, const Arguments &... args)
Log a message with notice severity.
Definition: Log.hpp:247
friend Log & operator<<(Log &me, LogSeverity severity)
set the default severity for stream operators
Definition: Log.hpp:388
void emergency(const std::string &s, const Arguments &... args)
Log a message with emergency severity.
Definition: Log.hpp:312
friend Log & operator<<(Log &me, const T &t)
log a non-string object via stream operator, using default log severity
Definition: Log.hpp:418
@ 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:402
@ alert
alert severity
const char * str
Definition: l1c_msi.cpp:35
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:84
void log(int severity, const std::string &s)
Log a message with a given (numeric) severity, performing i18n in the process.
Definition: Log.hpp:120
void info(const std::string &s, const Arguments &... args)
Log a message with info severity.
Definition: Log.hpp:234
void warning(const std::string &s, const Arguments &... args)
Log a message with warning severity.
Definition: Log.hpp:260
@ notice
notice severity
data_t s[NROOTS]
Definition: decode_rs.h:75
Multiple output, versatile logger.
Definition: Log.hpp:42
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:173
void err(const std::string &s)
Log a message with error severity.
Definition: Log.hpp:331
void debug(const std::string &s, const Arguments &... args)
Log a message with debug severity.
Definition: Log.hpp:221
void log(LogSeverity severity, const std::string &s)
Log a message with a given severity.
Definition: Log.hpp:160
void crit(const std::string &s)
Log a message with critical severity.
Definition: Log.hpp:344
size_t num_loggers()
Check number of log facilities.
Definition: Log.hpp:372
@ 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:339
Log(bool default_loggers)
Default constructor, optionally creating default facilities.
Definition: Log.hpp:51
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:239