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