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
82            .log(&self.streaming_index, "cubecl::streaming", msg);
83    }
84
85    /// Logs a message for memory, directing it to all configured memory loggers.
86    pub fn log_memory<S: Display>(&mut self, msg: &S) {
87        self.sinks.log(&self.memory_index, "cubecl::memory", msg);
88    }
89
90    /// Logs a message for compilation, directing it to all configured compilation loggers.
91    pub fn log_compilation<S: Display>(&mut self, msg: &S) {
92        self.sinks
93            .log(&self.compilation_index, "cubecl::compilation", msg);
94    }
95
96    /// Logs a message for profiling, directing it to all configured profiling loggers.
97    pub fn log_profiling<S: Display>(&mut self, msg: &S) {
98        self.sinks
99            .log(&self.profiling_index, "cubecl::profiling", msg);
100    }
101
102    /// Logs a message for autotuning, directing it to all configured autotuning loggers.
103    pub fn log_autotune<S: Display>(&mut self, msg: &S) {
104        self.sinks
105            .log(&self.autotune_index, "cubecl::autotune", msg);
106    }
107
108    /// Returns the current streaming log level from the global configuration.
109    pub fn log_level_streaming(&self) -> StreamingLogLevel {
110        self.config.streaming.logger.level
111    }
112
113    /// Returns the current autotune log level from the global configuration.
114    pub fn log_level_autotune(&self) -> AutotuneLogLevel {
115        self.config.autotune.logger.level
116    }
117
118    /// Returns the current compilation log level from the global configuration.
119    pub fn log_level_compilation(&self) -> CompilationLogLevel {
120        self.config.compilation.logger.level
121    }
122
123    /// Returns the current profiling log level from the global configuration.
124    pub fn log_level_profiling(&self) -> ProfilingLogLevel {
125        self.config.profiling.logger.level
126    }
127}
128
129fn register_enabled<L: LogLevel>(
130    sinks: &mut LoggerSinks,
131    config: &LoggerConfig<L>,
132    enabled: bool,
133) -> Vec<usize> {
134    if enabled {
135        sinks.register(config)
136    } else {
137        Vec::new()
138    }
139}