Skip to content

Commit 47c3e9c

Browse files
Renaming to improve self-descriptiveness.
1. GLOG defaults (log_dir=None) to outputing to color-enhanced std::cerr 2. Specifying valid log_dir changes the mode to logging 3. Context manager renamed from verbose to syclinterface_diagnostic Test updated.
1 parent 2f6aaec commit 47c3e9c

File tree

5 files changed

+60
-42
lines changed

5 files changed

+60
-42
lines changed

.flake8

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ exclude =
1313
.git
1414

1515
per-file-ignores =
16-
dpctl/_dev.pyx: E999
16+
dpctl/_diagnostics.pyx: E999
1717
dpctl/_sycl_context.pyx: E999, E225, E227
1818
dpctl/_sycl_device.pyx: E999, E225
1919
dpctl/_sycl_device_factory.pyx: E999, E225

dpctl/_dev.pyx renamed to dpctl/_diagnostics.pyx

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,49 +29,52 @@ cdef extern from "syclinterface/dpctl_service.h":
2929
cdef void DPCTLService_ShutdownLogger()
3030

3131

32-
def init_logger(log_dir=None):
32+
def _init_logger(log_dir=None):
3333
"""Initialize logger to use given directory to save logs.
3434
3535
The call has no effect if `dpctl` was not built to use logger.
3636
"""
3737
cdef bytes p = b""
3838
cdef const char *app_name = "dpctl"
39-
if log_dir is None:
40-
log_dir = os.getcwd()
41-
if not os.path.exists(log_dir):
42-
raise ValueError(f"Path {log_dir} does not exist")
43-
if isinstance(log_dir, str):
44-
p = bytes(log_dir, "utf-8")
45-
else:
46-
p = bytes(log_dir)
47-
DPCTLService_InitLogger(app_name, <char *>p)
39+
cdef char *ld_cstr = NULL
40+
if log_dir:
41+
if not os.path.exists(log_dir):
42+
raise ValueError(f"Path {log_dir} does not exist")
43+
if isinstance(log_dir, str):
44+
p = bytes(log_dir, "utf-8")
45+
else:
46+
p = bytes(log_dir)
47+
ld_cstr = <char *>p
48+
DPCTLService_InitLogger(app_name, ld_cstr)
4849

4950

