DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
Issue.hpp
Go to the documentation of this file.
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 ers/Issue.h to include/ers/Issue.hpp).
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.h
14 * ers
15 *
16 * Created by Matthias Wiesmann on 08.12.04.
17 * Modified by Serguei Kolos on 02.08.05.
18 * Copyright 2005 CERN. All rights reserved.
19 *
20 */
21
22#ifndef ERS_ISSUE_H
23#define ERS_ISSUE_H
24
25#include <math.h>
26#include <stdio.h>
27#include <string.h>
28
29#include <chrono>
30#include <iostream>
31#include <list>
32#include <map>
33#include <memory>
34#include <sstream>
35#include <string>
36
37#include <ers/IssueFactory.hpp>
38#include <ers/LocalContext.hpp>
39#include <ers/Severity.hpp>
40
46
47using std::chrono::system_clock;
48
49namespace ers {
50class OutputStream;
51
52typedef std::map<std::string, std::string> string_map;
53
54using inheritance_type = std::list<std::string>;
55
56template<class T>
58{
59public:
61
62private:
63 static ers::Issue* create(const Context& context) { return new T(context); }
64};
65
75class Issue : public std::exception
76{
77 friend class IssueFactory;
78
79public:
80 Issue(const Context& context, const std::string& message = std::string());
81
82 Issue(const Context& context, const std::exception& cause);
83
84 Issue(const Context& context, const std::string& message, const std::exception& cause);
85
86 Issue(const Issue& other);
87
88 virtual ~Issue() noexcept;
89
90 virtual Issue* clone() const = 0;
91
92 virtual const char* get_class_name() const = 0;
93
95
96 virtual void raise() const = 0;
97
98 void add_qualifier(const std::string& qualif);
99
100 const Issue* cause() const { return m_cause.get(); }
101
102 const Context& context() const { return *(m_context.get()); }
103
104 const std::string& message() const { return m_message; }
105
106 const std::vector<std::string>& qualifiers() const { return m_qualifiers; }
107
108 const string_map& parameters() const { return m_values; }
109
111
112 template<class Precision = std::chrono::seconds>
113 std::string time(const std::string& format = "%Y-%b-%d %H:%M:%S", bool isUTC = false) const;
114
116 template<class Precision>
117 std::string localtime(const std::string& format = "%Y-%b-%d %H:%M:%S") const
118 {
119 return time<Precision>(format, false);
120 }
121
123 template<class Precision>
124 std::string gmtime(const std::string& format = "%Y-%b-%d %H:%M:%S") const
125 {
126 return time<Precision>(format, true);
127 }
128
129 std::time_t time_t() const;
130
131 const system_clock::time_point& ptime() const { return m_time; }
132
133 const char* what() const noexcept { return m_message.c_str(); }
134
136
137 void wrap_message(const std::string& begin, const std::string& end);
138
139protected:
141 const system_clock::time_point& time,
142 const ers::Context& context,
143 const std::string& message,
144 const std::vector<std::string>& qualifiers,
145 const std::map<std::string, std::string>& parameters,
146 const ers::Issue* cause = 0);
147
149 template<typename T>
150 void get_value(const std::string& key, T& value) const;
151 void get_value(const std::string& key, const char*& value) const;
152 void get_value(const std::string& key, std::string& value) const;
153
155 template<typename T>
156 void set_value(const std::string& key, T value);
157
158 void set_message(const std::string& message) { m_message = message; }
159
160 void prepend_message(const std::string& message);
161
162 static auto _get_inheritance()
163 {
164 inheritance_type chain;
165 chain.push_back("ers::Issue");
166 return chain;
167 }
168
169private:
170 Issue& operator=(const Issue& other) = delete;
171
172 std::unique_ptr<const Issue> m_cause;
173 std::unique_ptr<Context> m_context;
174 std::string m_message;
175 std::vector<std::string> m_qualifiers;
177 system_clock::time_point m_time;
179};
180
181std::ostream&
182operator<<(std::ostream&, const ers::Issue&);
183
184} // ers
185
187
188ERS_DECLARE_ISSUE(ers, NoValue, "value for the \"" << key << "\" key is not set ", ((std::string)key))
189
190template<typename T>
191void
192ers::Issue::get_value(const std::string& key, T& value) const
193{
194 string_map::const_iterator it = m_values.find(key);
195 if (it == m_values.end()) {
196 throw ers::NoValue(ERS_HERE, key);
197 }
198 std::istringstream in(it->second);
199 in >> value;
200}
201
202template<typename T>
203void
204ers::Issue::set_value(const std::string& key, T value)
205{
206 std::ostringstream out;
207 out << value;
208 m_values[key] = out.str();
209}
210
211template<class Precision>
212std::string
213ers::Issue::time(const std::string& format, bool isUTC) const
214{
215 static const int width(::log10(Precision::period::den));
216
217 std::time_t t = time_t();
218 std::tm tm = isUTC ? *gmtime_r(&t, &tm) : *localtime_r(&t, &tm);
219
220 char buff[128];
221 std::strftime(buff, 128 - 16, format.c_str(), &tm);
222
223 auto c = std::chrono::duration_cast<Precision>(m_time.time_since_epoch()).count();
224 double frac = c - (double)t * Precision::period::den;
225 sprintf(buff + strlen(buff), ",%0*.0f", width, frac);
226
227 return buff;
228}
229
230#endif
#define ERS_HERE
An abstract interface to access an Issue context.
Definition Context.hpp:40
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:63
Base class for any user define issue.
Definition Issue.hpp:76
const Context & context() const
Context of the issue.
Definition Issue.hpp:102
ers::Severity severity() const
severity of the issue
Definition Issue.hpp:110
Severity m_severity
Issue's severity.
Definition Issue.hpp:176
void wrap_message(const std::string &begin, const std::string &end)
Definition Issue.cpp:206
std::unique_ptr< Context > m_context
Context of the current issue.
Definition Issue.hpp:173
Issue & operator=(const Issue &other)=delete
void prepend_message(const std::string &message)
Definition Issue.cpp:196
system_clock::time_point m_time
Time when issue was thrown.
Definition Issue.hpp:177
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:131
void set_message(const std::string &message)
Definition Issue.hpp:158
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:104
std::string gmtime(const std::string &format="%Y-%b-%d %H:%M:%S") const
Definition Issue.hpp:124
const string_map & parameters() const
return array of parameters
Definition Issue.hpp:108
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:213
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:117
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:172
friend class IssueFactory
Definition Issue.hpp:77
void set_value(const std::string &key, T value)
string_map m_values
List of user defined attributes.
Definition Issue.hpp:178
std::string m_message
Issue's explanation text.
Definition Issue.hpp:174
void add_qualifier(const std::string &qualif)
adds a qualifier to the issue
Definition Issue.cpp:177
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:185
std::time_t time_t() const
seconds since 1 Jan 1970
Definition Issue.cpp:146
std::vector< std::string > m_qualifiers
List of associated qualifiers.
Definition Issue.hpp:175
Issue(const Context &context, const std::string &message=std::string())
Definition Issue.cpp:80
static auto _get_inheritance()
Definition Issue.hpp:162
const Issue * cause() const
return the cause Issue of this Issue
Definition Issue.hpp:100
ERS abstract output stream interface.
#define ERS_DECLARE_ISSUE(namespace_name, class_name, message_, attributes)
Definition macro.hpp:71
FELIX Initialization std::string initerror FELIX queue timed out
std::list< std::string > inheritance_type
Definition Issue.hpp:54
message(message)
Definition __init__.py:84
std::map< std::string, std::string > string_map
Definition Issue.hpp:52
std::ostream & operator<<(std::ostream &, const ers::Configuration &)
severity
Definition Severity.hpp:37