Skip to content

Commit b88dcdc

Browse files
committed
Minor cleanup of logging issues
+ Removed references in mako files to old logger + Removed some dead code + Fixed recursive log directory creation corner case Signed-off-by: Russell McGuire <russell.w.mcguire@intel.com>
1 parent dfe0964 commit b88dcdc

4 files changed

Lines changed: 47 additions & 38 deletions

File tree

scripts/templates/ze_loader_internal.h.mako

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ from templates import helper as th
3232
#include "zer_ldrddi.h"
3333

3434
#include "loader/ze_loader.h"
35-
#include "../utils/logging.h"
35+
#include "../utils/ze_logger.h"
3636
#include "source/lib/error_state.h"
3737
namespace loader
3838
{
@@ -138,7 +138,7 @@ namespace loader
138138
bool instrumentationEnabled = false;
139139
bool pciOrderingRequested = false;
140140
dditable_t tracing_dditable = {};
141-
std::shared_ptr<Logger> zel_logger;
141+
std::shared_ptr<ZeLogger> zel_logger;
142142
ze_driver_handle_t defaultZerDriverHandle = nullptr;
143143
};
144144

source/loader/ze_loader_api.cpp

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,6 @@ zelLoaderGetContext() {
5959
return loader::context;
6060
}
6161

62-
///////////////////////////////////////////////////////////////////////////////
63-
/// @brief Get the loader's shared logger instance.
64-
///
65-
ZE_DLLEXPORT std::shared_ptr<loader::ZeLogger> *ZE_APICALL
66-
zelLoaderGetLogger() {
67-
if (loader::context == nullptr)
68-
return nullptr;
69-
auto &log = loader::context->zel_logger;
70-
if (!log)
71-
return nullptr;
72-
return &log;
73-
}
74-
7562
///////////////////////////////////////////////////////////////////////////////
7663
/// @brief Internal function for Setting the ZE ddi table for the Tracing Layer.
7764
///

source/loader/ze_loader_api.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,6 @@ zeLoaderGetTracingHandle();
6565
ZE_DLLEXPORT loader::context_t *ZE_APICALL
6666
zelLoaderGetContext();
6767

68-
///////////////////////////////////////////////////////////////////////////////
69-
/// @brief Get the loader's shared logger instance.
70-
/// Returns nullptr if the loader context is not yet initialized.
71-
/// Callers (e.g. the validation layer) should adopt this logger
72-
/// instead of creating their own to share a single file handle and mutex.
73-
///
74-
/// @returns
75-
/// - ::Pointer to the shared ZeLogger, or nullptr
76-
ZE_DLLEXPORT std::shared_ptr<loader::ZeLogger> *ZE_APICALL
77-
zelLoaderGetLogger();
78-
79-
8068
///////////////////////////////////////////////////////////////////////////////
8169
/// @brief Exported function for getting version
8270
///

source/utils/ze_logger.cpp

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
#include "ze_logger.h"
1010
#include "ze_util.h"
1111

12+
#include <cerrno>
1213
#include <chrono>
14+
#include <cstdio>
1315
#include <ctime>
1416
#include <iomanip>
1517
#include <iostream>
@@ -158,9 +160,10 @@ LogLevel logLevelFromString(const std::string &s) {
158160
if (s == "trace") return LogLevel::trace;
159161
if (s == "debug") return LogLevel::debug;
160162
if (s == "info") return LogLevel::info;
161-
if (s == "warn") return LogLevel::warn;
162-
if (s == "error") return LogLevel::err;
163-
if (s == "critical") return LogLevel::critical;
163+
if (s == "warn" || s == "warning") return LogLevel::warn;
164+
if (s == "err" || s == "error") return LogLevel::err;
165+
if (s == "crit" || s == "critical") return LogLevel::critical;
166+
if (s == "off") return LogLevel::off;
164167
return LogLevel::warn; // default
165168
}
166169

@@ -205,7 +208,7 @@ void ZeLogger::flush() {
205208
// Default pattern tokens:
206209
// %Y-%m-%d %H:%M:%S.%e — timestamp with milliseconds
207210
// %t — thread id (cached thread_local, STB_LOCAL — safe for dlclose)
208-
// %P — process id (cached at construction)
211+
// %P — process id
209212
// %^%l%$ — level label (with color when tty)
210213
// %v — message
211214
//
@@ -516,16 +519,47 @@ std::shared_ptr<ZeLogger> createLogger(const std::string &caller) {
516519
logger = std::make_shared<ZeLogger>(/*use_stderr=*/true, level, log_pattern);
517520
output_dest = "stderr (console)";
518521
} else {
519-
// Create the log directory only if it does not already exist.
522+
// Create the full directory path (equivalent to mkdir -p).
520523
#ifdef _WIN32
521-
DWORD attrs = GetFileAttributesA(log_directory.c_str());
522-
if (attrs == INVALID_FILE_ATTRIBUTES || !(attrs & FILE_ATTRIBUTE_DIRECTORY)) {
523-
_mkdir(log_directory.c_str());
524+
// Walk each component and create it if missing.
525+
for (std::size_t pos = 0; pos <= log_directory.size(); ++pos) {
526+
if (pos == log_directory.size() ||
527+
log_directory[pos] == '\\' || log_directory[pos] == '/') {
528+
if (pos == 0) continue;
529+
std::string partial = log_directory.substr(0, pos);
530+
DWORD attrs = GetFileAttributesA(partial.c_str());
531+
if (attrs == INVALID_FILE_ATTRIBUTES) {
532+
if (_mkdir(partial.c_str()) != 0 && errno != EEXIST) {
533+
std::cerr << "ze_logger: Failed to create log directory '"
534+
<< partial << "': " << strerror(errno) << "\n";
535+
return std::make_shared<ZeLogger>();
536+
}
537+
} else if (!(attrs & FILE_ATTRIBUTE_DIRECTORY)) {
538+
std::cerr << "ze_logger: Log directory path component '"
539+
<< partial << "' exists but is not a directory\n";
540+
return std::make_shared<ZeLogger>();
541+
}
542+
}
524543
}
525544
#else
526-
struct stat st{};
527-
if (stat(log_directory.c_str(), &st) != 0 || !S_ISDIR(st.st_mode)) {
528-
mkdir(log_directory.c_str(), 0755);
545+
// Walk each component and create it if missing.
546+
for (std::size_t pos = 0; pos <= log_directory.size(); ++pos) {
547+
if (pos == log_directory.size() || log_directory[pos] == '/') {
548+
if (pos == 0) continue;
549+
std::string partial = log_directory.substr(0, pos);
550+
struct stat st{};
551+
if (stat(partial.c_str(), &st) != 0) {
552+
if (mkdir(partial.c_str(), 0755) != 0 && errno != EEXIST) {
553+
std::cerr << "ze_logger: Failed to create log directory '"
554+
<< partial << "': " << strerror(errno) << "\n";
555+
return std::make_shared<ZeLogger>();
556+
}
557+
} else if (!S_ISDIR(st.st_mode)) {
558+
std::cerr << "ze_logger: Log directory path component '"
559+
<< partial << "' exists but is not a directory\n";
560+
return std::make_shared<ZeLogger>();
561+
}
562+
}
529563
}
530564
#endif
531565
logger = std::make_shared<ZeLogger>(full_log_file_path, level, log_pattern);

0 commit comments

Comments
 (0)