Skip to content

Commit 054255a

Browse files
authored
New logger and remove code bloat (#443)
* Remove spdlog files from repo * Added logging enablement modes to support =2 for enhanced output ZE_ENABLE_LOADER_DEBUG_TRACE=1 retains behavior ZE_ENABLE_LOADER_DEBUG_TRACE=2 enables enhanced output The same for ZEL_ENABLE_LOADER_LOGGING=1 and =2 Signed-off-by: Russell McGuire <russell.w.mcguire@intel.com>
1 parent f7cffe0 commit 054255a

65 files changed

Lines changed: 941 additions & 15627 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CMakeLists.txt

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,6 @@ else()
3838
endif()
3939
add_definitions(-DLOADER_VERSION_SHA="${VERSION_SHA}")
4040

41-
if(SYSTEM_SPDLOG)
42-
find_package(spdlog CONFIG)
43-
if(spdlog_FOUND)
44-
message(STATUS "System spdlog found.")
45-
else()
46-
message(FATAL_ERROR "SYSTEM_SPDLOG specified but spdlog wasn't found.")
47-
endif()
48-
else()
49-
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/third_party/spdlog_headers")
50-
endif()
51-
5241
include(FetchContent)
5342

5443
if(BUILD_L0_LOADER_TESTS)

README.md

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -46,39 +46,62 @@ When the `-DBUILD_STATIC=1` is executed, the dynamic loader and layers are not b
4646
Testing with the static loader requires a build of the dynamic loader or an installation of the dynamic loader to exist in the library path.
4747

4848
# Debug Trace
49+
50+
> **Deprecated:** `ZE_ENABLE_LOADER_DEBUG_TRACE` is deprecated and will be removed in a future release.
51+
> Use `ZEL_LOADER_LOG_CONSOLE=1` with `ZEL_LOADER_LOGGING_LEVEL=trace` instead, which provides
52+
> the same or enhanced information with structured timestamps, thread IDs, and log levels via the logging system.
53+
4954
The Level Zero Loader has the ability to print warnings and errors which occur within the internals of the Level Zero Loader itself.
5055

5156
To enable this debug tracing feature, set the environment variable `ZE_ENABLE_LOADER_DEBUG_TRACE=1`.
5257

5358
This will enforce the Loader to print all errors whether fatal or non-fatal to stderr with the PREFIX `ZE_LOADER_DEBUG_TRACE:`.
5459

5560

56-
# Logging to File - PREVIEW
57-
The Level Zero Loader uses spdlog logging and can be controlled via environment variables:
61+
# Logging to File or Console
62+
The Level Zero Loader provides built-in logging controlled via environment variables:
5863

59-
`ZEL_ENABLE_LOADER_LOGGING=1`
64+
| Environment Variable | Default | Description |
65+
|---|---|---|
66+
| `ZEL_ENABLE_LOADER_LOGGING` | `0` | Set to `1` to enable file logging |
67+
| `ZEL_LOADER_LOG_CONSOLE` | `0` | Set to `1` to enable console (stderr) logging, overrides file logging |
68+
| `ZEL_LOADER_LOGGING_LEVEL` | `warn` | Log level: `trace`, `debug`, `info`, `warn`, `error`, `critical`, `off` |
69+
| `ZEL_LOADER_LOG_DIR` | `~/.oneapi_logs` | Directory to write the log file into |
70+
| `ZEL_LOADER_LOG_FILE` | `ze_loader.log` | Log filename |
71+
| `ZEL_LOADER_LOG_PATTERN` | see below | Custom log format pattern |
72+
73+
## Output destination
6074

61-
`ZEL_LOADER_LOG_DIR='/directory/path'`
75+
The two flags control output as follows:
6276

63-
`ZEL_LOADER_LOGGING_LEVEL=debug`
77+
| `ZEL_ENABLE_LOADER_LOGGING` | `ZEL_LOADER_LOG_CONSOLE` | Output |
78+
|---|---|---|
79+
| `0` (default) | `0` (default) | Logging disabled — no file or console output |
80+
| `0` | `1` | Console output to **stderr** at the configured level |
81+
| `1` | `0` | File output to `ZEL_LOADER_LOG_DIR/ZEL_LOADER_LOG_FILE` |
82+
| `1` | `1` | Console output to **stderr** — file path is ignored |
6483

65-
Default Log Pattern (Does not need to be set, please see below):
66-
`ZEL_LOADER_LOG_PATTERN='[%Y-%m-%d %H:%M:%S.%e] [thread-id: %t] [%^%l%$] %v'`
84+
> **Note:** When both `ZEL_ENABLE_LOADER_LOGGING=1` and `ZEL_LOADER_LOG_CONSOLE=1` are set,
85+
> output goes to the console only. The file path configuration is not used. If persistent file
86+
> capture is required, set `ZEL_LOADER_LOG_CONSOLE=0`.
6787
68-
Valid logging levels are trace, debug, info, warn, error, critical, off.
69-
Logging is disabled by default but when enabled the default level is 'warn'.
70-
The default log file is 'ze_loader.log' in '.oneapi_logs' in the current
71-
user's home directory.
88+
The log directory (`ZEL_LOADER_LOG_DIR`) is created automatically on first use if it does not exist.
7289

73-
The default log pattern includes timestamps, thread IDs, log levels, and messages.
74-
You can customize the pattern using `ZEL_LOADER_LOG_PATTERN`. Common pattern flags:
75-
- `%t` - thread id
76-
- `%Y-%m-%d %H:%M:%S.%e` - timestamp with milliseconds
77-
- `%l` - log level
78-
- `%v` - the actual log message
79-
See spdlog documentation for more pattern options.
90+
## Log pattern
91+
92+
Default pattern (used when `ZEL_LOADER_LOG_PATTERN` is not set):
93+
```
94+
[%Y-%m-%d %H:%M:%S.%e] [thread-id: %t] [%^%l%$] %v
95+
```
8096

81-
This feature is in early development and is preview only.
97+
Supported pattern tokens:
98+
- `%Y-%m-%d %H:%M:%S.%e` — timestamp with milliseconds (must appear as this exact sequence)
99+
- `%t` — thread id
100+
- `%P` — process id
101+
- `%l` — log level label
102+
- `%^` — begin color range (no-op when output is not a TTY)
103+
- `%$` — end color range
104+
- `%v` — log message
82105

83106
# Logging API calls
84107
The Level Zero Loader will log all API calls whenever logging level is set to `trace` and
@@ -95,8 +118,10 @@ To print successful API call results, set
95118
Otherwise, only error results will be printed in the API trace output.
96119
NOTE: This will become the default behavior in future releases. for now, please set the env var to enable this logging feature.
97120

98-
By default logs will be written to the log file, as described above. To print the logs
99-
to stderr instead, `ZEL_LOADER_LOG_CONSOLE=1` needs to be set.
121+
By default logs will be written to the log file as described above. To print logs
122+
to stderr instead of a file, set `ZEL_LOADER_LOG_CONSOLE=1`. Note that when
123+
`ZEL_LOADER_LOG_CONSOLE=1`, the file path configuration is ignored — output goes
124+
to the console only.
100125

101126
The API logging output format includes both function entry and exit information, showing parameter names on entry and parameter values with the result code on exit. Each log entry is timestamped and includes the thread-id, logger name, log level. Example output:
102127

scripts/templates/ze_loader_internal.h.mako

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +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"
36-
#include "spdlog/spdlog.h"
35+
#include "../utils/ze_logger.h"
3736
#include "source/lib/error_state.h"
3837
namespace loader
3938
{
@@ -139,7 +138,7 @@ namespace loader
139138
bool instrumentationEnabled = false;
140139
bool pciOrderingRequested = false;
141140
dditable_t tracing_dditable = {};
142-
std::shared_ptr<Logger> zel_logger;
141+
std::shared_ptr<ZeLogger> zel_logger;
143142
ze_driver_handle_t defaultZerDriverHandle = nullptr;
144143
};
145144

source/inc/ze_util.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,32 @@ inline bool getenv_tobool( const char* name )
148148
return ( 0 == strcmp( "1", env ) );
149149
}
150150

