CLHEP VERSION Reference Documentation
   
CLHEP Home Page     CLHEP Documentation     CLHEP Bug Reports

ZMexception.cc
Go to the documentation of this file.
1 // ----------------------------------------------------------------------
2 //
3 // ZMexception.cc -- implementation of the ZMexception class
4 //
5 // Methods found here:
6 // handleThis(x)
7 // logMessage()
8 //
9 // Revision History:
10 // 970912 MF Initial version after separating .icc from .cc
11 // 970916 WEB Updated per code review
12 // 970917 WEB Updated per code review 2
13 // 970918 WEB Updated per code review 3
14 // 971113 WEB Updated to conform to standard coding techniques
15 // 971211 WEB Updated per code walkthrough
16 // 971215 WEB Gave names to the default handler & logger
17 // 971217 WEB Append filter failure messages in logMessage()
18 // 971219 WEB Append newline to formatted messages
19 // 980213 WEB Include ZMutility/ZMtime.h instead of <ctime>
20 // 980223 WEB Include ZMutility/ctime instead of ZMtime
21 // 980304 WEB Reformat logged messages to avoid excessively
22 // long lines; otherwise cleaned up logMessage() &
23 // related stuff
24 // 980421 WEB Moved name() and facility() from .icc to .cc
25 // 980615 WEB Added namespace support
26 // 000217 WEB Improve C++ standards compliance
27 // 010410 MF Added code to supress time and file path
28 // 010626 MF Added code for ctor from ostringstream
29 // 031105 LG Get rid of all ZMutility references
30 //
31 // ----------------------------------------------------------------------
32 
33 
34 #include "CLHEP/Exceptions/defs.h"
40 
41 #include <sstream>
42 #include <ctime>
43 
44 
45 namespace zmex {
46 
47 // **************************************
48 //
49 // ZMexUserActivity, ZMexUserNumericalTag
50 //
51 // **************************************
52 
53 std::string ZMexUserActivity = "";
54 int ZMexUserNumericalTag = 0;
55 
56 
57 // *******************
58 //
59 // ZMhandler, ZMlogger
60 //
61 // *******************
62 
65  return ZMhandler;
66 } // ZMhandler()
67 
70  return ZMlogger;
71 }
72 
73 
74 // ***********************
75 //
76 // ZMexception::_classInfo
77 //
78 // ***********************
79 
80 ZMexClassInfo ZMexception::_classInfo(
81  "ZMexception"
82 , "Exceptions"
83 , ZMexFATAL
84 , ZMhandler()
85 , ZMlogger()
86 );
87 
88 
89 // ***********************
90 // ZMexception::facility()
91 // ***********************
92 
93 std::string ZMexception::facility() const {
94 
95  return classInfo().facility();
96 
97 } // ZMexception::facility()
98 
99 
100 // *******************
101 // ZMexception::name()
102 // *******************
103 
104 std::string ZMexception::name() const {
105 
106  return classInfo().name();
107 
108 } // ZMexception::name()
109 
110 
111 //*************
112 // logMessage()
113 //*************
114 
115 // This will be overridden in cases where, for a particular exception,
116 // one wishes to include auxiliary information with the logged message.
117 // The overriding function should compose its string, then call this
118 // (its ancestor) function with that string as argument.
119 
120 std::string ZMexception::logMessage( const std::string optText ) const {
121 
122  std::ostringstream mesg;
123 
124  // define how each follow-on line should begin:
125  #define NEXT "\n "
126 
127  // Supply the exception's identification as the first line:
128  mesg << facility()
129  << "-" << ZMexSeverityLetter[ severity() ]
130  << "-" << name() << " [#" << count() << "]";
131 
132  // Second line gives the exception instance's message
133  mesg << NEXT << message();
134 
135  // Warn if this exception hits the max for its severity:
136  if ( 1 == ZMexSeverityLimit[ severity() ] )
137  mesg << NEXT "-- Note: severity threshhold has been reached; "
138  "logging will be suppressed "
139  "for any future exceptions of this severity";
140 
141  // Warn if this exception hits the max for its class:
142  if ( classInfo().count() == classInfo().filterMax() )
143  mesg << NEXT "-- Note: class threshhold has been reached; "
144  "logging will be suppressed "
145  "for any future exceptions of this class";
146 
147  // Insert optional text (probably from override calling this as its ancestor):
148  if ( optText.length() )
149  mesg << NEXT << optText;
150 
151  // Insert time stamp:
152  ZMexLogger lgr = getLogger();
153  if ( lgr.control()->isTimeDesired() ) {
154  time_t now( time(0) );
155  char * timeText = ctime( & now );
156  timeText[24] = '\0'; // overwrite terminal '\n'
157  mesg << NEXT << timeText;
158  }
159 
160  // Identify whence we got here:
161  mesg << NEXT "-- ZMthrow was issued at line " << line();
162  std::string fullName = fileName();
163  std::string fname;
164  if ( lgr.control()->isFilePathDesired() ) {
165  fname = fullName;
166  } else {
167  unsigned int lastSlash = fullName.find_last_of("/\\");
168  if ( lastSlash == fullName.length() ) {
169  fname = fullName;
170  } else {
171  fname = fullName.substr(lastSlash+1);
172  }
173  }
174  mesg << NEXT "of file \"" << fname << '\"';
175 
176  // Identify disposition:
177  mesg << NEXT "... Exception " << ( wasThrown() ? "thrown!"
178  : "ignored"
179  );
180 
181  // Include optional user information, part 1:
182  if ( ZMexUserActivity.length() )
183  mesg << NEXT "-- ZMexUserActivity was: " << ZMexUserActivity;
184 
185  // Include optional user information, part 2:
186  if ( ZMexUserNumericalTag )
187  mesg << NEXT "-- User Numerical Tag was: " << ZMexUserNumericalTag;
188 
189  return mesg.str() + '\n';
190 
191 } // ZMexception::logMessage()
192 
193 //***********************************************
194 // Constructor of ZMexception from ostringstream&
195 //***********************************************
196 
198  const std::ostringstream & msg
199 , const ZMexSeverity howBad
200 , int icount
201 ) :
202  message_(msg.str())
203 , line_( 0 )
204 , sourceFileName_( "not ZMthrow'n as of yet" )
205 , mySeverity_( howBad == ZMexSEVERITYenumLAST ? _classInfo.severity() : howBad )
206 , myCount_( icount )
207 , wasThrown_( false )
208 { }
209 
210 } // namespace zmex
#define NEXT
const std::string facility() const
const std::string name() const
virtual bool isFilePathDesired() const
Definition: ZMexLogger.cc:61
virtual bool isTimeDesired() const
Definition: ZMexLogger.cc:60
ZMexLogBehavior * control()
Definition: ZMexLogger.cc:262
int count() const
std::string fileName() const
int line() const
virtual zmex::ZMexClassInfo & classInfo() const
static ZMexClassInfo _classInfo
virtual std::string logMessage(const std::string optText="") const
Definition: ZMexception.cc:120
ZMexception(const std::string &mesg, const ZMexSeverity howBad=ZMexSEVERITYenumLAST, int icount=ZMexception::_classInfo.nextCount())
std::string message() const
virtual std::string facility() const
Definition: ZMexception.cc:93
bool wasThrown() const
ZMexSeverity severity() const
virtual std::string name() const
Definition: ZMexception.cc:104
int ZMexUserNumericalTag
int ZMexSeverityLimit[ZMexSEVERITYenumLAST]
ZMexLogger & ZMlogger()
Definition: ZMexception.cc:68
ZMexHandler & ZMhandler()
Definition: ZMexception.cc:63
std::string ZMexUserActivity
const char ZMexSeverityLetter[ZMexSEVERITYenumLAST]