Skip to content

Commit 9bc62ab

Browse files
Implement InitLogger and ShutdownLogger funcs for glog and change error_handlers
1 parent 05a5abd commit 9bc62ab

File tree

3 files changed

+81
-7
lines changed

3 files changed

+81
-7
lines changed

libsyclinterface/helper/source/dpctl_error_handlers.cpp

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@
2424
//===----------------------------------------------------------------------===//
2525

2626
#include "dpctl_error_handlers.h"
27+
#include "dpctl_service.h"
2728
#include <cstring>
29+
#include <sstream>
30+
#ifdef ENABLE_GLOG
31+
#include <glog/logging.h>
32+
#endif
2833

2934
void DPCTL_AsyncErrorHandler::operator()(
3035
const cl::sycl::exception_list &exceptions)
@@ -59,6 +64,25 @@ int requested_verbosity_level(void)
5964

6065
return requested_level;
6166
}
67+
68+
void output_message(std::string ss_str, [[maybe_unused]] error_level error_type)
69+
{
70+
#ifdef ENABLE_GLOG
71+
switch (error_type) {
72+
case error_level::error:
73+
LOG(ERROR) << ss_str;
74+
break;
75+
case error_level::warning:
76+
LOG(WARNING) << ss_str;
77+
break;
78+
default:
79+
LOG(FATAL) << ss_str;
80+
}
81+
#else
82+
std::cerr << ss_str;
83+
#endif
84+
}
85+
6286
} // namespace
6387

6488
void error_handler(const std::exception &e,
@@ -70,9 +94,14 @@ void error_handler(const std::exception &e,
7094
int requested_level = requested_verbosity_level();
7195
int error_level = static_cast<int>(error_type);
7296

73-
if (requested_level >= error_level) {
74-
std::cerr << e.what() << " in " << func_name << " at " << file_name
75-
<< ":" << line_num << std::endl;
97+
bool to_output = requested_level >= error_level;
98+
99+
if (to_output) {
100+
std::stringstream ss;
101+
ss << e.what() << " in " << func_name << " at " << file_name << ":"
102+
<< line_num << std::endl;
103+
104+
output_message(ss.str(), error_type);
76105
}
77106
}
78107

@@ -85,8 +114,13 @@ void error_handler(const std::string &what,
85114
int requested_level = requested_verbosity_level();
86115
int error_level = static_cast<int>(error_type);
87116

88-
if (requested_level >= error_level) {
89-
std::cerr << what << " In " << func_name << " at " << file_name << ":"
90-
<< line_num << std::endl;
117+
bool to_output = requested_level >= error_level;
118+
119+
if (to_output) {
120+
std::stringstream ss;
121+
ss << what << " in " << func_name << " at " << file_name << ":"
122+
<< line_num << std::endl;
123+
124+
output_message(ss.str(), error_type);
91125
}
92126
}

libsyclinterface/include/dpctl_service.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,7 @@ DPCTL_API
4444
__dpctl_give const char *DPCTLService_GetDPCPPVersion(void);
4545

4646
DPCTL_C_EXTERN_C_END
47+
48+
void DPCTLService_InitLogger(const char *app_name, const char *log_dir);
49+
50+
void DPCTLService_ShutdownLogger(void);

libsyclinterface/source/dpctl_service.cpp

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,49 @@
2626
#include "dpctl_service.h"
2727
#include "Config/dpctl_config.h"
2828

29-
#include "../helper/include/dpctl_string_utils.hpp"
29+
#include "dpctl_string_utils.hpp"
3030
#include <algorithm>
3131
#include <cstring>
3232
#include <iostream>
33+
#include <sys/stat.h>
34+
#ifdef ENABLE_GLOG
35+
#include <glog/logging.h>
36+
#endif
3337

3438
__dpctl_give const char *DPCTLService_GetDPCPPVersion(void)
3539
{
3640
std::string version = DPCTL_DPCPP_VERSION;
3741
return dpctl::helper::cstring_from_string(version);
3842
}
43+
44+
#ifdef ENABLE_GLOG
45+
46+
void DPCTLService_InitLogger(const char *app_name, const char *log_dir)
47+
{
48+
google::InitGoogleLogging(app_name);
49+
google::EnableLogCleaner(0);
50+
google::InstallFailureSignalHandler();
51+
FLAGS_colorlogtostderr = true;
52+
FLAGS_stderrthreshold = google::FATAL;
53+
struct stat buffer;
54+
if (stat(log_dir, &buffer) != 0) {
55+
FLAGS_log_dir = log_dir;
56+
}
57+
else {
58+
std::cerr << "Directory not found. Log files will be created in the "
59+
"default logging directory.\n";
60+
}
61+
}
62+
63+
void DPCTLService_ShutdownLogger(void)
64+
{
65+
google::ShutdownGoogleLogging();
66+
}
67+
68+
#else
69+
void DPCTLService_InitLogger([[maybe_unused]] const char *app_name,
70+
[[maybe_unused]] const char *log_dir){};
71+
72+
void DPCTLService_ShutdownLogger(void){};
73+
74+
#endif

0 commit comments

Comments
 (0)