Skip to content

Commit 9241819

Browse files
authored
Merge pull request #5940 from fdefelici/fix/win-log-isatty
feat: enable pretty print logging for windows
2 parents 3aec2b1 + 4cc177f commit 9241819

File tree

3 files changed

+26
-20
lines changed

3 files changed

+26
-20
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

stacks-common/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ slog = { version = "2.5.2", features = ["max_level_trace"] }
4040
slog-term = "2.6.0"
4141
slog-json = { version = "2.3.0", optional = true }
4242
chrono = "0.4.19"
43-
libc = "0.2.82"
4443
hashbrown = { workspace = true }
4544
rusqlite = { workspace = true, optional = true }
4645

stacks-common/src/util/log.rs

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ fn make_logger() -> Logger {
225225
let debug = env::var("STACKS_LOG_DEBUG") == Ok("1".into());
226226
let pretty_print = env::var("STACKS_LOG_PP") == Ok("1".into());
227227
let decorator = get_decorator();
228-
let atty = isatty(Stream::Stderr);
228+
let atty = isatty();
229229
let drain = TermFormat::new(decorator, pretty_print, debug, atty);
230230
Logger::root(drain.ignore_res(), o!())
231231
}
@@ -236,11 +236,23 @@ fn get_decorator() -> slog_term::PlainSyncDecorator<slog_term::TestStdoutWriter>
236236
slog_term::PlainSyncDecorator::new(slog_term::TestStdoutWriter)
237237
}
238238

239+
#[cfg(any(test, feature = "testing"))]
240+
fn isatty() -> bool {
241+
use std::io::IsTerminal;
242+
io::stdout().is_terminal()
243+
}
244+
239245
#[cfg(not(any(test, feature = "testing")))]
240246
fn get_decorator() -> slog_term::PlainSyncDecorator<std::io::Stderr> {
241247
slog_term::PlainSyncDecorator::new(std::io::stderr())
242248
}
243249

250+
#[cfg(not(any(test, feature = "testing")))]
251+
fn isatty() -> bool {
252+
use std::io::IsTerminal;
253+
io::stderr().is_terminal()
254+
}
255+
244256
fn inner_get_loglevel() -> slog::Level {
245257
if env::var("STACKS_LOG_TRACE") == Ok("1".into()) {
246258
slog::Level::Trace
@@ -331,21 +343,17 @@ fn color_if_tty(color: &str, isatty: bool) -> &str {
331343
}
332344
}
333345

334-
enum Stream {
335-
Stdout,
336-
Stderr,
337-
}
338-
339-
#[cfg(unix)]
340-
fn isatty(stream: Stream) -> bool {
341-
let fd = match stream {
342-
Stream::Stdout => libc::STDOUT_FILENO,
343-
Stream::Stderr => libc::STDERR_FILENO,
344-
};
345-
unsafe { libc::isatty(fd) != 0 }
346-
}
347-
348-
#[cfg(not(unix))]
349-
fn isatty(stream: Stream) -> bool {
350-
false
346+
#[cfg(test)]
347+
mod tests {
348+
use super::*;
349+
350+
#[test]
351+
#[ignore = "manual test"]
352+
fn test_log_pretty_print() {
353+
env::set_var("STACKS_LOG_PP", "1");
354+
let logger: Logger = make_logger();
355+
slog::slog_info!(logger, "Info test"); //equivalent to info!(..)
356+
slog::slog_warn!(logger, "Warn test"); //equivalent to warn!(..)
357+
slog::slog_error!(logger, "Erro test"); //equivalent to erro!(..)
358+
}
351359
}

0 commit comments

Comments
 (0)