blob: e56366c9ab16a4f3b6ed74902e1bcb655ec3df7f [file] [log] [blame]
Anthony Barbier6db0ff52018-01-05 10:59:12 +00001/*
Georgios Pinitas40f51a62020-11-21 03:04:18 +00002 * Copyright (c) 2018-2020 Arm Limited.
Anthony Barbier6db0ff52018-01-05 10:59:12 +00003 *
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 "CommonOptions.h"
25
26#include "../Framework.h"
27#include "../printers/Printers.h"
Pablo Tello4e66d702022-03-07 18:20:12 +000028#if !defined(_WIN64)
Anthony Barbier46edf632018-01-26 14:27:15 +000029#include <unistd.h>
Pablo Tello4e66d702022-03-07 18:20:12 +000030#endif // !defined(_WIN64)
Anthony Barbier6db0ff52018-01-05 10:59:12 +000031
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010032using namespace arm_compute::utils;
33
Anthony Barbier6db0ff52018-01-05 10:59:12 +000034namespace arm_compute
35{
36namespace test
37{
38namespace framework
39{
40CommonOptions::CommonOptions(CommandLineParser &parser)
41 : help(parser.add_option<ToggleOption>("help")),
42 instruments(),
43 iterations(parser.add_option<SimpleOption<int>>("iterations", 1)),
Anthony Barbier6db0ff52018-01-05 10:59:12 +000044 log_format(),
45 log_file(parser.add_option<SimpleOption<std::string>>("log-file")),
46 log_level(),
47 throw_errors(parser.add_option<ToggleOption>("throw-errors")),
Pablo Tello4e66d702022-03-07 18:20:12 +000048#if !defined(_WIN64)
49 color_output(parser.add_option<ToggleOption>("color-output", isatty(STDOUT_FILENO))), // Only enable colors by default if we're running in a terminal
50#endif // !defined(_WIN64)
Anthony Barbier6db0ff52018-01-05 10:59:12 +000051 pretty_console(parser.add_option<ToggleOption>("pretty-console", false)),
52 json_file(parser.add_option<SimpleOption<std::string>>("json-file")),
53 pretty_file(parser.add_option<SimpleOption<std::string>>("pretty-file")),
54 log_streams()
55{
56 Framework &framework = Framework::get();
57 std::set<InstrumentsDescription> allowed_instruments
58 {
59 std::pair<InstrumentType, ScaleFactor>(InstrumentType::ALL, ScaleFactor::NONE),
60 std::pair<InstrumentType, ScaleFactor>(InstrumentType::NONE, ScaleFactor::NONE),
61 };
62
63 for(const auto &type : framework.available_instruments())
64 {
65 allowed_instruments.insert(type);
66 }
67
68 std::set<LogFormat> supported_log_formats
69 {
70 LogFormat::NONE,
71 LogFormat::PRETTY,
72 LogFormat::JSON,
73 };
74
75 std::set<LogLevel> supported_log_levels
76 {
77 LogLevel::NONE,
78 LogLevel::CONFIG,
79 LogLevel::TESTS,
80 LogLevel::ERRORS,
81 LogLevel::DEBUG,
82 LogLevel::MEASUREMENTS,
83 LogLevel::ALL,
84 };
85
86 instruments = parser.add_option<EnumListOption<InstrumentsDescription>>("instruments", allowed_instruments, std::initializer_list<InstrumentsDescription> { std::pair<InstrumentType, ScaleFactor>(InstrumentType::WALL_CLOCK_TIMER, ScaleFactor::NONE) });
87 log_format = parser.add_option<EnumOption<LogFormat>>("log-format", supported_log_formats, LogFormat::PRETTY);
88 log_level = parser.add_option<EnumOption<LogLevel>>("log-level", supported_log_levels, LogLevel::ALL);
89
90 help->set_help("Show this help message");
91 instruments->set_help("Set the profiling instruments to use");
92 iterations->set_help("Number of iterations per test case");
Anthony Barbier6db0ff52018-01-05 10:59:12 +000093 log_format->set_help("Output format for measurements and failures (affects only log-file)");
94 log_file->set_help("Write output to file instead of to the console (affected by log-format)");
95 log_level->set_help("Verbosity of the output");
96 throw_errors->set_help("Don't catch fatal errors (useful for debugging)");
97 color_output->set_help("Produce colored output on the console");
98 pretty_console->set_help("Produce pretty output on the console");
99 json_file->set_help("Write output to a json file.");
100 pretty_file->set_help("Write output to a text file");
101}
102std::vector<std::unique_ptr<Printer>> CommonOptions::create_printers()
103{
104 std::vector<std::unique_ptr<Printer>> printers;
105
106 if(pretty_console->value() && (log_file->is_set() || log_format->value() != LogFormat::PRETTY))
107 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000108 auto pretty_printer = std::make_unique<PrettyPrinter>();
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000109 pretty_printer->set_color_output(color_output->value());
110 printers.push_back(std::move(pretty_printer));
111 }
112
113 std::unique_ptr<Printer> printer;
114 switch(log_format->value())
115 {
116 case LogFormat::JSON:
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000117 printer = std::make_unique<JSONPrinter>();
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000118 break;
119 case LogFormat::NONE:
120 break;
121 case LogFormat::PRETTY:
122 default:
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000123 auto pretty_printer = std::make_unique<PrettyPrinter>();
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000124 // Don't use colours if we print to a file:
125 pretty_printer->set_color_output((!log_file->is_set()) && color_output->value());
126 printer = std::move(pretty_printer);
127 break;
128 }
129
130 if(log_file->is_set())
131 {
132 log_streams.push_back(std::make_shared<std::ofstream>(log_file->value()));
133 if(printer != nullptr)
134 {
135 printer->set_stream(*log_streams.back().get());
136 }
137 }
138
139 if(printer != nullptr)
140 {
141 printers.push_back(std::move(printer));
142 }
143
144 if(json_file->is_set())
145 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000146 printers.push_back(std::make_unique<JSONPrinter>());
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000147 log_streams.push_back(std::make_shared<std::ofstream>(json_file->value()));
148 printers.back()->set_stream(*log_streams.back().get());
149 }
150
151 if(pretty_file->is_set())
152 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000153 printers.push_back(std::make_unique<PrettyPrinter>());
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000154 log_streams.push_back(std::make_shared<std::ofstream>(pretty_file->value()));
155 printers.back()->set_stream(*log_streams.back().get());
156 }
157
158 return printers;
159}
160} // namespace framework
161} // namespace test
162} // namespace arm_compute