CMS 3D CMS Logo

ErrorObj.cc
Go to the documentation of this file.
1 // ----------------------------------------------------------------------
2 //
3 // ErrorObj.cc
4 //
5 // History:
6 //
7 // Created 7/8/98 mf
8 // 6/16/99 mf, jvr ErrorObj::operator<<( void (* f)(ErrorLog &) )
9 // allows an "ErrorObj << endmsg;"
10 // 7/16/99 jvr Added setSeverity() and setID functions
11 // 6/7/00 web Forbid setting extreme severities; guard against
12 // too-long ID's
13 // 9/21/00 mf Added copy constructor for use by ELerrorList
14 // 5/7/01 mf operator<< (const char[])
15 // 6/5/01 mf setReactedTo
16 // 11/01/01 web maxIDlength now unsigned
17 //
18 // 2/14/06 mf Removed oerator<<(endmsg) which is not needed for
19 // MessageLogger for CMS
20 //
21 // 3/13/06 mf Instrumented for automatic suppression of spaces.
22 // 3/20/06 mf Instrumented for universal suppression of spaces
23 // (that is, items no longer contain any space added
24 // by the MessageLogger stack)
25 //
26 // 4/28/06 mf maxIDlength changed from 20 to 200
27 // If a category name exceeds that, then the
28 // limit not taking effect bug will occur again...
29 //
30 // 6/6/06 mf verbatim
31 //
32 // ErrorObj( const ELseverityLevel & sev, const ELstring & id )
33 // ~ErrorObj()
34 // set( const ELseverityLevel & sev, const ELstring & id )
35 // clear()
36 // setModule ( const ELstring & module )
37 // setSubroutine( const ELstring & subroutine )
38 // emitToken( const ELstring & txt )
39 //
40 // ----------------------------------------------------------------------
41 
42 #include <algorithm>
43 #include <atomic>
44 #include <cctype>
45 
48 
49 #ifndef IOSTREAM_INCLUDED
50 #endif
51 
52 // ----------------------------------------------------------------------
53 
54 // Possible Traces
55 // #define ErrorObjCONSTRUCTOR_TRACE
56 // #define ErrorObj_EMIT_TRACE
57 // #define ErrorObj_SUB_TRACE
58 
59 // ----------------------------------------------------------------------
60 
61 namespace {
62  bool eq_nocase(std::string_view a, std::string_view b) {
63  return std::equal(
64  a.begin(), a.end(), b.begin(), b.end(), [](char x, char y) { return std::toupper(x) == std::toupper(y); });
65  }
66 } // namespace
67 
68 namespace edm {
69 
70  // ----------------------------------------------------------------------
71  // Class static and class-wide parameter:
72  // ----------------------------------------------------------------------
73 
74  // --- class-wide serial number stamper:
75  //
76  CMS_THREAD_SAFE static std::atomic<int> ourSerial(0);
77  const unsigned int maxIDlength(200); // changed 4/28/06 from 20
78 
79  // ----------------------------------------------------------------------
80  // Birth/death:
81  // ----------------------------------------------------------------------
82 
83  ErrorObj::ErrorObj(const messagelogger::ELseverityLevel& sev, std::string_view id, bool verbat) : verbatim(verbat) {
84 #ifdef ErrorObjCONSTRUCTOR_TRACE
85  std::cerr << "Constructor for ErrorObj\n";
86 #endif
87 
88  clear();
89  set(sev, id);
90 
91  } // ErrorObj()
92 
94  : mySerial(orig.mySerial),
95  myXid(orig.myXid),
96  myIdOverflow(orig.myIdOverflow),
97  myTimestamp(orig.myTimestamp),
98  myItems(orig.myItems),
99  myReactedTo(orig.myReactedTo),
100  myOs(),
101  emptyString(),
102  verbatim(orig.verbatim) {
103 #ifdef ErrorObjCONSTRUCTOR_TRACE
104  std::cerr << "Copy Constructor for ErrorObj\n";
105 #endif
106 
107  } // ErrorObj(ErrorObj)
108 
110 #ifdef ErrorObjCONSTRUCTOR_TRACE
111  std::cerr << "Destructor for ErrorObj\n";
112 #endif
113 
114  } // ~ErrorObj()
115 
118  this->swap(temp);
119  return *this;
120  }
121 
123  std::swap(mySerial, other.mySerial);
124  std::swap(myXid, other.myXid);
125  myIdOverflow.swap(other.myIdOverflow);
126  std::swap(myTimestamp, other.myTimestamp);
127  myItems.swap(other.myItems);
128  std::swap(myReactedTo, other.myReactedTo);
129  myContext.swap(other.myContext);
130  std::string temp(other.myOs.str());
131  other.myOs.str(myOs.str());
132  myOs.str(temp);
133  emptyString.swap(other.emptyString);
134  std::swap(verbatim, other.verbatim);
135  }
136 
137  // ----------------------------------------------------------------------
138  // Accessors:
139  // ----------------------------------------------------------------------
140 
141  int ErrorObj::serial() const { return mySerial; }
142  const ELextendedID& ErrorObj::xid() const { return myXid; }
143  const std::string& ErrorObj::idOverflow() const { return myIdOverflow; }
144  time_t ErrorObj::timestamp() const { return myTimestamp; }
145  const ELlist_string& ErrorObj::items() const { return myItems; }
146  bool ErrorObj::reactedTo() const { return myReactedTo; }
147  bool ErrorObj::is_verbatim() const { return verbatim; }
148 
149  const std::string& ErrorObj::context() const { return myContext; }
150 
153  for (auto const& it : myItems)
154  result += it;
155  return result;
156  } // fullText()
157 
158  // ----------------------------------------------------------------------
159  // Mutators:
160  // ----------------------------------------------------------------------
161 
163  using namespace edm::messagelogger;
166  : sev;
167  }
168 
169  void ErrorObj::setID(std::string_view id) {
170  myXid.id = id.substr(0, maxIDlength);
171  if (id.length() > maxIDlength)
172  myIdOverflow = id.substr(maxIDlength, id.length() - maxIDlength);
173  }
174 
175  void ErrorObj::setModule(std::string_view module) { myXid.module = module; }
176 
177  void ErrorObj::setContext(std::string_view c) { myContext = c; }
178 
179  void ErrorObj::setSubroutine(std::string_view subroutine) {
180 #ifdef ErrorObj_SUB_TRACE
181  std::cerr << "=:=:=: ErrorObj::setSubroutine(" << subroutine << ")\n";
182 #endif
183  myXid.subroutine = (subroutine[0] == ' ') ? subroutine.substr(1) : subroutine;
184  }
185 
186  void ErrorObj::setReactedTo(bool r) { myReactedTo = r; }
187 
188 #ifdef ErrorObj_SUB_TRACE
189  static int subN = 0;
190 #endif
191 
192  ErrorObj& ErrorObj::emitToken(std::string_view s) {
193 #ifdef ErrorObj_EMIT_TRACE
194  std::cerr << "=:=:=: ErrorObj::emitToken( " << s << " )\n";
195 #endif
196 
197 #ifdef ErrorObj_SUB_TRACE
198  if (subN > 0) {
199  std::cerr << "=:=:=: subN ErrorObj::emitToken( " << s << " )\n";
200  subN--;
201  }
202 #endif
203 
204  if (eq_nocase(s.substr(0, 5), "@SUB=")) {
205 #ifdef ErrorObj_SUB_TRACE
206  std::cerr << "=:=:=: ErrorObj::@SUB s.substr(5) is: " << s.substr(5) << '\n';
207 #endif
208  setSubroutine(s.substr(5));
209  } else {
210  myItems.emplace_back(s);
211  }
212 
213  return *this;
214 
215  } // emitToken()
216 
217  void ErrorObj::set(const messagelogger::ELseverityLevel& sev, std::string_view id) {
218  clear();
219 
220  myTimestamp = time(nullptr);
221  mySerial = ++ourSerial;
222 
223  setID(id);
224  setSeverity(sev);
225 
226  } // set()
227 
229  mySerial = 0;
230  myXid.clear();
231  myIdOverflow = "";
232  myTimestamp = 0;
233  myItems.erase(myItems.begin(), myItems.end()); // myItems.clear();
234  myReactedTo = false;
235 
236  } // clear()
237 
238  ErrorObj& ErrorObj::opltlt(const char s[]) {
239  // Exactly equivalent to the general template.
240  // If this is not provided explicitly, then the template will
241  // be instantiated once for each length of string ever used.
242  myOs.str(emptyString);
243  myOs << s;
244 #ifdef OLD_STYLE_AUTOMATIC_SPACES
245  if (!myOs.str().empty()) {
246  if (!verbatim) {
247  emitToken(myOs.str() + ' ');
248  } else {
249  emitToken(myOs.str());
250  }
251  }
252 #else
253  if (!myOs.str().empty())
254  emitToken(myOs.str());
255 #endif
256  return *this;
257  }
258 
259  ErrorObj& operator<<(ErrorObj& e, const char s[]) { return e.opltlt(s); }
260 
261 } // end of namespace edm */
std::string emptyString
Definition: ErrorObj.h:105
messagelogger::ELseverityLevel severity
Definition: ELextendedID.h:30
time_t myTimestamp
Definition: ErrorObj.h:100
virtual ~ErrorObj()
Definition: ErrorObj.cc:109
virtual void clear()
Definition: ErrorObj.cc:228
virtual ErrorObj & emitToken(std::string_view txt)
Definition: ErrorObj.cc:192
time_t timestamp() const
Definition: ErrorObj.cc:144
int serial() const
Definition: ErrorObj.cc:141
std::string fullText() const
Definition: ErrorObj.cc:151
void swap(ErrorObj &other)
Definition: ErrorObj.cc:122
const ELlist_string & items() const
Definition: ErrorObj.cc:145
virtual void setSeverity(const messagelogger::ELseverityLevel &sev)
Definition: ErrorObj.cc:162
bool reactedTo() const
Definition: ErrorObj.cc:146
std::list< std::string > ELlist_string
Definition: ELlist.h:33
constexpr const ELseverityLevel ELhighestSeverity
ELlist_string myItems
Definition: ErrorObj.h:101
const std::string & context() const
Definition: ErrorObj.cc:149
bool equal(const T &first, const T &second)
Definition: Equal.h:32
constexpr const ELseverityLevel ELsevere
virtual void setReactedTo(bool r)
Definition: ErrorObj.cc:186
std::string myContext
Definition: ErrorObj.h:103
std::string module
Definition: ELextendedID.h:31
ELextendedID myXid
Definition: ErrorObj.h:98
ErrorObj & opltlt(const T &t)
virtual void set(const messagelogger::ELseverityLevel &sev, std::string_view id)
Definition: ErrorObj.cc:217
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
virtual void setSubroutine(std::string_view subroutine)
Definition: ErrorObj.cc:179
ErrorObj(const messagelogger::ELseverityLevel &sev, std::string_view id, bool verbatim=false)
Definition: ErrorObj.cc:83
ErrorObj & operator<<(std::ostream &(*f)(std::ostream &))
std::ostringstream myOs
Definition: ErrorObj.h:104
#define CMS_THREAD_SAFE
const unsigned int maxIDlength(200)
virtual void setModule(std::string_view module)
Definition: ErrorObj.cc:175
const ELextendedID & xid() const
Definition: ErrorObj.cc:142
const std::string & idOverflow() const
Definition: ErrorObj.cc:143
std::string myIdOverflow
Definition: ErrorObj.h:99
bool verbatim
Definition: ErrorObj.h:106
static std::atomic< int > ourSerial(0)
bool is_verbatim() const
Definition: ErrorObj.cc:147
std::string id
Definition: ELextendedID.h:29
ErrorObj & operator=(const ErrorObj &other)
Definition: ErrorObj.cc:116
double b
Definition: hdecay.h:120
constexpr const ELseverityLevel ELzeroSeverity
std::string subroutine
Definition: ELextendedID.h:32
virtual void setContext(std::string_view context)
Definition: ErrorObj.cc:177
static std::string const emptyString("")
HLT enums.
double a
Definition: hdecay.h:121
virtual void setID(std::string_view ID)
Definition: ErrorObj.cc:169
float x
bool myReactedTo
Definition: ErrorObj.h:102
constexpr const ELseverityLevel ELdebug
int mySerial
Definition: ErrorObj.h:97