Compute Library
 22.05
Exceptions.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #include "Exceptions.h"
25 
26 #include "Utils.h"
27 
28 #include <map>
29 #include <sstream>
30 
31 namespace arm_compute
32 {
33 namespace test
34 {
35 namespace framework
36 {
37 LogLevel log_level_from_name(const std::string &name)
38 {
39  static const std::map<std::string, LogLevel> levels =
40  {
41  { "none", LogLevel::NONE },
42  { "config", LogLevel::CONFIG },
43  { "tests", LogLevel::TESTS },
44  { "errors", LogLevel::ERRORS },
45  { "debug", LogLevel::DEBUG },
46  { "measurements", LogLevel::MEASUREMENTS },
47  { "all", LogLevel::ALL },
48  };
49 
50  try
51  {
52  return levels.at(tolower(name));
53  }
54  catch(const std::out_of_range &)
55  {
56  throw std::invalid_argument(name);
57  }
58 }
59 
60 ::std::istream &operator>>(::std::istream &stream, LogLevel &level)
61 {
62  std::string value;
63  stream >> value;
64  level = log_level_from_name(value);
65  return stream;
66 }
67 
68 ::std::ostream &operator<<(::std::ostream &stream, LogLevel level)
69 {
70  switch(level)
71  {
72  case LogLevel::NONE:
73  stream << "NONE";
74  break;
75  case LogLevel::CONFIG:
76  stream << "CONFIG";
77  break;
78  case LogLevel::TESTS:
79  stream << "TESTS";
80  break;
81  case LogLevel::ERRORS:
82  stream << "ERRORS";
83  break;
84  case LogLevel::DEBUG:
85  stream << "DEBUG";
86  break;
88  stream << "MEASUREMENTS";
89  break;
90  case LogLevel::ALL:
91  stream << "ALL";
92  break;
93  default:
94  throw std::invalid_argument("Unsupported log level");
95  }
96 
97  return stream;
98 }
99 
101 {
102  std::stringstream stream;
103  stream << level;
104  return stream.str();
105 }
106 
107 FileNotFound::FileNotFound(const std::string &msg)
108  : std::runtime_error{ msg }
109 {
110 }
111 
112 TestError::TestError(const std::string &msg, LogLevel level, std::string context)
113  : std::runtime_error{ msg }, _level{ level }, _msg{ msg }, _context{ std::move(context) }, _combined{ "ERROR: " + msg }
114 {
115  if(!_context.empty())
116  {
117  _combined += "\nCONTEXT:\n" + _context;
118  }
119 }
120 
122 {
123  return _level;
124 }
125 
126 const char *TestError::what() const noexcept
127 {
128  return _combined.c_str();
129 }
130 
131 } // namespace framework
132 } // namespace test
133 } // namespace arm_compute
std::string to_string(DatasetMode mode)
Definition: DatasetModes.h:100
FileNotFound(const std::string &msg)
Construct error with message.
Definition: Exceptions.cpp:107
std::string tolower(std::string string)
Convert string to lower case.
Definition: Utils.h:147
LogLevel
Severity of the information.
Definition: Exceptions.h:50
const char * what() const noexcept override
Get the error message.
Definition: Exceptions.cpp:126
Copyright (c) 2017-2022 Arm Limited.
inline ::std::istream & operator>>(::std::istream &stream, DatasetMode &mode)
Definition: DatasetModes.h:69
LogLevel level() const
Severity of the error.
Definition: Exceptions.cpp:121
const char * name
LogLevel log_level_from_name(const std::string &name)
Definition: Exceptions.cpp:37
inline ::std::ostream & operator<<(::std::ostream &stream, DatasetMode mode)
Definition: DatasetModes.h:77
TestError(const std::string &msg, LogLevel level, std::string context="")
Construct error with severity.
Definition: Exceptions.cpp:112