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