151+
// Returns the numeric mode (0, 1, or 2) for env vars that support a two-level
152+
// flag scheme: 0 = off, 1 = legacy/normal, 2 = advanced/verbose.
153+
// Any value other than "1" or "2" is treated as 0.
154+
inline uint32_t getenv_tomode( const char* name )
155+
{
156+
const char* env = nullptr;
157+
158+
#if defined(_WIN32)
159+
char buffer[8];
160+
auto rc = GetEnvironmentVariable(name, buffer, 8);
161+
if (0 != rc && rc <= 8) {
162+
env = buffer;
163+
}
164+
#else
165+
env = getenv(name);
166+
#endif
167+
168+
if (env == nullptr || strcmp("0", env) == 0)
169+
return 0;
170+
if (strcmp("2", env) == 0)
171+
return 2;
172+
if (strcmp("1", env) == 0)
173+
return 1;
174+
return 0;
175+
}
176+
151177

152178
inline std::string getenv_string ( const char* name){
153179

source/layers/validation/ze_validation_layer.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ namespace validation_layer
2525
enableThreadingValidation = getenv_tobool( "ZE_ENABLE_THREADING_VALIDATION" );
2626
verboseLogging = getenv_tobool( "ZEL_LOADER_LOGGING_ENABLE_SUCCESS_PRINT" );
2727

28-
logger = loader::createLogger();
28+
// Point at the process-lifetime no-op logger until the loader calls
29+
// zelLoaderSetLogger(). This is never null, so call sites need no null check.
30+
// Thread-safety: zelLoaderSetLogger() writes this field exactly once on the
31+
// init thread before zeDdiTable.exchange() makes the layer reachable.
32+
logger = loader::noopLogger();
2933
}
3034

