ekg_util/log/
mod.rs

1#![allow(missing_docs)]
2
3use ekg_error::Error;
4#[cfg(all(not(target_family = "wasm")))]
5use owo_colors::OwoColorize;
6
7pub const LOG_TARGET_TRANSFORM: &str = "transform";
8pub const LOG_TARGET_DATABASE: &str = "database";
9pub const LOG_TARGET_PROJECT: &str = "project";
10pub const LOG_TARGET_NUMBERS: &str = "numbers";
11pub const LOG_TARGET_COMMAND: &str = "command";
12pub const LOG_TARGET_EXPORT: &str = "export";
13pub const LOG_TARGET_CONFIG: &str = "config";
14pub const LOG_TARGET_SPARQL: &str = "sparql";
15pub const LOG_TARGET_SERVER: &str = "server";
16pub const LOG_TARGET_FETCH: &str = "fetch";
17pub const LOG_TARGET_FILES: &str = "files";
18pub const LOG_TARGET_STORY: &str = "story";
19pub const LOG_TARGET_TEST: &str = "test";
20
21#[allow(missing_docs, unused_variables, clippy::cognitive_complexity)]
22pub fn log_item<T: std::fmt::Display>(target: &'static str, item: &str, value: T) {
23    #[cfg(target_family = "wasm")]
24    let value = format!("{value:>10}");
25    #[cfg(not(target_family = "wasm"))]
26    let value = format!("{value:>10}").blue().to_string();
27    match target {
28        LOG_TARGET_TRANSFORM => tracing::info!(target: LOG_TARGET_TRANSFORM, "{item:<54}: {value}"),
29        LOG_TARGET_DATABASE => tracing::info!(target: LOG_TARGET_DATABASE, "{item:<54}: {value}"),
30        LOG_TARGET_PROJECT => tracing::info!(target: LOG_TARGET_PROJECT, "{item:<54}: {value}"),
31        LOG_TARGET_NUMBERS => tracing::info!(target: LOG_TARGET_NUMBERS, "{item:<54}: {value}"),
32        LOG_TARGET_COMMAND => tracing::info!(target: LOG_TARGET_COMMAND, "{item:<54}: {value}"),
33        LOG_TARGET_EXPORT => tracing::info!(target: LOG_TARGET_EXPORT, "{item:<54}: {value}"),
34        LOG_TARGET_CONFIG => tracing::info!(target: LOG_TARGET_CONFIG, "{item:<54}: {value}"),
35        LOG_TARGET_SPARQL => tracing::info!(target: LOG_TARGET_SPARQL, "{item:<54}: {value}"),
36        LOG_TARGET_FETCH => tracing::info!(target: LOG_TARGET_FETCH, "{item:<54}: {value}"),
37        LOG_TARGET_FILES => tracing::info!(target: LOG_TARGET_FILES, "{item:<54}: {value}"),
38        LOG_TARGET_TEST => tracing::info!(target: LOG_TARGET_TEST, "{item:<54}: {value}"),
39        _ => tracing::info!("{item:<54}: {value}"),
40    }
41}
42
43#[allow(missing_docs, unused_variables, clippy::cognitive_complexity)]
44pub fn log_item_debug<T: std::fmt::Display>(target: &str, item: &str, value: T) {
45    #[cfg(target_family = "wasm")]
46    let value = format!("{value:>10}");
47    #[cfg(not(target_family = "wasm"))]
48    let value = format!("{value:>10}").blue().to_string();
49    match target {
50        LOG_TARGET_TRANSFORM => {
51            tracing::debug!(target: LOG_TARGET_TRANSFORM, "{item:<54}: {value}")
52        },
53        LOG_TARGET_DATABASE => tracing::debug!(target: LOG_TARGET_DATABASE, "{item:<54}: {value}"),
54        LOG_TARGET_PROJECT => tracing::debug!(target: LOG_TARGET_PROJECT, "{item:<54}: {value}"),
55        LOG_TARGET_NUMBERS => tracing::debug!(target: LOG_TARGET_NUMBERS, "{item:<54}: {value}"),
56        LOG_TARGET_COMMAND => tracing::debug!(target: LOG_TARGET_COMMAND, "{item:<54}: {value}"),
57        LOG_TARGET_EXPORT => tracing::debug!(target: LOG_TARGET_EXPORT, "{item:<54}: {value}"),
58        LOG_TARGET_CONFIG => tracing::debug!(target: LOG_TARGET_CONFIG, "{item:<54}: {value}"),
59        LOG_TARGET_SPARQL => tracing::debug!(target: LOG_TARGET_SPARQL, "{item:<54}: {value}"),
60        LOG_TARGET_FETCH => tracing::debug!(target: LOG_TARGET_FETCH, "{item:<54}: {value}"),
61        LOG_TARGET_FILES => tracing::debug!(target: LOG_TARGET_FILES, "{item:<54}: {value}"),
62        LOG_TARGET_TEST => tracing::debug!(target: LOG_TARGET_TEST, "{item:<54}: {value}"),
63        _ => tracing::debug!("{item:<54}: {value}"),
64    }
65}
66
67/// Log a path as an item at info level, relative to the current directory
68pub fn log_path(target: &'static str, item: &str, path: &std::path::Path) -> Result<(), Error> {
69    let current_dir = std::env::current_dir()?;
70
71    match path.strip_prefix(current_dir.clone()) {
72        Ok(p) => {
73            log_item(target, item, format!("./{}", p.display()));
74        },
75        Err(_) => {
76            log_item(target, item, path.display());
77        },
78    }
79
80    Ok(())
81}
82
83/// Log a path as an item at debug level, relative to the current directory
84pub fn log_path_debug(
85    target: &'static str,
86    item: &str,
87    path: &std::path::Path,
88) -> Result<(), Error> {
89    let current_dir = std::env::current_dir()?;
90
91    match path.strip_prefix(current_dir.clone()) {
92        Ok(p) => {
93            log_item_debug(target, item, format!("./{}", p.display()));
94        },
95        Err(_) => {
96            log_item_debug(target, item, path.display());
97        },
98    }
99
100    Ok(())
101}
102
103// noinspection ALL
104#[allow(unused_variables, clippy::cognitive_complexity)]
105pub fn log_duration<'a, T, U>(target: &str, task: &str, f: T) -> U
106where T: FnOnce() -> U + 'a {
107    let start = std::time::Instant::now();
108    let result = f();
109    #[cfg(target_family = "wasm")]
110    let value = format!("{:}", start.elapsed().as_millis());
111    #[cfg(not(target_family = "wasm"))]
112    let value = format!("{:}", start.elapsed().as_millis())
113        .blue()
114        .to_string();
115    match target {
116        LOG_TARGET_TRANSFORM => {
117            tracing::info!(
118                target: LOG_TARGET_TRANSFORM,
119                "{task:49} took: {value:>10}ms"
120            );
121        },
122        LOG_TARGET_DATABASE => {
123            tracing::info!(
124                target: LOG_TARGET_DATABASE,
125                "{task:49} took: {value:>10}ms"
126            );
127        },
128        LOG_TARGET_PROJECT => {
129            tracing::info!(
130                target: LOG_TARGET_PROJECT,
131                "{task:49} took: {value:>10}ms"
132            );
133        },
134        LOG_TARGET_NUMBERS => {
135            tracing::info!(
136                target: LOG_TARGET_NUMBERS,
137                "{task:49} took: {value:>10}ms"
138            );
139        },
140        LOG_TARGET_COMMAND => {
141            tracing::info!(
142                target: LOG_TARGET_COMMAND,
143                "{task:49} took: {value:>10}ms"
144            );
145        },
146        LOG_TARGET_EXPORT => {
147            tracing::info!(
148                target: LOG_TARGET_EXPORT,
149                "{task:49} took: {value:>10}ms"
150            );
151        },
152        LOG_TARGET_CONFIG => {
153            tracing::info!(
154                target: LOG_TARGET_CONFIG,
155                "{task:49} took: {value:>10}ms"
156            );
157        },
158        LOG_TARGET_SPARQL => {
159            tracing::info!(
160                target: LOG_TARGET_SPARQL,
161                "{task:49} took: {value:>10}ms"
162            );
163        },
164        LOG_TARGET_FETCH => {
165            tracing::info!(
166                target: LOG_TARGET_FETCH,
167                "{task:49} took: {value:>10}ms"
168            );
169        },
170        LOG_TARGET_FILES => {
171            tracing::info!(
172                target: LOG_TARGET_FILES,
173                "{task:49} took: {value:>10}ms"
174            );
175        },
176        LOG_TARGET_TEST => {
177            tracing::info!(
178                target: LOG_TARGET_TEST,
179                "{task:49} took: {value:>10}ms"
180            );
181        },
182        _ => tracing::info!("{task:49} took: {value:>10}ms"),
183    }
184    result
185}
186
187#[cfg(target_family = "wasm")]
188pub fn does_console_support_color() -> bool { return false; }
189
190#[cfg(not(target_family = "wasm"))]
191pub fn does_console_support_color() -> bool {
192    if let Ok(term) = std::env::var("TERM") {
193        if term.as_str() == "dumb" {
194            return false;
195        }
196    }
197    if let Ok(no_color) = std::env::var("NO_COLOR") {
198        if no_color.as_str() == "1" {
199            return false;
200        }
201    }
202    if let Ok(style) = std::env::var("RUST_LOG_STYLE") {
203        if style.as_str() == "never" {
204            return false;
205        }
206    }
207
208    true
209}