version  0.0.1
Defines the C++ API for MsPASS
ErrorLogger.h
1 #ifndef _ERROR_LOGGER_H_
2 #define _ERROR_LOGGER_H_
3 #include <unistd.h>
4 #include <list>
5 #include <boost/archive/basic_archive.hpp>
6 #include <boost/archive/text_iarchive.hpp>
7 #include <boost/archive/text_oarchive.hpp>
8 #include <boost/serialization/serialization.hpp>
9 #include <boost/serialization/list.hpp>
10 #include "mspass/utility/MsPASSError.h"
11 #include "mspass/utility/ErrorLogger.h"
12 namespace mspass
13 {
14 namespace utility{
15 class LogData
16 {
17 public:
18  int job_id;
19  int p_id; // output of getpid()
20  std::string algorithm;
21  mspass::utility::ErrorSeverity badness;
22  std::string message;
23  LogData(){};
30  LogData(const int jid, const std::string alg,const mspass::utility::MsPASSError& merr);
38  LogData(const int jid, const std::string alg, const std::string msg, const mspass::utility::ErrorSeverity lvl);
39  friend std::ostream& operator<<(std::ostream&, LogData&);
40 private:
41  friend boost::serialization::access;
42  template<class Archive>
43  void serialize(Archive& ar,const unsigned int version)
44  {
45  ar & job_id;
46  ar & p_id;
47  ar & algorithm;
48  ar & badness;
49  ar & message;
50  };
51 };
61 {
62 public:
63  ErrorLogger(){job_id=0;};
64  ErrorLogger(int job)
65  {
66  job_id=job;
67  };
68  ErrorLogger(const ErrorLogger& parent);
69  void set_job_id(int jid){job_id=jid;};
70  int get_job_id(){return job_id;};
78  int log_error(const mspass::utility::MsPASSError& merr);
92  int log_error(const std::string alg, const std::string mess,
93  const mspass::utility::ErrorSeverity level);
94 
102  int log_verbose(const std::string alg, const std::string mess);
103  std::list<LogData> get_error_log()const{return allmessages;};
104  int size()const{return allmessages.size();};
106  void clear(){allmessages.clear();};
107  ErrorLogger& operator=(const ErrorLogger& parent);
110  ErrorLogger& operator+=(const ErrorLogger& parent);
112  std::list<LogData> worst_errors()const;
113 private:
114  int job_id;
115  std::list<LogData> allmessages;
116  friend boost::serialization::access;
117  template<class Archive>
118  void serialize(Archive& ar,const unsigned int version)
119  {
120  ar & job_id;
121  ar & allmessages;
122  };
123 };
124 
137 template <typename Tdata> bool data_are_valid(const Tdata& d)
138 {
139  if(d.dead()) return false;
140  std::list<LogData> welog;
141  welog=d.elog.worst_errors();
142  /*The return will be empty if there are no errors logged*/
143  if(welog.size()<=0) return true;
144  LogData ld;
145  /* Worst errors can return a list of multiple entries. For
146  fatal or invalid id should never be more than one, but this is
147  a clean way to extract the first member of the list if it isn't empty*/
148  ld=*(welog.begin());
149  if(ld.badness == ErrorSeverity::Fatal || ld.badness == ErrorSeverity::Invalid)
150  return false;
151  else
152  return true;
153 }
154 } // end utility namespace
155 } // End mspass namespace
156 #endif
Container to hold error logs for a data object.
Definition: ErrorLogger.h:61
ErrorLogger & operator+=(const ErrorLogger &parent)
Definition: ErrorLogger.cc:70
int log_verbose(const std::string alg, const std::string mess)
Log a verbose message marking it informational.
Definition: ErrorLogger.cc:94
std::list< LogData > worst_errors() const
Definition: ErrorLogger.cc:107
int log_error(const mspass::utility::MsPASSError &merr)
Definition: ErrorLogger.cc:81
void clear()
Definition: ErrorLogger.h:106
Definition: ErrorLogger.h:16
Base class for error object thrown by MsPASS library routines.
Definition: MsPASSError.h:40