Skip to content

Commit 424e766

Browse files
committed
utl: Adds source lines to Logger
This PR adds source lines to warn+ log messages to aide in developer/agentic debugging. This can be disabled by setting an env variable ``` DISABLE_SOURCE_LINES=1 ``` ``` ethanmoon@ethanmoon2:~/OpenROAD$ ./bazel-bin/openroad OpenROAD bazel-nostamp Features included (+) or not (-): -GPU -GUI -Python This program is licensed under the BSD-3 license. See the LICENSE file for details. Components of this program may be licensed under more restrictive licenses which must be honored. openroad> initialize_floorplan [ERROR IFP-0019] [bazel-out/k8-opt/bin/src/utl/swig.cc:2326] no -utilization or -die_area specified. IFP-0019 while evaluating initialize_floorplan openroad> [WARNING UTL-0014] [src/utl/src/Progress.cpp:52] OpenROAD will terminate after the next control+C ``` Signed-off-by: Ethan Mahintorabi <ethanmoon@google.com>
1 parent 5f1bd87 commit 424e766

8 files changed

Lines changed: 125 additions & 35 deletions

File tree

CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,15 @@ set(CMAKE_CXX_EXTENSIONS OFF)
7474

7575
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
7676

77+
# Strip absolute paths from __FILE__ and std::source_location
78+
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
79+
add_compile_options("-fmacro-prefix-map=${CMAKE_SOURCE_DIR}/=")
80+
81+
# Optional: If you also compile generated files in your build directory
82+
# and want them to be relative too:
83+
add_compile_options("-fmacro-prefix-map=${CMAKE_BINARY_DIR}/=")
84+
endif()
85+
7786
# Get version string in OPENROAD_VERSION
7887
if(NOT OPENROAD_VERSION)
7988
include(GetGitRevisionDescription)

src/utl/include/utl/Logger.h

Lines changed: 77 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <map>
1717
#include <memory>
1818
#include <ostream>
19+
#include <source_location>
1920
#include <sstream>
2021
#include <stack>
2122
#include <stdexcept>
@@ -95,7 +96,30 @@ class Progress;
9596
enum ToolId
9697
{
9798
FOREACH_TOOL(GENERATE_ENUM)
98-
SIZE // the number of tools, do not put anything after this
99+
SIZE // the number of tools, do not put anything after this
100+
};
101+
102+
// Captures the caller's source location alongside the log message
103+
struct LogMessage
104+
{
105+
std::string message;
106+
std::source_location loc;
107+
108+
LogMessage(const char* msg,
109+
const std::source_location& l = std::source_location::current())
110+
: message(msg), loc(l)
111+
{
112+
}
113+
LogMessage(const std::string& msg,
114+
const std::source_location& l = std::source_location::current())
115+
: message(msg), loc(l)
116+
{
117+
}
118+
LogMessage(std::string_view msg,
119+
const std::source_location& l = std::source_location::current())
120+
: message(msg), loc(l)
121+
{
122+
}
99123
};
100124

101125
class Logger
@@ -143,43 +167,57 @@ class Logger
143167
}
144168

145169
template <typename... Args>
146-
void info(ToolId tool,
147-
int id,
148-
const std::string& message,
149-
const Args&... args)
170+
void info(ToolId tool, int id, LogMessage log_message, const Args&... args)
150171
{
151-
log(tool, spdlog::level::level_enum::info, id, message, args...);
172+
log(tool,
173+
spdlog::level::level_enum::info,
174+
id,
175+
log_message.loc,
176+
log_message.message,
177+
args...);
152178
}
153179

154180
template <typename... Args>
155-
void warn(ToolId tool,
156-
int id,
157-
const std::string& message,
158-
const Args&... args)
181+
void warn(ToolId tool, int id, LogMessage log_message, const Args&... args)
159182
{
160183
warning_count_++;
161-
log(tool, spdlog::level::level_enum::warn, id, message, args...);
184+
log(tool,
185+
spdlog::level::level_enum::warn,
186+
id,
187+
log_message.loc,
188+
log_message.message,
189+
args...);
162190
}
163191

