cubecl_runtime/config/
logger.rs1use super::{CubeClRuntimeConfig, RuntimeConfig};
2use crate::config::{
3 autotune::AutotuneLogLevel, compilation::CompilationLogLevel, memory::MemoryLogLevel,
4 profiling::ProfilingLogLevel, streaming::StreamingLogLevel,
5};
6use alloc::{sync::Arc, vec::Vec};
7use core::fmt::Display;
8
9use cubecl_common::config::logger::LoggerSinks;
10pub(crate) use cubecl_common::config::logger::{LogLevel, LoggerConfig};
11
12#[derive(Debug)]
14pub struct Logger {
15 sinks: LoggerSinks,
16 compilation_index: Vec<usize>,
17 profiling_index: Vec<usize>,
18 autotune_index: Vec<usize>,
19 streaming_index: Vec<usize>,
20 memory_index: Vec<usize>,
21 pub config: Arc<CubeClRuntimeConfig>,
23}
24
25impl Default for Logger {
26 fn default() -> Self {
27 Self::new()
28 }
29}
30
31impl Logger {
32 pub fn new() -> Self {
36 let config = CubeClRuntimeConfig::get();
37 let mut sinks = LoggerSinks::new();
38
39 let compilation_index = register_enabled(
40 &mut sinks,
41 &config.compilation.logger,
42 !matches!(
43 config.compilation.logger.level,
44 CompilationLogLevel::Disabled
45 ),
46 );
47 let profiling_index = register_enabled(
48 &mut sinks,
49 &config.profiling.logger,
50 !matches!(config.profiling.logger.level, ProfilingLogLevel::Disabled),
51 );
52 let autotune_index = register_enabled(
53 &mut sinks,
54 &config.autotune.logger,
55 !matches!(config.autotune.logger.level, AutotuneLogLevel::Disabled),
56 );
57 let streaming_index = register_enabled(
58 &mut sinks,
59 &config.streaming.logger,
60 !matches!(config.streaming.logger.level, StreamingLogLevel::Disabled),
61 );
62 let memory_index = register_enabled(
63 &mut sinks,
64 &config.memory.logger,
65 !matches!(config.memory.logger.level, MemoryLogLevel::Disabled),
66 );
67
68 Self {
69 sinks,
70 compilation_index,
71 profiling_index,
72 autotune_index,
73 streaming_index,
74 memory_index,
75 config,
76 }
77 }
78
79 pub fn log_streaming<S: Display>(&mut self, msg: &S) {
81 self.sinks.log(&self.streaming_index, msg);
82 }
83
84 pub fn log_memory<S: Display>(&mut self, msg: &S) {
86 self.sinks.log(&self.memory_index, msg);
87 }
88
89 pub fn log_compilation<S: Display>(&mut self, msg: &S) {
91 self.sinks.log(&self.compilation_index, msg);
92 }
93
94 pub fn log_profiling<S: Display>(&mut self, msg: &S) {
96 self.sinks.log(&self.profiling_index, msg);
97 }
98
99 pub fn log_autotune<S: Display>(&mut self, msg: &S) {
101 self.sinks.log(&self.autotune_index, msg);
102 }
103
104 pub fn log_level_streaming(&self) -> StreamingLogLevel {
106 self.config.streaming.logger.level
107 }
108
109 pub fn log_level_autotune(&self) -> AutotuneLogLevel {
111 self.config.autotune.logger.level
112 }
113
114 pub fn log_level_compilation(&self) -> CompilationLogLevel {
116 self.config.compilation.logger.level
117 }
118
119 pub fn log_level_profiling(&self) -> ProfilingLogLevel {
121 self.config.profiling.logger.level
122 }
123}
124
125fn register_enabled<L: LogLevel>(
126 sinks: &mut LoggerSinks,
127 config: &LoggerConfig<L>,
128 enabled: bool,
129) -> Vec<usize> {
130 if enabled {
131 sinks.register(config)
132 } else {
133 Vec::new()
134 }
135}