xref: /aosp_15_r20/external/ComputeLibrary/tests/framework/Exceptions.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
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 {
log_level_from_name(const std::string & name)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 
operator >>(::std::istream & stream,LogLevel & level)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 
operator <<(::std::ostream & stream,LogLevel level)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;
87         case LogLevel::MEASUREMENTS:
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 
to_string(LogLevel level)100 std::string to_string(LogLevel level)
101 {
102     std::stringstream stream;
103     stream << level;
104     return stream.str();
105 }
106 
FileNotFound(const std::string & msg)107 FileNotFound::FileNotFound(const std::string &msg)
108     : std::runtime_error{ msg }
109 {
110 }
111 
TestError(const std::string & msg,LogLevel level,std::string context)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 
level() const121 LogLevel TestError::level() const
122 {
123     return _level;
124 }
125 
what() const126 const char *TestError::what() const noexcept
127 {
128     return _combined.c_str();
129 }
130 
131 } // namespace framework
132 } // namespace test
133 } // namespace arm_compute
134