Skip to content

Commit 6b03ba8

Browse files
Added debug logs configuration
1 parent 9adeab8 commit 6b03ba8

File tree

3 files changed

+36
-15
lines changed

3 files changed

+36
-15
lines changed

include/gc/Utils/Error.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ namespace mlir::gc::err {
3333

3434
template <typename... Args>
3535
[[nodiscard]] llvm::Error makeLlvmError(GC_ERR_LOC_DECL Args... args) {
36-
log::insetLog(GC_ERR_LOC_ARGS std::cerr, "ERROR", args...);
36+
log::log(GC_ERR_LOC_ARGS std::cerr, "ERROR", args...);
3737
std::ostringstream oss;
3838
log::insertArgs(oss, args...);
3939
auto msg = oss.str();
@@ -42,7 +42,7 @@ template <typename... Args>
4242
}
4343

4444
[[noreturn]] static void report(GC_ERR_LOC_DECL llvm::Error err) {
45-
log::insetLog(GC_ERR_LOC_ARGS std::cerr, "ERROR", "Unrecoverable error!");
45+
log::log(GC_ERR_LOC_ARGS std::cerr, "ERROR", "Unrecoverable error!");
4646
report_fatal_error(std::move(err));
4747
}
4848

include/gc/Utils/Log.h

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
#define GC_LOG_H
1111
#include <iostream>
1212

13+
#ifndef NDEBUG
14+
#include <regex>
15+
#endif
16+
1317
namespace mlir::gc::log {
1418
static void insertArgs(std::ostream &stream) { stream << std::endl; }
1519

@@ -20,7 +24,7 @@ static void insertArgs(std::ostream &stream, T first, Args... args) {
2024
}
2125

2226
template <typename... Args>
23-
static void insetLog(
27+
static void log(
2428
#ifndef NDEBUG
2529
const char *fileName, int lineNum,
2630
#endif
@@ -36,16 +40,35 @@ static void insetLog(
3640
#define gcLogD(...)
3741
#define gcLogE(...) mlir::gc::log::insetLog(std::cerr, "ERROR", __VA_ARGS__)
3842
#else
39-
#define _insetLog(stream, pref, ...) \
40-
mlir::gc::log::insetLog(__FILE__, __LINE__, stream, pref, __VA_ARGS__)
41-
#define gcLogD(...) _insetLog(std::cout, "DEBUG", __VA_ARGS__)
42-
#define gcLogE(...) _insetLog(std::cerr, "ERROR", __VA_ARGS__)
43-
#endif
44-
} // namespace mlir::gc::log
4543

46-
#ifdef GC_LOG_NO_DEBUG
47-
#undef gcLogD
48-
#define gcLogD(...)
44+
// The debug logs are enabled by setting the environment variable GC_DEBUG to a
45+
// regex pattern. The pattern is matched against the file name where the log is
46+
// called. Examples:
47+
// GC_DEBUG=.* - Enable all debug logs.
48+
// GC_DEBUG=/(CPU|GPU)Runtime/ - Enable debug logs in files containing
49+
// CPURuntime or GPURuntime in the path.
50+
static bool isDebugEnabled(const char *fileName) {
51+
static std::regex pattern = []() {
52+
auto env = std::getenv("GC_DEBUG");
53+
return env ? std::regex(env, std::regex::extended)
54+
: std::regex("", std::regex::basic);
55+
}();
56+
// The flag 'basic' is used as a marker for an empty regex.
57+
return pattern.flags() != std::regex::basic &&
58+
std::regex_search(fileName, pattern);
59+
}
60+
61+
template <typename... Args>
62+
static void debug(const char *fileName, int lineNum, Args... args) {
63+
if (isDebugEnabled(fileName)) {
64+
log(fileName, lineNum, std::cout, "DEBUG", args...);
65+
}
66+
}
67+
68+
#define gcLogD(...) mlir::gc::log::debug(__FILE__, __LINE__, __VA_ARGS__)
69+
#define gcLogE(...) \
70+
mlir::gc::log::log(__FILE__, __LINE__, std::cerr, "ERROR", __VA_ARGS__)
4971
#endif
72+
} // namespace mlir::gc::log
5073

5174
#endif

lib/gc/ExecutionEngine/GPURuntime/ocl/GpuOclRuntime.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
#include <CL/cl_ext.h>
1010

11-
// Comment out the following line to enable debug logging
12-
// #define GC_LOG_NO_DEBUG
1311
#include "gc/ExecutionEngine/GPURuntime/GpuOclRuntime.h"
1412
#include "gc/Transforms/Passes.h"
1513
#include "gc/Utils/Error.h"
@@ -569,7 +567,7 @@ bool OclRuntime::isUsm(const void *ptr) const {
569567
#ifndef NDEBUG
570568
void OclRuntime::debug(const char *file, int line, const char *msg) {
571569
#ifndef GC_LOG_NO_DEBUG
572-
mlir::gc::log::insetLog(file, line, std::cout, "DEBUG", msg);
570+
log::debug(file, line, msg);
573571
#endif
574572
}
575573
#endif

0 commit comments

Comments
 (0)