Line data Source code
1 : /*
2 : * DUNE DAQ modification notice:
3 : * This file has been modified from the original ATLAS ers source for the DUNE DAQ project.
4 : * Fork baseline commit: 8267df82a4f6fe6bf02c4014923eba19eddc4614 (2020-04-14).
5 : * Renamed since fork: yes (from src/Issue.cxx to src/Issue.cpp).
6 : *
7 : * Original copyright:
8 : * Copyright (C) 2001-2020 CERN for the benefit of the ATLAS collaboration.
9 : * Licensed under the Apache License, Version 2.0.
10 : */
11 :
12 : /*
13 : * Issue.cxx
14 : * ers
15 : *
16 : * Created by Matthias Wiesmann on 26.11.04.
17 : * Modified by Serguei Kolos on 02.08.05.
18 : * Copyright 2004 CERN. All rights reserved.
19 : *
20 : */
21 :
22 : #include <csignal>
23 : #include <iomanip>
24 : #include <sstream>
25 : #include <algorithm>
26 : #include <ctime>
27 : #include <time.h>
28 :
29 : #include <ers/Issue.hpp>
30 : #include <ers/IssueFactory.hpp>
31 : #include <ers/OutputStream.hpp>
32 : #include <ers/StandardStreamOutput.hpp>
33 : #include <ers/ers.hpp>
34 : #include <ers/internal/Util.hpp>
35 :
36 : using namespace ers;
37 :
38 80 : ERS_DECLARE_ISSUE( ers, StdIssue, ERS_EMPTY, ERS_EMPTY )
39 :
40 : namespace
41 : {
42 3376 : int get_default_qualifiers( std::vector<std::string> & qualifiers )
43 : {
44 3376 : static const char * environment = ::getenv( "DUNEDAQ_ERS_QUALIFIERS" );
45 3376 : if ( environment )
46 : {
47 0 : ers::tokenize( environment, ",", qualifiers );
48 : }
49 3376 : return 1;
50 : }
51 :
52 3376 : void add_default_qualifiers( Issue & issue )
53 : {
54 3376 : static std::vector<std::string> qualifiers;
55 : // static int _unused_ = get_default_qualifiers( qualifiers );
56 3376 : get_default_qualifiers( qualifiers );
57 3376 : for ( std::vector<std::string>::const_iterator it = qualifiers.begin(); it != qualifiers.end(); ++it )
58 : {
59 0 : issue.add_qualifier( *it );
60 : }
61 3376 : }
62 : }
63 :
64 74 : Issue::Issue( const Issue & other )
65 : : std::exception( other ),
66 74 : m_cause( other.m_cause.get() ? other.m_cause->clone() : 0 ),
67 74 : m_context( other.m_context->clone() ),
68 74 : m_message( other.m_message ),
69 74 : m_qualifiers( other.m_qualifiers ),
70 74 : m_severity( other.m_severity ),
71 74 : m_time( other.m_time ),
72 148 : m_values( other.m_values )
73 74 : { ; }
74 :
75 :
76 : /** This constructor create a new issue with the given message.
77 : * \param context the context of the Issue, e.g where in the code the issue appeared
78 : * \param message the user message associated with this issue
79 : */
80 3346 : Issue::Issue( const Context & context,
81 3346 : const std::string & message )
82 3346 : : m_context( context.clone() ),
83 3346 : m_message( message ),
84 3346 : m_severity( ers::Error ),
85 10038 : m_time( system_clock::now() )
86 : {
87 3346 : add_qualifier( m_context->package_name() );
88 3346 : add_default_qualifiers( *this );
89 3346 : }
90 :
91 : /** This constructor takes another exceptions as its cause.
92 : * \param context the context of the Issue, e.g where in the code the issue appeared
93 : * \param cause the other exception that has caused this one
94 : */
95 30 : Issue::Issue( const Context & context,
96 30 : const std::exception & cause )
97 30 : : m_context( context.clone() ),
98 30 : m_severity( ers::Error ),
99 90 : m_time( system_clock::now() )
100 : {
101 30 : const Issue * issue = dynamic_cast<const Issue *>( &cause );
102 30 : m_cause.reset( issue ? issue->clone() : new StdIssue( ERS_HERE, cause.what() ) );
103 30 : add_qualifier( m_context->package_name() );
104 30 : add_default_qualifiers( *this );
105 30 : }
106 :
107 : /** This constructor takes another exceptions as its cause.
108 : * \param context the context of the Issue, e.g where in the code did the issue appear
109 : * \param message the user message associated with this issue
110 : * \param cause exception that caused the current issue
111 : */
112 0 : Issue::Issue( const Context & context,
113 : const std::string & message,
114 0 : const std::exception & cause )
115 0 : : m_context( context.clone() ),
116 0 : m_message( message ),
117 0 : m_severity( ers::Error ),
118 0 : m_time( system_clock::now() )
119 : {
120 0 : const Issue * issue = dynamic_cast<const Issue *>( &cause );
121 0 : m_cause.reset( issue ? issue->clone() : new StdIssue( ERS_HERE, cause.what() ) );
122 0 : add_qualifier( m_context->package_name() );
123 0 : add_default_qualifiers( *this );
124 0 : }
125 :
126 0 : Issue::Issue( Severity severity,
127 : const system_clock::time_point & time,
128 : const ers::Context & context,
129 : const std::string & message,
130 : const std::vector<std::string> & qualifiers,
131 : const std::map<std::string, std::string> & parameters,
132 0 : const ers::Issue * cause )
133 0 : : m_cause( cause ),
134 0 : m_context( context.clone() ),
135 0 : m_message( message ),
136 0 : m_qualifiers( qualifiers ),
137 0 : m_severity( severity ),
138 0 : m_time( time ),
139 0 : m_values( parameters )
140 0 : { ; }
141 :
142 3450 : ers::Issue::~Issue() noexcept
143 3450 : { ; }
144 :
145 : std::time_t
146 1194 : ers::Issue::time_t() const
147 : {
148 1194 : return system_clock::to_time_t(m_time);
149 : }
150 :
151 : void
152 2 : ers::Issue::get_value( const std::string & key, const char * & value ) const
153 : {
154 2 : string_map::const_iterator it = m_values.find(key);
155 2 : if ( it != m_values.end() )
156 : {
157 2 : value = it->second.c_str();
158 : }
159 : else
160 : {
161 0 : throw ers::NoValue( ERS_HERE, key );
162 : }
163 2 : }
164 :
165 : void
166 0 : ers::Issue::get_value( const std::string & key, std::string & value ) const
167 : {
168 0 : string_map::const_iterator it = m_values.find(key);
169 0 : if ( it != m_values.end() )
170 : {
171 0 : value = it->second;
172 : }
173 : else
174 : {
175 0 : throw ers::NoValue( ERS_HERE, key );
176 : }
177 0 : }
178 :
179 : /** Add a new qualifier to the qualifiers list of this issue
180 : * \param qualifier the qualifier to add
181 : */
182 : void
183 3376 : Issue::add_qualifier( const std::string & qualifier )
184 : {
185 3376 : if ( std::find( m_qualifiers.begin(), m_qualifiers.end(), qualifier ) == m_qualifiers.end() ) {
186 3376 : m_qualifiers.push_back( qualifier );
187 : }
188 3376 : }
189 :
190 : ers::Severity
191 2259 : Issue::set_severity( ers::Severity severity ) const
192 : {
193 2259 : ers::Severity old_severity = m_severity;
194 2259 : m_severity = severity;
195 2259 : return old_severity;
196 : }
197 :
198 : /** Adds the given text to the beginning of the issue's message
199 : * \param msg text to be prepended
200 : */
201 : void
202 2395 : Issue::prepend_message( const std::string & msg )
203 : {
204 2395 : m_message = msg + m_message;
205 2395 : }
206 :
207 : /** Adds the given text strings to the beginning and to the end of the issue's message
208 : * \param begin text to be prepended
209 : * \param begin text to be appended
210 : */
211 : void
212 0 : Issue::wrap_message( const std::string & begin, const std::string & end )
213 : {
214 0 : m_message = begin + m_message + end;
215 0 : }
216 :
217 : namespace ers {
218 :
219 : /** Standard streaming operator - puts the issue in human readable format into the standard out stream.
220 : * \param out the destination out stream
221 : * \param issue the Issue to be printed
222 : */
223 4 : std::ostream & operator<<( std::ostream & out, const ers::Issue & issue )
224 : {
225 4 : return StandardStreamOutput::print( out, issue, ers::verbosity_level() );
226 : }
227 : }
228 :
229 :
230 :
231 :
|