Merge pull request #3310 from robinsonb5-PRs/master
[yosys.git] / passes / cmds / logger.cc
1 /*
2 * yosys -- Yosys Open SYnthesis Suite
3 *
4 * Copyright (C) 2020 Miodrag Milanovic <micko@yosyshq.com>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 *
18 */
19
20 #include "kernel/register.h"
21 #include "kernel/log.h"
22
23 USING_YOSYS_NAMESPACE
24 PRIVATE_NAMESPACE_BEGIN
25
26 struct LoggerPass : public Pass {
27 LoggerPass() : Pass("logger", "set logger properties") { }
28 void help() override
29 {
30 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
31 log("\n");
32 log(" logger [options]\n");
33 log("\n");
34 log("This command sets global logger properties, also available using command line\n");
35 log("options.\n");
36 log("\n");
37 log(" -[no]time\n");
38 log(" enable/disable display of timestamp in log output.\n");
39 log("\n");
40 log(" -[no]stderr\n");
41 log(" enable/disable logging errors to stderr.\n");
42 log("\n");
43 log(" -warn regex\n");
44 log(" print a warning for all log messages matching the regex.\n");
45 log("\n");
46 log(" -nowarn regex\n");
47 log(" if a warning message matches the regex, it is printed as regular\n");
48 log(" message instead.\n");
49 log("\n");
50 log(" -werror regex\n");
51 log(" if a warning message matches the regex, it is printed as error\n");
52 log(" message instead and the tool terminates with a nonzero return code.\n");
53 log("\n");
54 log(" -[no]debug\n");
55 log(" globally enable/disable debug log messages.\n");
56 log("\n");
57 log(" -experimental <feature>\n");
58 log(" do not print warnings for the specified experimental feature\n");
59 log("\n");
60 log(" -expect <type> <regex> <expected_count>\n");
61 log(" expect log, warning or error to appear. matched errors will terminate\n");
62 log(" with exit code 0.\n");
63 log("\n");
64 log(" -expect-no-warnings\n");
65 log(" gives error in case there is at least one warning that is not expected.\n");
66 log("\n");
67 log(" -check-expected\n");
68 log(" verifies that the patterns previously set up by -expect have actually\n");
69 log(" been met, then clears the expected log list. If this is not called\n");
70 log(" manually, the check will happen at yosys exist time instead.\n");
71 log("\n");
72 }
73
74 void execute(std::vector<std::string> args, RTLIL::Design * design) override
75 {
76 size_t argidx;
77 for (argidx = 1; argidx < args.size(); argidx++)
78 {
79
80 if (args[argidx] == "-time") {
81 log_time = true;
82 log("Enabled timestamp in logs.\n");
83 continue;
84 }
85 if (args[argidx] == "-notime") {
86 log_time = false;
87 log("Disabled timestamp in logs.\n");
88 continue;
89 }
90 if (args[argidx] == "-stderr") {
91 log_error_stderr = true;
92 log("Enabled loggint errors to stderr.\n");
93 continue;
94 }
95 if (args[argidx] == "-nostderr") {
96 log_error_stderr = false;
97 log("Disabled loggint errors to stderr.\n");
98 continue;
99 }
100 if (args[argidx] == "-warn" && argidx+1 < args.size()) {
101 std::string pattern = args[++argidx];
102 if (pattern.front() == '\"' && pattern.back() == '\"') pattern = pattern.substr(1, pattern.size() - 2);
103 try {
104 log("Added regex '%s' for warnings to warn list.\n", pattern.c_str());
105 log_warn_regexes.push_back(YS_REGEX_COMPILE(pattern));
106 }
107 catch (const YS_REGEX_NS::regex_error& e) {
108 log_cmd_error("Error in regex expression '%s' !\n", pattern.c_str());
109 }
110 continue;
111 }
112 if (args[argidx] == "-nowarn" && argidx+1 < args.size()) {
113 std::string pattern = args[++argidx];
114 if (pattern.front() == '\"' && pattern.back() == '\"') pattern = pattern.substr(1, pattern.size() - 2);
115 try {
116 log("Added regex '%s' for warnings to nowarn list.\n", pattern.c_str());
117 log_nowarn_regexes.push_back(YS_REGEX_COMPILE(pattern));
118 }
119 catch (const YS_REGEX_NS::regex_error& e) {
120 log_cmd_error("Error in regex expression '%s' !\n", pattern.c_str());
121 }
122 continue;
123 }
124 if (args[argidx] == "-werror" && argidx+1 < args.size()) {
125 std::string pattern = args[++argidx];
126 if (pattern.front() == '\"' && pattern.back() == '\"') pattern = pattern.substr(1, pattern.size() - 2);
127 try {
128 log("Added regex '%s' for warnings to werror list.\n", pattern.c_str());
129 log_werror_regexes.push_back(YS_REGEX_COMPILE(pattern));
130 }
131 catch (const YS_REGEX_NS::regex_error& e) {
132 log_cmd_error("Error in regex expression '%s' !\n", pattern.c_str());
133 }
134 continue;
135 }
136 if (args[argidx] == "-debug") {
137 log_force_debug = 1;
138 log("Enabled debug log messages.\n");
139 continue;
140 }
141 if (args[argidx] == "-nodebug") {
142 log_force_debug = 0;
143 log("Disabled debug log messages.\n");
144 continue;
145 }
146 if (args[argidx] == "-experimental" && argidx+1 < args.size()) {
147 std::string value = args[++argidx];
148 log("Added '%s' experimental ignore list.\n", value.c_str());
149 log_experimentals_ignored.insert(value);
150 continue;
151 }
152 if (args[argidx] == "-expect" && argidx+3 < args.size()) {
153 std::string type = args[++argidx];
154 if (type!="error" && type!="warning" && type!="log")
155 log_cmd_error("Expect command require type to be 'log', 'warning' or 'error' !\n");
156 if (type=="error" && log_expect_error.size()>0)
157 log_cmd_error("Only single error message can be expected !\n");
158 std::string pattern = args[++argidx];
159 if (pattern.front() == '\"' && pattern.back() == '\"') pattern = pattern.substr(1, pattern.size() - 2);
160 int count = atoi(args[++argidx].c_str());
161 if (count<=0)
162 log_cmd_error("Number of expected messages must be higher then 0 !\n");
163 if (type=="error" && count!=1)
164 log_cmd_error("Expected error message occurrences must be 1 !\n");
165 log("Added regex '%s' for warnings to expected %s list.\n", pattern.c_str(), type.c_str());
166 try {
167 if (type == "error")
168 log_expect_error[pattern] = LogExpectedItem(YS_REGEX_COMPILE(pattern), count);
169 else if (type == "warning")
170 log_expect_warning[pattern] = LogExpectedItem(YS_REGEX_COMPILE(pattern), count);
171 else if (type == "log")
172 log_expect_log[pattern] = LogExpectedItem(YS_REGEX_COMPILE(pattern), count);
173 else log_abort();
174 }
175 catch (const YS_REGEX_NS::regex_error& e) {
176 log_cmd_error("Error in regex expression '%s' !\n", pattern.c_str());
177 }
178 continue;
179 }
180 if (args[argidx] == "-expect-no-warnings") {
181 log_expect_no_warnings = true;
182 continue;
183 }
184 if (args[argidx] == "-check-expected") {
185 log_check_expected();
186 continue;
187 }
188 break;
189 }
190 extra_args(args, argidx, design, false);
191 }
192 } LoggerPass;
193
194 PRIVATE_NAMESPACE_END