DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
StandardStreamOutput.cpp
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 src/StandardStreamOutput.cxx to src/StandardStreamOutput.cpp).
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 * StandardStreamOutput.cxx
14 * ers
15 *
16 * Created by Serguei Kolos on 02.08.07.
17 * Copyright 2007 CERN. All rights reserved.
18 *
19 */
20#include <iomanip>
21
22#include <ers/Issue.hpp>
24#include <ers/Severity.hpp>
25#include <ers/internal/Util.hpp>
26
27#include <boost/algorithm/string.hpp>
28
29#define FIELD_SEPARATOR "\n\t"
30
31namespace
32{
33 std::function<std::string( const ers::Issue & issue )> getTimeFormatter()
34 {
35 static std::string format = ers::read_from_environment(
36 "DUNEDAQ_ERS_TIMESTAMP_FORMAT", "%Y-%b-%d %H:%M:%S");
37
38 static const std::string precision = ers::read_from_environment(
39 "DUNEDAQ_ERS_TIMESTAMP_PRECISION", "MILLI");
40
41 static const bool isUTC = ::getenv("DUNEDAQ_ERS_TIMESTAMP_UTC");
42
43 if ( boost::algorithm::ifind_first(precision, "NANO") ) {
44 return [](const ers::Issue & issue){
45 return issue.time<std::chrono::nanoseconds>(format, isUTC);
46 };
47 }
48
49 if ( boost::algorithm::ifind_first(precision, "MICRO") ) {
50 return [](const ers::Issue & issue){
51 return issue.time<std::chrono::microseconds>(format, isUTC);
52 };
53 }
54
55 if ( boost::algorithm::ifind_first(precision, "MILLI") ) {
56 return [](const ers::Issue & issue){
57 return issue.time<std::chrono::milliseconds>(format, isUTC);
58 };
59 }
60
61 return [](const ers::Issue & issue) {
62 return issue.time(format, isUTC);
63 };
64 }
65
66 const auto formatted_time = getTimeFormatter();
67}
68
69std::ostream &
70ers::StandardStreamOutput::println( std::ostream & out, const Issue & issue, int verbosity )
71{
72 print( out, issue, verbosity );
73 out << std::endl;
74 return out;
75}
76
77std::ostream &
78ers::StandardStreamOutput::print( std::ostream & out, const Issue & issue, int verbosity )
79{
80 if ( verbosity > -3 )
81 {
82 out << formatted_time( issue ) << " ";
83 }
84
85 if ( verbosity > -2 )
86 {
87 out << ers::to_string( issue.severity() ) << " ";
88 }
89
90 if ( verbosity > -1 )
91 {
92 out << "[" << issue.context().position( verbosity ) << "] ";
93 }
94
95 out << issue.message();
96
97 if ( verbosity > 1 )
98 {
99 out << FIELD_SEPARATOR << "Parameters = ";
100 for ( ers::string_map::const_iterator it = issue.parameters().begin(); it != issue.parameters().end(); ++it )
101 {
102 out << "'" << it->first << "=" << it->second << "' ";
103 }
104
105 out << FIELD_SEPARATOR << "Qualifiers = ";
106 for ( std::vector<std::string>::const_iterator it = issue.qualifiers().begin(); it != issue.qualifiers().end(); ++it )
107 {
108 out << "'" << *it << "' ";
109 }
110 }
111
112 if ( verbosity > 2 )
113 {
114 out << FIELD_SEPARATOR << "host = " << issue.context().host_name()
115 << FIELD_SEPARATOR << "user = " << issue.context().user_name()
116 << " (" << issue.context().user_id() << ")"
117 << FIELD_SEPARATOR << "process id = " << issue.context().process_id()
118 << FIELD_SEPARATOR << "thread id = " << issue.context().thread_id()
119 << FIELD_SEPARATOR << "process wd = " << issue.context().cwd();
120 }
121
122 if ( verbosity > 3 )
123 {
124 std::ios_base::fmtflags flags( out.flags() );
125
126 out << std::left;
127 std::vector<std::string> stack = issue.context().stack();
128 out << FIELD_SEPARATOR << "stack trace of the crashing thread:";
129 for( size_t i = 0; i < stack.size(); i++ )
130 {
131 out << FIELD_SEPARATOR << " #" << std::setw(3) << i << stack[i];
132 }
133 // restore the original state
134 out.flags( flags );
135 }
136
137 if ( issue.cause() )
138 {
139 out << FIELD_SEPARATOR << "was caused by: " << *issue.cause();
140 }
141
142 return out;
143}
#define FIELD_SEPARATOR
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:116
virtual pid_t process_id() const =0
virtual const char * cwd() const =0
std::vector< std::string > stack() const
Definition Context.cpp:95
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
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
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
const Issue * cause() const
return the cause Issue of this Issue
Definition Issue.hpp:108
int read_from_environment(const char *name, int default_value)
Definition Util.cpp:38
std::string to_string(severity s)
static std::ostream & print(std::ostream &out, const Issue &issue, int verbosity)
static std::ostream & println(std::ostream &out, const Issue &issue, int verbosity)