Line data Source code
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/Severity.cxx to src/Severity.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 : #include <assert.h>
13 : #include <sstream>
14 : #include <ers/ers.hpp>
15 :
16 : namespace
17 : {
18 : const char * SeverityNames[] =
19 : {
20 : "DEBUG",
21 : "LOG",
22 : "INFO",
23 : "WARNING",
24 : "ERROR",
25 : "FATAL"
26 : };
27 : }
28 :
29 76 : ERS_DECLARE_ISSUE( ers,
30 : BadSeverity,
31 : "string \"" << severity << "\" does not contain valid severity",
32 : ((std::string)severity) )
33 :
34 :
35 : /**
36 : * \brief Transforms a severity type into the corresponding string
37 : * \param s severity
38 : * \return reference to string with associated text
39 : */
40 : std::string
41 57 : ers::to_string( ers::severity severity )
42 : {
43 57 : assert( ers::Debug <= severity && severity <= ers::Fatal );
44 57 : return SeverityNames[severity];
45 : }
46 :
47 : /**
48 : * \brief Transforms a severity type into the corresponding string
49 : * \param s severity
50 : * \return pointer to string with associated text
51 : */
52 : std::string
53 1076 : ers::to_string( ers::Severity severity )
54 : {
55 1076 : assert( ers::Debug <= severity && severity <= ers::Fatal );
56 :
57 1076 : if ( severity.type == ers::Debug )
58 : {
59 0 : std::ostringstream out;
60 0 : out << SeverityNames[severity] << "_" << severity.rank;
61 0 : return out.str();
62 0 : }
63 1076 : return SeverityNames[severity.type];
64 : }
65 :
66 : /** Parses a string and extracts a severity
67 : * \param s the string to parse
68 : * \return a severity value
69 : */
70 : ers::severity
71 0 : ers::parse( const std::string & string, ers::severity & s )
72 : {
73 0 : for( short ss = ers::Debug; ss <= ers::Fatal; ++ss )
74 : {
75 0 : if ( string == SeverityNames[ss] )
76 : {
77 0 : return ( s = (ers::severity)ss );
78 : }
79 : }
80 0 : throw ers::BadSeverity( ERS_HERE, string );
81 : }
82 :
83 : /** Parses a string and extracts a severity
84 : * \param s the string to parse
85 : * \return a severity value
86 : */
87 : ers::Severity
88 0 : ers::parse( const std::string & string, ers::Severity & s )
89 : {
90 0 : int level = 0;
91 0 : ers::severity type;
92 0 : std::string::size_type pos = string.find( '_' );
93 0 : if ( pos )
94 : {
95 0 : std::istringstream in( string.substr( pos + 1 ) );
96 0 : in >> level;
97 0 : ers::parse( string.substr( 0, pos ), type );
98 0 : }
99 : else
100 : {
101 0 : ers::parse( string, type );
102 : }
103 0 : return ( s = ers::Severity( type, level ) );
104 : }
105 :
|