50-
def fini_logger():
51-
"""Finilize logger.
51+
def _shutdown_logger():
52+
"""Finalize logger.
5253
5354
The call has no effect if `dpctl` was not built to use logger.
5455
"""
5556
DPCTLService_ShutdownLogger()
5657

5758

5859
@contextlib.contextmanager
59-
def verbose(verbosity="warning", log_dir=None):
60-
"""Context manager that activate verbosity"""
60+
def syclinterface_diagnostics(verbosity="warning", log_dir=None):
61+
"""Context manager that activate verbosity of DPCTLSyclInterface
62+
function calls.
63+
"""
6164
_allowed_verbosity = ["warning", "error"]
6265
if not verbosity in _allowed_verbosity:
6366
raise ValueError(
6467
f"Verbosity argument not understood. "
6568
f"Permitted values are {_allowed_verbosity}"
6669
)
67-
init_logger(log_dir=log_dir)
68-
_saved = os.environ.get("DPCTL_VERBOSITY", None)
70+
_init_logger(log_dir=log_dir)
71+
_saved_verbosity = os.environ.get("DPCTL_VERBOSITY", None)
6972
os.environ["DPCTL_VERBOSITY"] = verbosity
7073
try:
7174
yield
7275
finally:
73-
fini_logger()
74-
if _saved:
75-
os.environ["DPCTL_VERBOSITY"] = _saved
76+
_shutdown_logger()
77+
if _saved_verbosity:
78+
os.environ["DPCTL_VERBOSITY"] = _saved_verbosity
7679
else:
7780
del os.environ["DPCTL_VERBOSITY"]

dpctl/tests/test_service.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,17 +110,22 @@ def test___version__():
110110

111111

112112
def test_dev_utils():
113-
import dpctl._dev as dd
113+
import tempfile
114114

115-
with dd.verbose():
115+
import dpctl._diagnostics as dd
116+
117+
ctx_mngr = dd.syclinterface_diagnostics
118+
119+
with ctx_mngr():
116120
dpctl.SyclDevice().parent_device
117-
with dd.verbose(verbosity="error"):
121+
with ctx_mngr(verbosity="error"):
118122
dpctl.SyclDevice().parent_device
119123
with pytest.raises(ValueError):
120-
with dd.verbose(verbosity="blah"):
124+
with ctx_mngr(verbosity="blah"):
125+
dpctl.SyclDevice().parent_device
126+
with tempfile.TemporaryDirectory() as temp_dir:
127+
with ctx_mngr(log_dir=temp_dir):
121128
dpctl.SyclDevice().parent_device
122-
with dd.verbose(log_dir="/tmp"):
123-
dpctl.SyclDevice().parent_device
124129
with pytest.raises(ValueError):
125-
with dd.verbose(log_dir="/not_a_dir"):
130+
with ctx_mngr(log_dir="/not_a_dir"):
126131
dpctl.SyclDevice().parent_device

libsyclinterface/helper/source/dpctl_error_handlers.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,30 @@ int requested_verbosity_level(void)
6565
return requested_level;
6666
}
6767

68-
void output_message(std::string ss_str, [[maybe_unused]] error_level error_type)
68+
void output_message(std::string ss_str, error_level error_type)
6969
{
7070
#ifdef ENABLE_GLOG
7171
switch (error_type) {
7272
case error_level::error:
73-
LOG(ERROR) << ss_str;
73+
LOG(ERROR) << "[ERR] " << ss_str;
7474
break;
7575
case error_level::warning:
76-
LOG(WARNING) << ss_str;
76+
LOG(WARNING) << "[WARN] " << ss_str;
7777
break;
7878
default:
79-
LOG(FATAL) << ss_str;
79+
LOG(FATAL) << "[FATAL] " << ss_str;
8080
}
8181
#else
82-
std::cerr << ss_str;
82+
switch (error_type) {
83+
case error_level::error:
84+
std::cerr << "[ERR] " << ss_str;
85+
break;
86+
case error_level::warning:
87+
std::cerr << "[WARN] " << ss_str;
88+
break;
89+
default:
90+
std::cerr << "[FATAL] " << ss_str;
91+
}
8392
#endif
8493
}
8594

libsyclinterface/source/dpctl_service.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,22 @@ __dpctl_give const char *DPCTLService_GetDPCPPVersion(void)
4646
void DPCTLService_InitLogger(const char *app_name, const char *log_dir)
4747
{
4848
google::InitGoogleLogging(app_name);
49-
google::EnableLogCleaner(0);
5049
google::InstallFailureSignalHandler();
51-
FLAGS_colorlogtostderr = true;
52-
FLAGS_stderrthreshold = google::FATAL;
5350

54-
namespace fs = std::filesystem;
55-
const fs::path path(log_dir);
56-
std::error_code ec;
51+
if (log_dir) {
52+
namespace fs = std::filesystem;
53+
const fs::path path(log_dir);
54+
std::error_code ec;
5755

58-
if (fs::is_directory(path, ec)) {
59-
FLAGS_log_dir = log_dir;
56+
if (fs::is_directory(path, ec)) {
57+
google::EnableLogCleaner(0);
58+
FLAGS_log_dir = log_dir;
59+
}
6060
}
6161
else {
62-
std::cerr << "Directory not found. Log files will be created in the "
63-
"default logging directory.\n";
62+
FLAGS_colorlogtostderr = true;
63+
FLAGS_stderrthreshold = google::FATAL;
64+
FLAGS_logtostderr = 1;
6465
}
6566
}
6667

0 commit comments

Comments
 (0)