3135
///////////////////////////////////////////////////////////////////////////////
@@ -54,6 +58,17 @@ zelLoaderGetVersion(zel_component_version_t *version)
5458
return ZE_RESULT_SUCCESS;
5559
}
5660

61+
/// @brief Called by the loader immediately after dlopen to share its logger.
62+
/// Replaces the no-op default so that validation-layer messages flow
63+
/// through the same sink as the loader.
64+
ZE_DLLEXPORT void ZE_APICALL
65+
zelLoaderSetLogger(loader::ZeLogger *loaderLogger)
66+
{
67+
if (loaderLogger) {
68+
validation_layer::context_t::getInstance().logger = loaderLogger;
69+
}
70+
}
71+
5772
#if defined(__cplusplus)
5873
};
5974
#endif

source/layers/validation/ze_validation_layer.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include "zet_entry_points.h"
2020
#include "zes_entry_points.h"
2121
#include "zer_entry_points.h"
22-
#include "logging.h"
22+
#include "ze_logger.h"
2323
#include "ze_to_string.h"
2424
#include "zes_to_string.h"
2525
#include "zet_to_string.h"
@@ -57,7 +57,12 @@ namespace validation_layer
5757
std::vector<validationChecker *> validationHandlers;
5858
std::unique_ptr<HandleLifetimeValidation> handleLifetime;
5959

60-
std::shared_ptr<loader::Logger> logger;
60+
// Raw pointer — the loader owns the ZeLogger and guarantees it outlives
61+
// the validation layer during normal operation (dlclose happens before
62+
// zel_logger is destroyed in context_t::~context_t()). Using a raw pointer
63+
// (rather than shared_ptr) avoids _Sp_counted_base::_M_release() being
64+
// called during _dl_call_fini after the control block has been freed.
65+
loader::ZeLogger *logger;
6166

6267
static context_t& getInstance() {
6368
static context_t instance;

source/lib/ze_lib.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ namespace ze_lib
6363
///////////////////////////////////////////////////////////////////////////////
6464
__zedlllocal context_t::context_t()
6565
{
66-
debugTraceEnabled = getenv_tobool( "ZE_ENABLE_LOADER_DEBUG_TRACE" );
66+
{
67+
uint32_t dtMode = getenv_tomode( "ZE_ENABLE_LOADER_DEBUG_TRACE" );
68+
debugTraceEnabled = (dtMode != 0);
69+
}
70+
6771
memset(&initialzeDdiTable, 0, sizeof(ze_dditable_t));
6872
memset(&initialzetDdiTable, 0, sizeof(zet_dditable_t));
6973
memset(&initialzesDdiTable, 0, sizeof(zes_dditable_t));
@@ -107,8 +111,9 @@ namespace ze_lib
107111
loaderLibraryPath = readLevelZeroLoaderLibraryPath();
108112
}
109113
#endif
110-
if (debugTraceEnabled)
114+
if (debugTraceEnabled) {
111115
debug_trace_message("Static Loader Using Loader Library Path: ", loaderLibraryPath);
116+
}
112117
std::string loaderFullLibraryPath = create_library_path(MAKE_LIBRARY_NAME( "ze_loader", L0_LOADER_VERSION), loaderLibraryPath.c_str());
113118
loader = LOAD_DRIVER_LIBRARY(loaderFullLibraryPath.c_str());
114119

source/lib/ze_lib.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818
#include "zer_ddi.h"
1919
#include "layers/zel_tracing_api.h"
2020
#include "layers/zel_tracing_ddi.h"
21-
#include "../utils/logging.h"
21+
#include "../utils/ze_logger.h"
2222
#include "loader/ze_loader.h"
2323
#include "ze_util.h"
2424
#include <vector>
25+
#include <map>
2526
#include <mutex>
2627
#include <atomic>
2728
#include <typeinfo>

0 commit comments

Comments
 (0)