DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
FormattedStandardStream.hxx
Go to the documentation of this file.
1/*
2 * FormattedStandardStream.i
3 * ers
4 *
5 * Created by Serguei Kolos on 02.08.07.
6 * Copyright 2007 CERN. All rights reserved.
7 *
8 */
9
10#include <iomanip>
11#include <iostream>
12#include <fstream>
13
14#define FIELD_SEPARATOR "\n\t"
15#define DELIMITER ','
16
17template <class Device>
19
20template <class Device>
22{
23 (*this)["severity"] = format::Severity;
24 (*this)["time"] = format::Time;
25 (*this)["position"] = format::Position;
26 (*this)["context"] = format::Context;
27 (*this)["pid"] = format::PID;
28 (*this)["tid"] = format::TID;
29 (*this)["user"] = format::User;
30 (*this)["cwd"] = format::CWD;
31 (*this)["function"] = format::Function;
32 (*this)["line"] = format::Line;
33 (*this)["text"] = format::Text;
34 (*this)["stack"] = format::Stack;
35 (*this)["cause"] = format::Cause;
36 (*this)["parameters"] = format::Parameters;
37 (*this)["qualifiers"] = format::Qualifiers;
38}
39
40template <class Device>
41std::string
43{
44 std::string first_token = param.substr( 0, param.find( ',' ) );
45 std::map<std::string,ers::format::Token>::iterator it = s_supported_fields.find( first_token );
46 if ( it != s_supported_fields.end() )
47 return "";
48 else
49 return first_token;
50}
51
52template <class Device>
53std::string
55{
56 std::string first_token = param.substr( 0, param.find( ',' ) );
57 std::map<std::string,ers::format::Token>::iterator it = s_supported_fields.find( first_token );
58 if ( it != s_supported_fields.end() )
59 return param;
60 else
61 return param.substr( first_token.size() < param.size() ? first_token.size() + 1 : param.size() );
62}
63
64template <class Device>
66 : Device( get_file_name( param ) )
67{
68 std::string format = get_format( param );
69
70 size_t offset = 0;
71 size_t index = std::string::npos;
72 do {
73 index = format.find( DELIMITER, offset );
74 std::string token = format.substr( offset, index - offset );
75 std::map<std::string,ers::format::Token>::iterator it = s_supported_fields.find( token );
76 if ( it != s_supported_fields.end() )
77 m_tokens.push_back( it->second );
78
79 offset += token.size() + 1;
80 } while ( index != std::string::npos );
81}
82
83template <class Device>
84void
85ers::FormattedStandardStream<Device>::report( std::ostream & out, const Issue & issue )
86{
87 for ( size_t i = 0; i < m_tokens.size(); i++ )
88 {
89 switch ( m_tokens[i] )
90 {
92 out << ers::to_string( issue.severity() );
93 break;
94 case format::Time:
95 out << issue.time<std::chrono::microseconds>() << " ";
96 break;
98 out << "[" << issue.context().position( ) << "]";
99 break;
100 case format::Function:
101 out << issue.context().function_name();
102 break;
103 case format::Line:
104 out << issue.context().line_number();
105 break;
106 case format::Text:
107 out << issue.message();
108 break;
110 {
111 out << FIELD_SEPARATOR << "Parameters = ";
112 for ( ers::string_map::const_iterator it = issue.parameters().begin(); it != issue.parameters().end(); ++it )
113 {
114 out << "'" << it->first << "=" << it->second << "' ";
115 }
116 }
117 break;
119 {
120 out << FIELD_SEPARATOR << "Qualifiers = ";
121 for ( std::vector<std::string>::const_iterator it = issue.qualifiers().begin(); it != issue.qualifiers().end(); ++it )
122 {
123 out << "'" << *it << "' ";
124 }
125 }
126 break;
127 case format::Context:
128 out << FIELD_SEPARATOR << "host = " << issue.context().host_name()
129 << FIELD_SEPARATOR << "user = " << issue.context().user_name()
130 << " (" << issue.context().user_id() << ")"
131 << FIELD_SEPARATOR << "process id = " << issue.context().process_id()
132 << FIELD_SEPARATOR << "thread id = " << issue.context().thread_id()
133 << FIELD_SEPARATOR << "process wd = " << issue.context().cwd();
134 break;
135 case format::Host:
136 out << FIELD_SEPARATOR << "host = " << issue.context().host_name();
137 break;
138 case format::User:
139 out << FIELD_SEPARATOR << "user = " << issue.context().user_name()
140 << " (" << issue.context().user_id() << ")";
141 break;
142 case format::PID:
143 out << FIELD_SEPARATOR << "process id = " << issue.context().process_id();
144 break;
145 case format::TID:
146 out << FIELD_SEPARATOR << "thread id = " << issue.context().thread_id();
147 break;
148 case format::CWD:
149 out << FIELD_SEPARATOR << "process wd = " << issue.context().cwd();
150 break;
151 case format::Stack:
152 {
153 std::vector<std::string> stack = issue.context().stack();
154 for( size_t i = 0; i < stack.size(); i++ )
155 {
156 out << FIELD_SEPARATOR << "#" << std::setw(3) << std::left << i << stack[i];
157 }
158 }
159 break;
160 case format::Cause:
161 if ( issue.cause() )
162 {
163 out << FIELD_SEPARATOR << "was caused by: ";
164 report( out, *issue.cause() );
165 }
166 break;
167 default:
168 break;
169 }
170 out << " ";
171 }
172 out << std::endl;
173}
174
175template <class Device>
176void
178{
179 report( device().stream(), issue );
180 chained().write( issue );
181}
#define DELIMITER
#define FIELD_SEPARATOR
virtual int line_number() const =0
virtual const char * user_name() const =0
virtual int user_id() const =0
virtual pid_t thread_id() const =0
virtual const char * host_name() const =0
std::string position(int verbosity=ers::Configuration::instance().verbosity_level()) const
Definition Context.cpp:105
virtual pid_t process_id() const =0
virtual const char * cwd() const =0
std::vector< std::string > stack() const
Definition Context.cpp:84
virtual const char * function_name() const =0
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
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
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
const Issue * cause() const
return the cause Issue of this Issue
Definition Issue.hpp:97
double offset
std::string to_string(severity s)
void report(std::ostream &out, const Issue &issue)
std::vector< format::Token > m_tokens
static std::string get_file_name(const std::string &param)
static std::string get_format(const std::string &param)
void write(const Issue &issue) override