DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
Issue.hpp
Go to the documentation of this file.
1/*
2 * Issue.h
3 * ers
4 *
5 * Created by Matthias Wiesmann on 08.12.04.
6 * Modified by Serguei Kolos on 02.08.05.
7 * Copyright 2005 CERN. All rights reserved.
8 *
9 */
10
11#ifndef ERS_ISSUE_H
12#define ERS_ISSUE_H
13
14#include <math.h>
15#include <stdio.h>
16#include <string.h>
17
18#include <map>
19#include <string>
20#include <iostream>
21#include <sstream>
22#include <memory>
23#include <chrono>
24#include <list>
25
26#include <ers/IssueFactory.hpp>
27#include <ers/LocalContext.hpp>
28#include <ers/Severity.hpp>
29
36using std::chrono::system_clock;
37
38namespace ers
39{
40 class OutputStream;
41
42 typedef std::map<std::string, std::string> string_map;
43
44 using inheritance_type = std::list<std::string> ;
45
46 template<class T>
48 public:
52
53 private:
54 static ers::Issue* create(const Context &context) {
55 return new T(context);
56 }
57 };
58
68 class Issue : public std::exception
69 {
70 friend class IssueFactory;
71
72 public:
73 Issue( const Context & context,
74 const std::string & message = std::string() );
75
76 Issue( const Context & context,
77 const std::exception & cause );
78
79 Issue( const Context & context,
80 const std::string & message,
81 const std::exception & cause );
82
83 Issue( const Issue & other );
84
85 virtual ~Issue() noexcept;
86
87 virtual Issue * clone() const = 0;
88
89 virtual const char * get_class_name() const = 0;
93 virtual void raise() const = 0;
95 void add_qualifier( const std::string & qualif );
97 const Issue * cause() const
98 { return m_cause.get(); }
99
100 const Context & context() const
101 { return *(m_context.get()); }
102
103 const std::string & message() const
104 { return m_message; }
105
106 const std::vector<std::string> & qualifiers() const
107 { return m_qualifiers; }
108
109 const string_map & parameters() const
110 { return m_values; }
111
113 { return m_severity; }
114
115 template <class Precision=std::chrono::seconds>
116 std::string time(const std::string & format = "%Y-%b-%d %H:%M:%S", bool isUTC = false) const;
117
119 template <class Precision>
120 std::string localtime(const std::string & format = "%Y-%b-%d %H:%M:%S") const
121 { return time<Precision>(format, false); }
122
124 template <class Precision>
125 std::string gmtime(const std::string & format = "%Y-%b-%d %H:%M:%S") const
126 { return time<Precision>(format, true); }
127
128 std::time_t time_t() const;
130 const system_clock::time_point & ptime() const
131 { return m_time; }
132
133 const char * what() const noexcept
134 { return m_message.c_str(); }
135
137
138 void wrap_message( const std::string & begin, const std::string & end );
139
140 protected:
142 const system_clock::time_point & time,
143 const ers::Context & context,
144 const std::string & message,
145 const std::vector<std::string> & qualifiers,
146 const std::map<std::string, std::string> & parameters,
147 const ers::Issue * cause = 0 );
148
150 template <typename T>
151 void get_value( const std::string & key, T & value ) const;
152 void get_value( const std::string & key, const char * & value ) const;
153 void get_value( const std::string & key, std::string & value ) const;
154
156 template <typename T>
157 void set_value( const std::string & key, T value );
158
159 void set_message( const std::string & message )
160 { m_message = message; }
161
162 void prepend_message( const std::string & message );
163
164 static auto _get_inheritance() {
165 inheritance_type chain;
166 chain.push_back( "ers::Issue" ) ;
167 return chain;
168 }
169
170 private:
171 Issue & operator=( const Issue & other ) = delete;
172
173 std::unique_ptr<const Issue> m_cause;
174 std::unique_ptr<Context> m_context;
175 std::string m_message;
176 std::vector<std::string> m_qualifiers;
178 system_clock::time_point m_time;
180 };
181
182 std::ostream & operator<<( std::ostream &, const ers::Issue & );
183
184} // ers
185
186
188
190 NoValue,
191 "value for the \"" << key << "\" key is not set ",
192 ((std::string)key ) )
193
194template <typename T>
195void
196ers::Issue::get_value( const std::string & key, T & value ) const
197{
198 string_map::const_iterator it = m_values.find(key);
199 if ( it == m_values.end() )
200 {
201 throw ers::NoValue( ERS_HERE, key );
202 }
203 std::istringstream in( it->second );
204 in >> value;
205}
206
207template <typename T>
208void
209ers::Issue::set_value( const std::string & key, T value )
210{
211 std::ostringstream out;
212 out << value;
213 m_values[key] = out.str();
214}
215
216template <class Precision>
217std::string
218ers::Issue::time(const std::string & format, bool isUTC) const
219{
220 static const int width(::log10(Precision::period::den));
221
222 std::time_t t = time_t();
223 std::tm tm = isUTC ? *gmtime_r(&t, &tm) : *localtime_r(&t, &tm);
224
225 char buff[128];
226 std::strftime(buff, 128 - 16, format.c_str(), &tm);
227
228 auto c = std::chrono::duration_cast<Precision>(
229 m_time.time_since_epoch()).count();
230 double frac = c - (double)t*Precision::period::den;
231 sprintf(buff + strlen(buff), ",%0*.0f", width, frac);
232
233 return buff;
234}
235
236#endif
237
#define ERS_DECLARE_ISSUE(namespace_name, class_name, message, attributes)
#define ERS_HERE
An abstract interface to access an Issue context.
Definition Context.hpp:30
Implements factory pattern for user defined Issues.
static IssueFactory & instance()
method to access singleton
void register_issue(const std::string &name, IssueCreator creator)
register an issue factory
static ers::Issue * create(const Context &context)
Definition Issue.hpp:54
Base class for any user define issue.
Definition Issue.hpp:69
const Context & context() const
Context of the issue.
Definition Issue.hpp:100
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
Issue & operator=(const Issue &other)=delete
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
virtual const char * get_class_name() const =0
Get key for class (used for serialisation)
const system_clock::time_point & ptime() const
original time point of the issue
Definition Issue.hpp:130
void set_message(const std::string &message)
Definition Issue.hpp:159
void get_value(const std::string &key, T &value) const
const std::vector< std::string > & qualifiers() const
return array of qualifiers
Definition Issue.hpp:106
const std::string & message() const
General cause of the issue.
Definition Issue.hpp:103
std::string gmtime(const std::string &format="%Y-%b-%d %H:%M:%S") const
Definition Issue.hpp:125
const string_map & parameters() const
return array of parameters
Definition Issue.hpp:109
std::string time(const std::string &format="%Y-%b-%d %H:%M:%S", bool isUTC=false) const
string representation of local time of the issue
Definition Issue.hpp:218
std::string localtime(const std::string &format="%Y-%b-%d %H:%M:%S") const
string representation of UTC time of the issue
Definition Issue.hpp:120
virtual Issue * clone() const =0
virtual void raise() const =0
throws a copy of this issue preserving the real issue type
std::unique_ptr< const Issue > m_cause
Issue that caused the current issue.
Definition Issue.hpp:173
void set_value(const std::string &key, T value)
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
virtual inheritance_type get_class_inheritance() const =0
Get inheritance chain.
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
static auto _get_inheritance()
Definition Issue.hpp:164
const Issue * cause() const
return the cause Issue of this Issue
Definition Issue.hpp:97
FELIX Initialization std::string initerror FELIX queue timed out
std::list< std::string > inheritance_type
Definition Issue.hpp:44
std::map< std::string, std::string > string_map
Definition Issue.hpp:42
std::ostream & operator<<(std::ostream &, const ers::Configuration &)
severity
Definition Severity.hpp:26