LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
ErrorObj.cc
Go to the documentation of this file.
2 // vim: set sw=2 expandtab :
3 
4 #include <atomic>
5 #include <list>
6 #include <string>
7 
8 using namespace std;
9 
10 namespace {
11 
12  unsigned int const maxCategoryLength{200U};
13 
14 } // unnamed namespace
15 
16 namespace mf {
17 
18  static atomic<int> ourSerial{0};
19 
20  ErrorObj::~ErrorObj() {}
21 
22  ErrorObj::ErrorObj(ELseverityLevel const sev,
23  string const& category,
24  bool const verbatim,
25  string const& filename,
26  int lineNumber)
27  : serial_{}
28  , xid_{}
29  , idOverflow_{}
30  , timestamp_{0, 0}
31  , items_{}
32  , reactedTo_{false}
33  , iteration_{}
34  , oss_{}
35  , verbatim_{verbatim}
38  {
39  set(sev, category);
40  }
41 
43  : serial_{orig.serial_}
44  , xid_(orig.xid_)
45  , idOverflow_{orig.idOverflow_}
46  , timestamp_(orig.timestamp_)
47  , items_{orig.items_}
48  , reactedTo_{orig.reactedTo_}
49  , verbatim_{orig.verbatim_}
50  , filename_{orig.filename_}
51  , lineNumber_{orig.lineNumber_}
52  {}
53 
54  int
56  {
57  return serial_;
58  }
59 
60  ELextendedID const&
61  ErrorObj::xid() const
62  {
63  return xid_;
64  }
65 
66  string const&
68  {
69  return idOverflow_;
70  }
71 
72  timeval
74  {
75  return timestamp_;
76  }
77 
78  std::list<std::string> const&
80  {
81  return items_;
82  }
83 
84  bool
86  {
87  return reactedTo_;
88  }
89 
90  bool
92  {
93  return verbatim_;
94  }
95 
96  // Note: Obsolete! Remove me when user code migrated.
97  string const&
99  {
100  return iteration_;
101  }
102 
103  string const&
105  {
106  return iteration_;
107  }
108 
109  string
111  {
112  string result;
113  for (auto const& text : items_) {
114  result += text;
115  }
116  return result;
117  }
118 
119  string const&
121  {
122  return filename_;
123  }
124 
125  int
127  {
128  return lineNumber_;
129  }
130 
131  void
133  {
135  (sev <= ELzeroSeverity) ?
137  ((sev >= ELhighestSeverity) ? (ELseverityLevel)ELsevere : sev));
138  }
139 
140  void
141  ErrorObj::setID(string const& category)
142  {
143  xid_.setID(string(category, 0, maxCategoryLength));
144  if (category.length() > maxCategoryLength) {
145  idOverflow_ = string(
146  category, maxCategoryLength, category.length() - maxCategoryLength);
147  }
148  }
149 
150  void
151  ErrorObj::setModule(string const& module)
152  {
153  xid_.setModule(module);
154  }
155 
156  // Note: Obsolete! Remove me when user code migrated.
157  void
158  ErrorObj::setContext(string const& s)
159  {
160  iteration_ = s;
161  }
162 
163  void
164  ErrorObj::setIteration(string const& s)
165  {
166  iteration_ = s;
167  }
168 
169  void
170  ErrorObj::setSubroutine(string const& subroutine)
171  {
172  xid_.setSubroutine((subroutine[0] == ' ') ? subroutine.substr(1) :
173  subroutine);
174  }
175 
176  void
178  {
179  reactedTo_ = r;
180  }
181 
182  void
183  ErrorObj::setHostName(string const& hostname)
184  {
185  xid_.setHostname(hostname);
186  }
187 
188  void
189  ErrorObj::setHostAddr(string const& hostaddr)
190  {
191  xid_.setHostaddr(hostaddr);
192  }
193 
194  void
195  ErrorObj::setApplication(string const& application)
196  {
197  xid_.setApplication(application);
198  }
199 
200  void
201  ErrorObj::setPID(long const pid)
202  {
203  xid_.setPID(pid);
204  }
205 
206  ErrorObj&
207  ErrorObj::eo_emit(string const& s)
208  {
209  if (s.substr(0, 5) == "@SUB=") {
210  setSubroutine(s.substr(5));
211  } else {
212  items_.push_back(s);
213  }
214  return *this;
215  }
216 
217  void
219  {
220  serial_ = 0;
221  xid_ = {};
222  idOverflow_.clear();
223  timestamp_ = {0, 0};
224  items_.clear();
225  reactedTo_ = false;
226  iteration_.clear();
227  oss_.str({});
228  verbatim_ = false;
229  }
230 
231  void
232  ErrorObj::set(ELseverityLevel const sev, string const& category)
233  {
234  gettimeofday(&timestamp_, 0);
235  serial_ = ++ourSerial;
236  setID(category);
237  setSeverity(sev);
238  }
239 
240  void
241  ErrorObj::setTimestamp(timeval const& t)
242  {
243  timestamp_ = t;
244  }
245 
246  ErrorObj&
247  ErrorObj::operator<<(std::ostream& (*f)(std::ostream&))
248  {
249  f(oss_);
250  oss_.str({});
251  return *this;
252  }
253 
254  ErrorObj&
255  ErrorObj::operator<<(std::ios_base& (*f)(std::ios_base&))
256  {
257  f(oss_);
258  return *this;
259  }
260 
261  // Force conversion of char arrays to string.
262  ErrorObj&
263  ErrorObj::opltlt(char const* s)
264  {
265  oss_.str({});
266  oss_ << s;
267  if (!oss_.str().empty()) {
268  eo_emit(oss_.str());
269  }
270  return *this;
271  }
272 
273  // Force conversion of char arrays to string.
274  ErrorObj&
275  operator<<(ErrorObj& e, char const* s)
276  {
277  return e.opltlt(s);
278  }
279 
280 } // namespace mf
TRandom r
Definition: spectrum.C:23
int lineNumber() const
Definition: ErrorObj.cc:126
virtual ErrorObj & eo_emit(std::string const &)
Definition: ErrorObj.cc:207
virtual void setID(std::string const &)
Definition: ErrorObj.cc:141
bool verbatim_
Definition: ErrorObj.h:88
timeval timestamp() const
Definition: ErrorObj.cc:73
timeval timestamp_
Definition: ErrorObj.h:83
std::string idOverflow_
Definition: ErrorObj.h:82
void setSeverity(ELseverityLevel severity)
Definition: ELextendedID.cc:81
virtual void setModule(std::string const &)
Definition: ErrorObj.cc:151
void setHostaddr(std::string const &hostaddr)
virtual void setPID(long)
Definition: ErrorObj.cc:201
virtual void set(ELseverityLevel const, std::string const &id)
Definition: ErrorObj.cc:232
virtual void setTimestamp(timeval const &)
Definition: ErrorObj.cc:241
STL namespace.
void setHostname(std::string const &hostname)
Definition: ELextendedID.cc:99
ELslProxy< ELhighestSeverityGen > constexpr ELhighestSeverity
virtual void setHostName(std::string const &)
Definition: ErrorObj.cc:183
std::string filename_
Definition: ErrorObj.h:89
std::list< std::string > const & items() const
Definition: ErrorObj.cc:79
std::string const & context() const
Definition: ErrorObj.cc:98
bool is_verbatim() const
Definition: ErrorObj.cc:91
static atomic< int > ourSerial
Definition: ErrorObj.cc:18
TFile f
Definition: plotHisto.C:6
int serial_
Definition: ErrorObj.h:80
std::string const & iteration() const
Definition: ErrorObj.cc:104
std::string const & idOverflow() const
Definition: ErrorObj.cc:67
void setSubroutine(std::string const &subroutine)
Definition: ELextendedID.cc:93
virtual void clear()
Definition: ErrorObj.cc:218
ErrorObj & operator<<(std::ostream &(*f)(std::ostream &))
Definition: ErrorObj.cc:247
int lineNumber_
Definition: ErrorObj.h:90
bool reactedTo_
Definition: ErrorObj.h:85
ELslProxy< ELzeroSeverityGen > constexpr ELzeroSeverity
ELextendedID const & xid() const
Definition: ErrorObj.cc:61
virtual void setSeverity(ELseverityLevel const)
Definition: ErrorObj.cc:132
std::string const & filename() const
Definition: ErrorObj.cc:120
virtual void setContext(std::string const &)
Definition: ErrorObj.cc:158
ELextendedID xid_
Definition: ErrorObj.h:81
virtual void setHostAddr(std::string const &)
Definition: ErrorObj.cc:189
std::string fullText() const
Definition: ErrorObj.cc:110
virtual void setApplication(std::string const &)
Definition: ErrorObj.cc:195
int serial() const
Definition: ErrorObj.cc:55
ErrorObj & opltlt(char const *)
Definition: ErrorObj.cc:263
void setID(std::string const &id)
Definition: ELextendedID.cc:75
ErrorObj()=delete
bool reactedTo() const
Definition: ErrorObj.cc:85
std::string iteration_
Definition: ErrorObj.h:86
void setPID(long pid)
std::ostringstream oss_
Definition: ErrorObj.h:87
void setApplication(std::string const &application)
ELslProxy< ELdebugGen > constexpr ELdebug
void setModule(std::string const &module)
Definition: ELextendedID.cc:87
virtual void setSubroutine(std::string const &)
Definition: ErrorObj.cc:170
virtual void setReactedTo(bool)
Definition: ErrorObj.cc:177
virtual void setIteration(std::string const &)
Definition: ErrorObj.cc:164
Float_t e
Definition: plot.C:35
ELslProxy< ELsevereGen > constexpr ELsevere
std::list< std::string > items_
Definition: ErrorObj.h:84