Skip to content

Commit 771041b

Browse files
Fixes for DPCTLService_InitLogger
Replaced use of `stat` function with use of `filesystem::is_directory`. Stat was always erroring out even for path to existing directory. Created `dpctl._dev` module for developers. It adds ```python dpctl._dev.init_logger(log_dir=None) ``` Function to initialize the logger. It uses `"dpctl"` for application name, and supplied log directory. `log_dir=None` is interpreted as use the current working directory. ```pyton dpctl._dev.fini_logger() ``` Call ShutdownLogger. ```python dpctl._dev.verbose(verbostiy="warning", log_dir=None) ``` Context manager which initializes the logger, and sets the verbosity level for a given block of Python code: Example: ``` In [1]: import dpctl, dpctl._dev as dd In [2]: with dd.verbose(): dpctl.SyclDevice().parent_device No parent for device because it is not a subdevice -33 (CL_INVALID_DEVICE) in DPCTLDevice_GetParentDevice at /localdisk/work/opavlyk/repos/dpctl/libsyclinterface/source/dpctl_sycl_device_interface.cpp:540 In [3]: dpctl.SyclDevice().parent_device # no message In [4]: with dd.verbose(): dpctl.SyclDevice().parent_device No parent for device because it is not a subdevice -33 (CL_INVALID_DEVICE) in DPCTLDevice_GetParentDevice at /localdisk/work/opavlyk/repos/dpctl/libsyclinterface/source/dpctl_sycl_device_interface.cpp:540 In [5]: dpctl.SyclDevice().parent_device # no message In [6]: quit ```
1 parent b83692a commit 771041b

File tree

2 files changed

+84
-3
lines changed

2 files changed

+84
-3
lines changed

dpctl/_dev.pyx

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Data Parallel Control (dpctl)
2+
#
3+
# Copyright 2020-2021 Intel Corporation
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# distutils: language = c++
18+
# cython: language_level=3
19+
# cython: linetrace=True
20+
21+
""" Implements developer utilities.
22+
"""
23+
import contextlib
24+
import os
25+
26+
27+
cdef extern from "syclinterface/dpctl_service.h":
28+
cdef void DPCTLService_InitLogger(const char *, const char *)
29+
cdef void DPCTLService_ShutdownLogger()
30+
31+
32+
def init_logger(log_dir=None):
33+
"""Initialize logger to use given directory to save logs.
34+
35+
The call has no effect if `dpctl` was not built to use logger.
36+
"""
37+
cdef bytes p = b""
38+
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)
48+
49+
50+
def fini_logger():
51+
"""Finilize logger.
52+
53+
The call has no effect if `dpctl` was not built to use logger.
54+
"""
55+
DPCTLService_ShutdownLogger()
56+
57+
58+
@contextlib.contextmanager
59+
def verbose(verbosity="warning", log_dir=None):
60+
"""Context manager that activate verbosity"""
61+
_allowed_verbosity = ["warning", "error"]
62+
if not verbosity in _allowed_verbosity:
63+
raise ValueError(
64+
f"Verbosity argument not understood. "
65+
f"Permitted values are {_allowed_verbosity}"
66+
)
67+
init_logger(log_dir=log_dir)
68+
_saved = os.environ.get("DPCTL_VERBOSITY", None)
69+
os.environ["DPCTL_VERBOSITY"] = verbosity
70+
try:
71+
yield
72+
finally:
73+
fini_logger()
74+
if _saved:
75+
os.environ["DPCTL_VERBOSITY"] = _saved
76+
else:
77+
del os.environ["DPCTL_VERBOSITY"]

libsyclinterface/source/dpctl_service.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
#include <algorithm>
3131
#include <cstring>
3232
#include <iostream>
33-
#include <sys/stat.h>
3433
#ifdef ENABLE_GLOG
34+
#include <filesystem>
3535
#include <glog/logging.h>
3636
#endif
3737

@@ -50,8 +50,12 @@ void DPCTLService_InitLogger(const char *app_name, const char *log_dir)
5050
google::InstallFailureSignalHandler();
5151
FLAGS_colorlogtostderr = true;
5252
FLAGS_stderrthreshold = google::FATAL;
53-
struct stat buffer;
54-
if (stat(log_dir, &buffer) != 0) {
53+
54+
namespace fs = std::filesystem;
55+
const fs::path path(log_dir);
56+
std::error_code ec;
57+
58+
if (fs::is_directory(path, ec)) {
5559
FLAGS_log_dir = log_dir;
5660
}
5761
else {

0 commit comments

Comments
 (0)