CLHEP 2.4.6.4
C++ Class Library for High Energy Physics
Loading...
Searching...
No Matches
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"
35#include "CLHEP/Exceptions/ZMexception.h"
36#include "CLHEP/Exceptions/ZMexAction.h"
37#include "CLHEP/Exceptions/ZMexClassInfo.h"
38#include "CLHEP/Exceptions/ZMexHandler.h"
39#include "CLHEP/Exceptions/ZMexLogger.h"
40#include "CLHEP/Exceptions/ZMexLogResult.h"
41#include "CLHEP/Exceptions/ZMexSeverity.h"
42
43#include <sstream>
44#include <ctime>
45
46
47namespace zmex {
48
49// **************************************
50//
51// ZMexUserActivity, ZMexUserNumericalTag
52//
53// **************************************
54
55std::string ZMexUserActivity = "";
57
58
59// *******************
60//
61// ZMhandler, ZMlogger
62//
63// *******************
64
67 return ZMhandler;
68} // ZMhandler()
69
72 return ZMlogger;
73}
74
75
76// ***********************
77//
78// ZMexception::_classInfo
79//
80// ***********************
81
82ZMexClassInfo ZMexception::_classInfo(
83 "ZMexception"
84, "Exceptions"
86, ZMhandler()
87, ZMlogger()
88);
89
90
91// ***********************
92// ZMexception::facility()
93// ***********************
94
95std::string ZMexception::facility() const {
96
97 return classInfo().facility();
98
99} // ZMexception::facility()
100
101
102// *******************
103// ZMexception::name()
104// *******************
105
106std::string ZMexception::name() const {
107
108 return classInfo().name();
109
110} // ZMexception::name()
111
112
113//*************
114// logMessage()
115//*************
116
117// This will be overridden in cases where, for a particular exception,
118// one wishes to include auxiliary information with the logged message.
119// The overriding function should compose its string, then call this
120// (its ancestor) function with that string as argument.
121
122std::string ZMexception::logMessage( const std::string optText ) const {
123
124 std::ostringstream mesg;
125
126 // define how each follow-on line should begin:
127 #define NEXT "\n "
128
129 // Supply the exception's identification as the first line:
130 mesg << facility()
131 << "-" << ZMexSeverityLetter[ severity() ]
132 << "-" << name() << " [#" << count() << "]";
133
134 // Second line gives the exception instance's message
135 mesg << NEXT << message();
136
137 // Warn if this exception hits the max for its severity:
138 if ( 1 == ZMexSeverityLimit[ severity() ] )
139 mesg << NEXT "-- Note: severity threshhold has been reached; "
140 "logging will be suppressed "
141 "for any future exceptions of this severity";
142
143 // Warn if this exception hits the max for its class:
144 if ( classInfo().count() == classInfo().filterMax() )
145 mesg << NEXT "-- Note: class threshhold has been reached; "
146 "logging will be suppressed "
147 "for any future exceptions of this class";
148
149 // Insert optional text (probably from override calling this as its ancestor):
150 if ( optText.length() )
151 mesg << NEXT << optText;
152
153 // Insert time stamp:
154 ZMexLogger lgr = getLogger();
155 if ( lgr.control()->isTimeDesired() ) {
156 time_t now( time(0) );
157 char * timeText = ctime( & now );
158 timeText[24] = '\0'; // overwrite terminal '\n'
159 mesg << NEXT << timeText;
160 }
161
162 // Identify whence we got here:
163 mesg << NEXT "-- ZMthrow was issued at line " << line();
164 std::string fullName = fileName();
165 std::string fname;
166 if ( lgr.control()->isFilePathDesired() ) {
167 fname = fullName;
168 } else {
169 unsigned long lastSlash = fullName.find_last_of("/\\");
170 if ( lastSlash == fullName.length() ) {
171 fname = fullName;
172 } else {
173 fname = fullName.substr(lastSlash+1);
174 }
175 }
176 mesg << NEXT "of file \"" << fname << '\"';
177
178 // Identify disposition:
179 mesg << NEXT "... Exception " << ( wasThrown() ? "thrown!"
180 : "ignored"
181 );
182
183 // Include optional user information, part 1:
184 if ( ZMexUserActivity.length() )
185 mesg << NEXT "-- ZMexUserActivity was: " << ZMexUserActivity;
186
187 // Include optional user information, part 2:
189 mesg << NEXT "-- User Numerical Tag was: " << ZMexUserNumericalTag;
190
191 return mesg.str() + '\n';
192
193} // ZMexception::logMessage()
194
195//***********************************************
196// Constructor of ZMexception from ostringstream&
197//***********************************************
198
200 const std::ostringstream & msg
201, const ZMexSeverity howBad
202, int icount
203) :
204 message_(msg.str())
205, line_( 0 )
206, sourceFileName_( "not ZMthrow'n as of yet" )
207, mySeverity_( howBad == ZMexSEVERITYenumLAST ? _classInfo.severity() : howBad )
208, myCount_( icount )
209, wasThrown_( false )
210{ }
211
212} // namespace zmex
#define NEXT
const std::string facility() const
const std::string name() const
virtual bool isFilePathDesired() const
Definition: ZMexLogger.cc:64
virtual bool isTimeDesired() const
Definition: ZMexLogger.cc:63
ZMexLogBehavior * control()
Definition: ZMexLogger.cc:265
int count() const
std::string fileName() const
int line() const
static ZMexClassInfo _classInfo
Definition: ZMexception.h:166
virtual std::string logMessage(const std::string optText="") const
Definition: ZMexception.cc:122
ZMexception(const std::string &mesg, const ZMexSeverity howBad=ZMexSEVERITYenumLAST, int icount=ZMexception::_classInfo.nextCount())
std::string message() const
virtual zmex::ZMexClassInfo & classInfo() const
Definition: ZMexception.h:443
virtual std::string facility() const
Definition: ZMexception.cc:95
bool wasThrown() const
ZMexSeverity severity() const
virtual std::string name() const
Definition: ZMexception.cc:106
Definition: ZMerrno.h:52
int ZMexUserNumericalTag
Definition: ZMexception.cc:56
int ZMexSeverityLimit[ZMexSEVERITYenumLAST]
Definition: ZMexSeverity.cc:45
ZMexLogger & ZMlogger()
Definition: ZMexception.cc:70
ZMexSeverity
Definition: ZMexSeverity.h:32
@ ZMexSEVERITYenumLAST
Definition: ZMexSeverity.h:66
@ ZMexFATAL
Definition: ZMexSeverity.h:57
ZMexHandler & ZMhandler()
Definition: ZMexception.cc:65
std::string ZMexUserActivity
Definition: ZMexception.cc:55
const char ZMexSeverityLetter[ZMexSEVERITYenumLAST]
Definition: ZMexSeverity.cc:34