blob: 46b1eac97465bdd77796b4e578a89136e38bd78b [file] [log] [blame]
Moritz Pflanzer2ac50402017-07-24 15:52:54 +01001/*
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 <map>
27#include <sstream>
28
29namespace arm_compute
30{
31namespace test
32{
33namespace framework
34{
35LogLevel log_level_from_name(const std::string &name)
36{
37 static const std::map<std::string, LogLevel> levels =
38 {
39 { "none", LogLevel::NONE },
40 { "config", LogLevel::CONFIG },
41 { "tests", LogLevel::TESTS },
42 { "errors", LogLevel::ERRORS },
43 { "debug", LogLevel::DEBUG },
44 { "measurements", LogLevel::MEASUREMENTS },
45 { "all", LogLevel::ALL },
46 };
47
48 try
49 {
50 return levels.at(name);
51 }
52 catch(const std::out_of_range &)
53 {
54 throw std::invalid_argument(name);
55 }
56}
57
58::std::istream &operator>>(::std::istream &stream, LogLevel &level)
59{
60 std::string value;
61 stream >> value;
62 level = log_level_from_name(value);
63 return stream;
64}
65
66::std::ostream &operator<<(::std::ostream &stream, LogLevel level)
67{
68 switch(level)
69 {
70 case LogLevel::NONE:
71 stream << "NONE";
72 break;
73 case LogLevel::CONFIG:
74 stream << "CONFIG";
75 break;
76 case LogLevel::TESTS:
77 stream << "TESTS";
78 break;
79 case LogLevel::ERRORS:
80 stream << "ERRORS";
81 break;
82 case LogLevel::DEBUG:
83 stream << "DEBUG";
84 break;
85 case LogLevel::MEASUREMENTS:
86 stream << "MEASUREMENTS";
87 break;
88 case LogLevel::ALL:
89 stream << "ALL";
90 break;
91 default:
92 throw std::invalid_argument("Unsupported log level");
93 }
94
95 return stream;
96}
97
98std::string to_string(LogLevel level)
99{
100 std::stringstream stream;
101 stream << level;
102 return stream.str();
103}
104
Moritz Pflanzer24a82462017-08-04 11:34:44 +0100105TestError::TestError(const std::string &msg, LogLevel level, std::string context)
106 : std::runtime_error{ msg }, _level{ level }, _msg{ msg }, _context{ std::move(context) }, _combined{ "ERROR: " + msg }
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100107{
Moritz Pflanzer24a82462017-08-04 11:34:44 +0100108 if(!_context.empty())
109 {
110 _combined += "\nCONTEXT:\n" + _context;
111 }
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100112}
113
114LogLevel TestError::level() const
115{
116 return _level;
117}
Moritz Pflanzer24a82462017-08-04 11:34:44 +0100118
119const char *TestError::what() const noexcept
120{
121 return _combined.c_str();
122}
123
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100124} // namespace framework
125} // namespace test
126} // namespace arm_compute