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 <map>
30#include <string>
31#include <iostream>
32#include <sstream>
33#include <memory>
34#include <chrono>
35#include <list>
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
50{
51 class OutputStream;
52
53 typedef std::map<std::string, std::string> string_map;
54
55 using inheritance_type = std::list<std::string> ;
56
57 template<class T>
59 public:
63
64 private:
65 static ers::Issue* create(const Context &context) {
66 return new T(context);
67 }
68 };
69
79 class Issue : public std::exception
80 {
81 friend class IssueFactory;
82
83 public:
84 Issue( const Context & context,
85 const std::string & message = std::string() );
86
87 Issue( const Context & context,
88 const std::exception & cause );
89
90 Issue( const Context & context,
91 const std::string & message,
92 const std::exception & cause );
93
94 Issue( const Issue & other );
95
96 virtual ~Issue() noexcept;
97
98 virtual Issue * clone() const = 0;
99
100 virtual const char * get_class_name() const = 0;
101
103
104 virtual void raise() const = 0;
105
106 void add_qualifier( const std::string & qualif );
107
108 const Issue * cause() const
109 { return m_cause.get(); }
110
111 const Context & context() const
112 { return *(m_context.get()); }
113
114 const std::string & message() const
115 { return m_message; }
116
117 const std::vector<std::string> & qualifiers() const
118 { return m_qualifiers; }
119
120 const string_map & parameters() const
121 { return m_values; }
122
124 { return m_severity; }
125
126 template <class Precision=std::chrono::seconds>
127 std::string time(const std::string & format = "%Y-%b-%d %H:%M:%S", bool isUTC = false) const;
128
130 template <class Precision>
131 std::string localtime(const std::string & format = "%Y-%b-%d %H:%M:%S") const
132 { return time<Precision>(format, false); }
133
135 template <class Precision>
136 std::string gmtime(const std::string & format = "%Y-%b-%d %H:%M:%S") const
137 { return time<Precision>(format, true); }
138
139 std::time_t time_t() const;
140
141 const system_clock::time_point & ptime() const
142 { return m_time; }
143
144 const char * what() const noexcept
145 { return m_message.c_str(); }
146
148
149 void wrap_message( const std::string & begin, const std::string & end );
150
151 protected:
153 const system_clock::time_point & time,
154 const ers::Context & context,
155 const std::string & message,
156 const std::vector<std::string> & qualifiers,
157 const std::map<std::string, std::string> & parameters,
158 const ers::Issue * cause = 0 );
159
161 template <typename T>
162 void get_value( const std::string & key, T & value ) const;
163 void get_value( const std::string & key, const char * & value ) const;
164 void get_value( const std::string & key, std::string & value ) const;
165
167 template <typename T>
168 void set_value( const std::string & key, T value );
169
170 void set_message( const std::string & message )
171 { m_message = message; }
172
173 void prepend_message( const std::string & message );
174
175 static auto _get_inheritance() {
176 inheritance_type chain;
177 chain.push_back( "ers::Issue" ) ;
178 return chain;
179 }
180
181 private:
182 Issue & operator=( const Issue & other ) = delete;
183
184 std::unique_ptr<const Issue> m_cause;
185 std::unique_ptr<Context> m_context;
186 std::string m_message;
187 std::vector<std::string> m_qualifiers;
189 system_clock::time_point m_time;
191 };
192
193 std::ostream & operator<<( std::ostream &, const ers::Issue & );
194
195} // ers
196
197
199
201 NoValue,
202 "value for the \"" << key << "\" key is not set ",
203 ((std::string)key ) )
204
205template <typename T>
206void
207ers::Issue::get_value( const std::string & key, T & value ) const
208{
209 string_map::const_iterator it = m_values.find(key);
210 if ( it == m_values.end() )
211 {
212 throw ers::NoValue( ERS_HERE, key );
213 }
214 std::istringstream in( it->second );
215 in >> value;
216}
217
218template <typename T>
219void
220ers::Issue::set_value( const std::string & key, T value )
221{
222 std::ostringstream out;
223 out << value;
224 m_values[key] = out.str();
225}
226
227template <class Precision>
228std::string
229ers::Issue::time(const std::string & format, bool isUTC) const
230{
231 static const int width(::log10(Precision::period::den));
232
233 std::time_t t = time_t();
234 std::tm tm = isUTC ? *gmtime_r(&t, &tm) : *localtime_r(&t, &tm);
235
236 char buff[128];
237 std::strftime(buff, 128 - 16, format.c_str(), &tm);
238
239 auto c = std::chrono::duration_cast<Precision>(
240 m_time.time_since_epoch()).count();
241 double frac = c - (double)t*Precision::period::den;
242 sprintf(buff + strlen(buff), ",%0*.0f", width, frac);
243
244 return buff;
245}
246
247#endif
248
#define ERS_HERE
An abstract interface to access an Issue context.
Definition Context.hpp:41
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:65
Base class for any user define issue.
Definition Issue.hpp:80
const Context & context() const
Context of the issue.
Definition Issue.hpp:111
ers::Severity severity() const
severity of the issue
Definition Issue.hpp:123
Severity m_severity
Issue's severity.
Definition Issue.hpp:188
void wrap_message(const std::string &begin, const std::string &end)
Definition Issue.cpp:212
std::unique_ptr< Context > m_context
Context of the current issue.
Definition Issue.hpp:185
virtual ~Issue() noexcept
Definition Issue.cpp:142
Issue & operator=(const Issue &other)=delete
void prepend_message(const std::string &message)
Definition Issue.cpp:202
system_clock::time_point m_time
Time when issue was thrown.
Definition Issue.hpp:189
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:141
void set_message(const std::string &message)
Definition Issue.hpp:170
void get_value(const std::string &key, T &value) const
const std::vector< std::string > & qualifiers() const
return array of qualifiers
Definition Issue.hpp:117
const std::string & message() const
General cause of the issue.
Definition Issue.hpp:114
std::string gmtime(const std::string &format="%Y-%b-%d %H:%M:%S") const
Definition Issue.hpp:136
const string_map & parameters() const
return array of parameters
Definition Issue.hpp:120
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:229
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:131
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:184
friend class IssueFactory
Definition Issue.hpp:81
void set_value(const std::string &key, T value)
string_map m_values
List of user defined attributes.
Definition Issue.hpp:190
std::string m_message
Issue's explanation text.
Definition Issue.hpp:186
void add_qualifier(const std::string &qualif)
adds a qualifier to the issue
Definition Issue.cpp:183
virtual inheritance_type get_class_inheritance() const =0
Get inheritance chain.
const char * what() const noexcept
General cause of the issue.
Definition Issue.hpp:144
ers::Severity set_severity(ers::Severity severity) const
Definition Issue.cpp:191
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:187
Issue(const Context &context, const std::string &message=std::string())
Definition Issue.cpp:80
static auto _get_inheritance()
Definition Issue.hpp:175
const Issue * cause() const
return the cause Issue of this Issue
Definition Issue.hpp:108
ERS abstract output stream interface.
#define ERS_DECLARE_ISSUE( namespace_name, class_name, message_, attributes)
Definition macro.hpp:65
FELIX Initialization std::string initerror FELIX queue timed out
std::list< std::string > inheritance_type
Definition Issue.hpp:55
message(message)
Definition __init__.py:84
std::map< std::string, std::string > string_map
Definition Issue.hpp:53
std::ostream & operator<<(std::ostream &, const ers::Configuration &)
severity
Definition Severity.hpp:37