164192
template <typename... Args>
165193
__attribute__((noreturn)) void error(ToolId tool,
166194
int id,
167-
const std::string& message,
195+
LogMessage log_message,
168196
const Args&... args)
169197
{
170198
error_count_++;
171-
log(tool, spdlog::level::err, id, message, args...);
199+
log(tool,
200+
spdlog::level::err,
201+
id,
202+
log_message.loc,
203+
log_message.message,
204+
args...);
172205
// Exception should be caught by swig error handler.
173206
throw std::runtime_error(fmt::format("{}-{:04}", tool_names_[tool], id));
174207
}
175208

176209
template <typename... Args>
177210
__attribute__((noreturn)) void critical(ToolId tool,
178211
int id,
179-
const std::string& message,
212+
LogMessage log_message,
180213
const Args&... args)
181214
{
182-
log(tool, spdlog::level::level_enum::critical, id, message, args...);
215+
log(tool,
216+
spdlog::level::level_enum::critical,
217+
id,
218+
log_message.loc,
219+
log_message.message,
220+
args...);
183221
exit(EXIT_FAILURE);
184222
}
185223

@@ -277,6 +315,7 @@ class Logger
277315
void log(ToolId tool,
278316
spdlog::level::level_enum level,
279317
int id,
318+
std::source_location loc,
280319
const std::string& message,
281320
const Args&... args)
282321
{
@@ -285,13 +324,25 @@ class Logger
285324
auto& counter = message_counters_[tool][id];
286325
auto count = counter++;
287326
if (count < max_message_print) {
288-
logger_->log(level,
289-
FMT_RUNTIME("[{} {}-{:04d}] " + message
290-
+ spdlog::details::os::default_eol),
291-
level_names[level],
292-
tool_names_[tool],
293-
id,
294-
args...);
327+
if (source_lines_enabled_ && level >= spdlog::level::warn) {
328+
logger_->log(level,
329+
FMT_RUNTIME("[{} {}-{:04d}] [{}:{}] " + message
330+
+ spdlog::details::os::default_eol),
331+
level_names[level],
332+
tool_names_[tool],
333+
id,
334+
loc.file_name(),
335+
loc.line(),
336+
args...);
337+
} else {
338+
logger_->log(level,
339+
FMT_RUNTIME("[{} {}-{:04d}] " + message
340+
+ spdlog::details::os::default_eol),
341+
level_names[level],
342+
tool_names_[tool],
343+
id,
344+
args...);
345+
}
295346
return;
296347
}
297348

@@ -372,6 +423,7 @@ class Logger
372423
bool debug_on_{false};
373424
std::atomic_int warning_count_{0};
374425
std::atomic_int error_count_{0};
426+
bool source_lines_enabled_{true};
375427
static constexpr const char* level_names[]
376428
= {"TRACE", "DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL", "OFF"};
377429
static constexpr const char* pattern_ = "%v";
@@ -400,9 +452,9 @@ struct test_ostream
400452
{
401453
public:
402454
template <class T>
403-
static auto test(int) -> decltype(std::declval<std::ostream>()
404-
<< std::declval<T>(),
405-
std::true_type());
455+
static auto test(int)
456+
-> decltype(std::declval<std::ostream>() << std::declval<T>(),
457+
std::true_type());
406458

407459
template <class>
408460
static auto test(...) -> std::false_type;

src/utl/src/Logger.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <algorithm>
77
#include <atomic>
88
#include <cstdint>
9+
#include <cstdlib>
910
#include <cstring>
1011
#include <fstream>
1112
#include <memory>
@@ -53,6 +54,11 @@ Logger::Logger(const char* log_filename, const char* metrics_filename)
5354
addMetricsSink(metrics_filename);
5455
}
5556

57+
const char* disable_source_lines = std::getenv("DISABLE_SOURCE_LINES");
58+
if (disable_source_lines != nullptr) {
59+
source_lines_enabled_ = false;
60+
}
61+
5662
metrics_policies_ = MetricsPolicy::makeDefaultPolicies();
5763

5864
for (auto& counters : message_counters_) {

src/utl/src/Logger.i

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ using ord::getLogger;
3232
%include "../../Exception.i"
3333
%include "stdint.i"
3434
%import <std_string.i>
35-
35+
%ignore utl::error(utl::ToolId, int, const char*, const std::source_location);
36+
%ignore utl::warn(utl::ToolId, int, const char*, const std::source_location);
37+
%ignore utl::critical(utl::ToolId, int, const char*, const std::source_location);
3638
%include "LoggerCommon.h"
3739

3840
%inline %{

src/utl/src/LoggerCommon.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,31 @@ void info(utl::ToolId tool, int id, const char* msg)
2929
logger->info(tool, id, "{}", msg);
3030
}
3131

32-
void warn(utl::ToolId tool, int id, const char* msg)
32+
void warn(utl::ToolId tool,
33+
int id,
34+
const char* msg,
35+
const std::source_location loc)
3336
{
3437
Logger* logger = getLogger();
35-
logger->warn(tool, id, "{}", msg);
38+
logger->warn(tool, id, utl::LogMessage{"{}", loc}, msg);
3639
}
3740

38-
void error(utl::ToolId tool, int id, const char* msg)
41+
void error(utl::ToolId tool,
42+
int id,
43+
const char* msg,
44+
const std::source_location loc)
3945
{
4046
Logger* logger = getLogger();
41-
logger->error(tool, id, "{}", msg);
47+
logger->error(tool, id, utl::LogMessage{"{}", loc}, msg);
4248
}
4349

44-
void critical(utl::ToolId tool, int id, const char* msg)
50+
void critical(utl::ToolId tool,
51+
int id,
52+
const char* msg,
53+
const std::source_location loc)
4554
{
4655
Logger* logger = getLogger();
47-
logger->critical(tool, id, "{}", msg);
56+
logger->critical(tool, id, utl::LogMessage{"{}", loc}, msg);
4857
}
4958

5059
void open_metrics(const char* metrics_filename)

src/utl/src/LoggerCommon.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Copyright (c) 2022-2025, The OpenROAD Authors
33

44
#include <cstdint>
5+
#include <source_location>
56
#include <string>
67

78
#include "utl/Logger.h"
@@ -10,9 +11,18 @@ namespace utl {
1011

1112
void report(const char* msg);
1213
void info(utl::ToolId tool, int id, const char* msg);
13-
void warn(utl::ToolId tool, int id, const char* msg);
14-
void error(utl::ToolId tool, int id, const char* msg);
15-
void critical(utl::ToolId tool, int id, const char* msg);
14+
void warn(utl::ToolId tool,
15+
int id,
16+
const char* msg,
17+
const std::source_location loc = std::source_location::current());
18+
void error(utl::ToolId tool,
19+
int id,
20+
const char* msg,
21+
const std::source_location loc = std::source_location::current());
22+
void critical(utl::ToolId tool,
23+
int id,
24+
const char* msg,
25+
const std::source_location loc = std::source_location::current());
1626
void open_metrics(const char* metrics_filename);
1727
void close_metrics(const char* metrics_filename);
1828
void metric(const char* metric, const char* value);

test/regression.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export OPENROAD_EXE={OPENROAD_EXE}
3131
export REGRESSION_TEST={REGRESSION_TEST}
3232
export TEST_CHECK_LOG={TEST_CHECK_LOG}
3333
export TEST_CHECK_PASSFAIL={TEST_CHECK_PASSFAIL}
34+
export DISABLE_SOURCE_LINES=1
3435
exec "{bazel_test_sh}" "$@"
3536
""".format(
3637
bazel_test_sh = ctx.file.bazel_test_sh.short_path,

test/regression_test.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ set -o pipefail
55

66
RESULTS_DIR="${RESULTS_DIR:-results}"
77
LOG_FILE="${RESULTS_DIR}/$TEST_NAME-$TEST_EXT.log"
8+
export DISABLE_SOURCE_LINES=1
89

910
mkdir -p ${RESULTS_DIR}
1011

0 commit comments

Comments
 (0)