blob: 54936124a3c12099263338b15940ac363a49812a [file] [log] [blame]
Moritz Pflanzer2ac50402017-07-24 15:52:54 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017 Arm Limited.
Moritz Pflanzer2ac50402017-07-24 15:52:54 +01003 *
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
Moritz Pflanzer52800712017-08-30 16:27:14 +010026#include "Utils.h"
27
Moritz Pflanzer2ac50402017-07-24 15:52:54 +010028#include <map>
29#include <sstream>
30
31namespace arm_compute
32{
33namespace test
34{
35namespace framework
36{
37LogLevel 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 {
Moritz Pflanzer52800712017-08-30 16:27:14 +010052 return levels.at(tolower(name));
Moritz Pflanzer2ac50402017-07-24 15:52:54 +010053 }
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;
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
100std::string to_string(LogLevel level)
101{
102 std::stringstream stream;
103 stream << level;
104 return stream.str();
105}
106
Anthony Barbierf6705ec2017-09-28 12:01:10 +0100107FileNotFound::FileNotFound(const std::string &msg)
108 : std::runtime_error{ msg }
109{
110}
111
Moritz Pflanzer24a82462017-08-04 11:34:44 +0100112TestError::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 }
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100114{
Moritz Pflanzer24a82462017-08-04 11:34:44 +0100115 if(!_context.empty())
116 {
117 _combined += "\nCONTEXT:\n" + _context;
118 }
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100119}
120
121LogLevel TestError::level() const
122{
123 return _level;
124}
Moritz Pflanzer24a82462017-08-04 11:34:44 +0100125
126const char *TestError::what() const noexcept
127{
128 return _combined.c_str();
129}
130
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100131} // namespace framework
132} // namespace test
133} // namespace arm_compute