9
9
#include " core/common/exceptions.h"
10
10
#include " core/common/logging/isink.h"
11
11
#include " core/common/logging/logging.h"
12
+ #include " core/common/logging/sinks/composite_sink.h"
12
13
13
14
#ifdef _WIN32
14
15
#include < Windows.h>
15
16
#include " core/platform/windows/logging/etw_sink.h"
16
- #include " core/common/logging/sinks/composite_sink.h"
17
17
#else
18
18
#include < unistd.h>
19
19
#if defined(__MACH__) || defined(__wasm__) || defined(_AIX)
22
22
#include < sys/syscall.h>
23
23
#endif
24
24
#endif
25
- #include " core/platform/ort_mutex.h"
26
25
27
26
#if __FreeBSD__
28
27
#include < sys/thr.h> // Use thr_self() syscall under FreeBSD to get thread id
28
+ #include " logging.h"
29
29
#endif
30
30
31
31
namespace onnxruntime {
@@ -52,6 +52,10 @@ static std::atomic<void*>& DefaultLoggerManagerInstance() noexcept {
52
52
return default_instance;
53
53
}
54
54
55
+ LoggingManager* LoggingManager::GetDefaultInstance () {
56
+ return static_cast <LoggingManager*>(DefaultLoggerManagerInstance ().load ());
57
+ }
58
+
55
59
// GSL_SUPRESS(i.22) is broken. Ignore the warnings for the static local variables that are trivial
56
60
// and should not have any destruction order issues via pragmas instead.
57
61
// https://developercommunity.visualstudio.com/content/problem/249706/gslsuppress-does-not-work-for-i22-c-core-guideline.html
@@ -66,6 +70,7 @@ static OrtMutex& DefaultLoggerMutex() noexcept {
66
70
}
67
71
68
72
Logger* LoggingManager::s_default_logger_ = nullptr ;
73
+ OrtMutex sink_mutex_;
69
74
70
75
#ifdef _MSC_VER
71
76
#pragma warning(pop)
@@ -245,35 +250,78 @@ unsigned int GetProcessId() {
245
250
#endif
246
251
}
247
252
248
- std::unique_ptr<ISink> EnhanceLoggerWithEtw (std::unique_ptr<ISink> existingLogger , logging::Severity originalSeverity ,
249
- logging::Severity etwSeverity ) {
253
+ std::unique_ptr<ISink> EnhanceSinkWithEtw (std::unique_ptr<ISink> existing_sink , logging::Severity original_severity ,
254
+ logging::Severity etw_severity ) {
250
255
#ifdef _WIN32
251
256
auto & manager = EtwRegistrationManager::Instance ();
252
257
if (manager.IsEnabled ()) {
253
258
auto compositeSink = std::make_unique<CompositeSink>();
254
- compositeSink->AddSink (std::move (existingLogger ), originalSeverity );
255
- compositeSink->AddSink (std::make_unique<EtwSink>(), etwSeverity );
259
+ compositeSink->AddSink (std::move (existing_sink ), original_severity );
260
+ compositeSink->AddSink (std::make_unique<EtwSink>(), etw_severity );
256
261
return compositeSink;
257
262
} else {
258
- return existingLogger ;
263
+ return existing_sink ;
259
264
}
260
265
#else
261
266
// On non-Windows platforms, just return the existing logger
262
- (void )originalSeverity ;
263
- (void )etwSeverity ;
264
- return existingLogger ;
267
+ (void )original_severity ;
268
+ (void )etw_severity ;
269
+ return existing_sink ;
265
270
#endif // _WIN32
266
271
}
267
272
268
- Severity OverrideLevelWithEtw (Severity originalSeverity ) {
273
+ Severity OverrideLevelWithEtw (Severity original_severity ) {
269
274
#ifdef _WIN32
270
275
auto & manager = logging::EtwRegistrationManager::Instance ();
271
276
if (manager.IsEnabled () &&
272
277
(manager.Keyword () & static_cast <ULONGLONG>(onnxruntime::logging::ORTTraceLoggingKeyword::Logs)) != 0 ) {
273
278
return manager.MapLevelToSeverity ();
274
279
}
275
280
#endif // _WIN32
276
- return originalSeverity;
281
+ return original_severity;
282
+ }
283
+
284
+ bool LoggingManager::AddSinkOfType (SinkType sink_type, std::function<std::unique_ptr<ISink>()> sinkFactory,
285
+ logging::Severity severity) {
286
+ std::lock_guard<OrtMutex> guard (sink_mutex_);
287
+ if (sink_->GetType () != SinkType::CompositeSink) {
288
+ // Current sink is not a composite, create a new composite sink and add the current sink to it
289
+ auto new_composite = std::make_unique<CompositeSink>();
290
+ new_composite->AddSink (std::move (sink_), default_min_severity_); // Move the current sink into the new composite
291
+ sink_ = std::move (new_composite); // Now sink_ is pointing to the new composite
292
+ }
293
+ // Adjust the default minimum severity level to accommodate new sink needs
294
+ default_min_severity_ = std::min (default_min_severity_, severity);
295
+ if (s_default_logger_ != nullptr ) {
296
+ s_default_logger_->SetSeverity (default_min_severity_);
297
+ }
298
+ CompositeSink* current_composite = static_cast <CompositeSink*>(sink_.get ());
299
+ if (current_composite->HasType (sink_type)) {
300
+ return false ; // Sink of this type already exists, do not add another
301
+ }
302
+
303
+ current_composite->AddSink (sinkFactory (), severity);
304
+ return true ;
305
+ }
306
+
307
+ void LoggingManager::RemoveSink (SinkType sink_type) {
308
+ std::lock_guard<OrtMutex> guard (sink_mutex_);
309
+
310
+ if (sink_->GetType () == SinkType::CompositeSink) {
311
+ auto composite_sink = static_cast <CompositeSink*>(sink_.get ());
312
+
313
+ Severity newSeverity = composite_sink->RemoveSink (sink_type);
314
+
315
+ if (composite_sink->HasOnlyOneSink ()) {
316
+ // If only one sink remains, replace the CompositeSink with this single sink
317
+ sink_ = composite_sink->GetRemoveSingleSink ();
318
+ }
319
+
320
+ default_min_severity_ = newSeverity;
321
+ if (s_default_logger_ != nullptr ) {
322
+ s_default_logger_->SetSeverity (default_min_severity_);
323
+ }
324
+ }
277
325
}
278
326
279
327
} // namespace logging
0 commit comments