DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
Issue.cpp
Go to the documentation of this file.
1/*
2 * Issue.cxx
3 * ers
4 *
5 * Created by Matthias Wiesmann on 26.11.04.
6 * Modified by Serguei Kolos on 02.08.05.
7 * Copyright 2004 CERN. All rights reserved.
8 *
9 */
10
11#include <csignal>
12#include <iomanip>
13#include <sstream>
14#include <algorithm>
15#include <ctime>
16#include <time.h>
17
18#include <ers/Issue.hpp>
19#include <ers/IssueFactory.hpp>
20#include <ers/OutputStream.hpp>
22#include <ers/ers.hpp>
23#include <ers/internal/Util.hpp>
24
25using namespace ers;
26
28
29namespace
30{
31 int get_default_qualifiers( std::vector<std::string> & qualifiers )
32 {
33 static const char * environment = ::getenv( "DUNEDAQ_ERS_QUALIFIERS" );
34 if ( environment )
35 {
36 ers::tokenize( environment, ",", qualifiers );
37 }
38 return 1;
39 }
40
41 void add_default_qualifiers( Issue & issue )
42 {
43 static std::vector<std::string> qualifiers;
44 // static int _unused_ = get_default_qualifiers( qualifiers );
45 get_default_qualifiers( qualifiers );
46 for ( std::vector<std::string>::const_iterator it = qualifiers.begin(); it != qualifiers.end(); ++it )
47 {
48 issue.add_qualifier( *it );
49 }
50 }
51}
52
53Issue::Issue( const Issue & other )
54 : std::exception( other ),
55 m_cause( other.m_cause.get() ? other.m_cause->clone() : 0 ),
56 m_context( other.m_context->clone() ),
57 m_message( other.m_message ),
58 m_qualifiers( other.m_qualifiers ),
59 m_severity( other.m_severity ),
60 m_time( other.m_time ),
61 m_values( other.m_values )
62{ ; }
63
64
69Issue::Issue( const Context & context,
70 const std::string & message )
71 : m_context( context.clone() ),
72 m_message( message ),
73 m_severity( ers::Error ),
74 m_time( system_clock::now() )
75{
76 add_qualifier( m_context->package_name() );
77 add_default_qualifiers( *this );
78}
79
84Issue::Issue( const Context & context,
85 const std::exception & cause )
86 : m_context( context.clone() ),
87 m_severity( ers::Error ),
88 m_time( system_clock::now() )
89{
90 const Issue * issue = dynamic_cast<const Issue *>( &cause );
91 m_cause.reset( issue ? issue->clone() : new StdIssue( ERS_HERE, cause.what() ) );
92 add_qualifier( m_context->package_name() );
93 add_default_qualifiers( *this );
94}
95
101Issue::Issue( const Context & context,
102 const std::string & message,
103 const std::exception & cause )
104 : m_context( context.clone() ),
105 m_message( message ),
106 m_severity( ers::Error ),
107 m_time( system_clock::now() )
108{
109 const Issue * issue = dynamic_cast<const Issue *>( &cause );
110 m_cause.reset( issue ? issue->clone() : new StdIssue( ERS_HERE, cause.what() ) );
111 add_qualifier( m_context->package_name() );
112 add_default_qualifiers( *this );
113}
114
116 const system_clock::time_point & time,
117 const ers::Context & context,
118 const std::string & message,
119 const std::vector<std::string> & qualifiers,
120 const std::map<std::string, std::string> & parameters,
121 const ers::Issue * cause )
122 : m_cause( cause ),
123 m_context( context.clone() ),
124 m_message( message ),
125 m_qualifiers( qualifiers ),
126 m_severity( severity ),
127 m_time( time ),
128 m_values( parameters )
129{ ; }
130
132{ ; }
133
134std::time_t
136{
137 return system_clock::to_time_t(m_time);
138}
139
140void
141ers::Issue::get_value( const std::string & key, const char * & value ) const
142{
143 string_map::const_iterator it = m_values.find(key);
144 if ( it != m_values.end() )
145 {
146 value = it->second.c_str();
147 }
148 else
149 {
150 throw ers::NoValue( ERS_HERE, key );
151 }
152}
153
154void
155ers::Issue::get_value( const std::string & key, std::string & value ) const
156{
157 string_map::const_iterator it = m_values.find(key);
158 if ( it != m_values.end() )
159 {
160 value = it->second;
161 }
162 else
163 {
164 throw ers::NoValue( ERS_HERE, key );
165 }
166}
167
171void
172Issue::add_qualifier( const std::string & qualifier )
173{
174 if ( std::find( m_qualifiers.begin(), m_qualifiers.end(), qualifier ) == m_qualifiers.end() ) {
175 m_qualifiers.push_back( qualifier );
176 }
177}
178
181{
182 ers::Severity old_severity = m_severity;
184 return old_severity;
185}
186
190void
191Issue::prepend_message( const std::string & msg )
192{
193 m_message = msg + m_message;
194}
195
200void
201Issue::wrap_message( const std::string & begin, const std::string & end )
202{
203 m_message = begin + m_message + end;
204}
205
206namespace ers {
207
212 std::ostream & operator<<( std::ostream & out, const ers::Issue & issue )
213 {
215 }
216}
217
218
219
220
#define ERS_DECLARE_ISSUE(namespace_name, class_name, message, attributes)
#define ERS_EMPTY
#define ERS_HERE
An abstract interface to access an Issue context.
Definition Context.hpp:30
Base class for any user define issue.
Definition Issue.hpp:69
ers::Severity severity() const
severity of the issue
Definition Issue.hpp:112
Severity m_severity
Issue's severity.
Definition Issue.hpp:177
void wrap_message(const std::string &begin, const std::string &end)
Definition Issue.cpp:201
std::unique_ptr< Context > m_context
Context of the current issue.
Definition Issue.hpp:174
virtual ~Issue() noexcept
Definition Issue.cpp:131
void prepend_message(const std::string &message)
Definition Issue.cpp:191
system_clock::time_point m_time
Time when issue was thrown.
Definition Issue.hpp:178
void get_value(const std::string &key, T &value) const
virtual Issue * clone() const =0
std::unique_ptr< const Issue > m_cause
Issue that caused the current issue.
Definition Issue.hpp:173
string_map m_values
List of user defined attributes.
Definition Issue.hpp:179
std::string m_message
Issue's explanation text.
Definition Issue.hpp:175
void add_qualifier(const std::string &qualif)
adds a qualifier to the issue
Definition Issue.cpp:172
const char * what() const noexcept
General cause of the issue.
Definition Issue.hpp:133
ers::Severity set_severity(ers::Severity severity) const
Definition Issue.cpp:180
std::time_t time_t() const
seconds since 1 Jan 1970
Definition Issue.cpp:135
std::vector< std::string > m_qualifiers
List of associated qualifiers.
Definition Issue.hpp:176
Issue(const Context &context, const std::string &message=std::string())
Definition Issue.cpp:69
const Issue * cause() const
return the cause Issue of this Issue
Definition Issue.hpp:97
caught dunedaq::conffwk::Exception exception
static int64_t now()
int verbosity_level()
Definition ers.hpp:108
void tokenize(const std::string &text, const std::string &separators, std::vector< std::string > &tokens)
Definition Util.cpp:7
std::ostream & operator<<(std::ostream &, const ers::Configuration &)
severity
Definition Severity.hpp:26
@ Error
Definition Severity.hpp:26
static std::ostream & print(std::ostream &out, const Issue &issue, int verbosity)