Skip to main content

cubecl_runtime/config/
logger.rs

1use 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/// Central logging utility for `CubeCL`, managing multiple log outputs.
13#[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    /// Global configuration for logging settings.
22    pub config: Arc<CubeClRuntimeConfig>,
23}
24
25impl Default for Logger {
26    fn default() -> Self {
27        Self::new()
28    }
29}
30
31impl Logger {
32    /// Creates a new `Logger` instance based on the global configuration.
33    ///
34    /// Note that creating a logger is quite expensive.
35    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    /// Logs a message for streaming, directing it to all configured streaming loggers.
80    pub fn log_streaming<S: Display>(&mut self, msg: &S) {
81        self.sinks.log(&self.streaming_index, msg);
82    }
83
84    /// Logs a message for memory, directing it to all configured memory loggers.
85    pub fn log_memory<S: Display>(&mut self, msg: &S) {
86        self.sinks.log(&self.memory_index, msg);
87    }
88
89    /// Logs a message for compilation, directing it to all configured compilation loggers.
90    pub fn log_compilation<S: Display>(&mut self, msg: &S) {
91        self.sinks.log(&self.compilation_index, msg);
92    }
93
94    /// Logs a message for profiling, directing it to all configured profiling loggers.
95    pub fn log_profiling<S: Display>(&mut self, msg: &S) {
96        self.sinks.log(&self.profiling_index, msg);
97    }
98
99    /// Logs a message for autotuning, directing it to all configured autotuning loggers.
100    pub fn log_autotune<S: Display>(&mut self, msg: &S) {
101        self.sinks.log(&self.autotune_index, msg);
102    }
103
104    /// Returns the current streaming log level from the global configuration.
105    pub fn log_level_streaming(&self) -> StreamingLogLevel {
106        self.config.streaming.logger.level
107    }
108
109    /// Returns the current autotune log level from the global configuration.
110    pub fn log_level_autotune(&self) -> AutotuneLogLevel {
111        self.config.autotune.logger.level
112    }
113
114    /// Returns the current compilation log level from the global configuration.
115    pub fn log_level_compilation(&self) -> CompilationLogLevel {
116        self.config.compilation.logger.level
117    }
118
119    /// Returns the current profiling log level from the global configuration.
120    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}