Skip to content

Commit

Permalink
Add --verbose to log also to stdout
Browse files Browse the repository at this point in the history
Useful when using demo mode.
  • Loading branch information
rharish101 committed Aug 3, 2024
1 parent 032e03f commit 5c933a1
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 16 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,12 @@ The log file is stored in `/var/log/regreet/log` (configurable during installati
Once the log file reaches a limit, it is compressed and rotated to `/var/log/regreet/log.X.gz`, where `X` is the index of the log file.
The higher the index, the older the log file.
After reaching a limit, the oldest log file is removed.

If the greeter is unable to write to this file or create files in the log directory, then it logs to stdout.
You can also print the logs to stdout in addition to the log file, with the `--verbose` argument as follows:
```sh
regreet --verbose
```

The recommended configuration is to run greetd greeters as a separate user (`greeter` in the above examples).
This can lead to insufficient permissions for either creating the cache/log directories, or writing to them.
Expand Down
57 changes: 41 additions & 16 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ use std::path::{Path, PathBuf};

use clap::{Parser, ValueEnum};
use file_rotate::{compression::Compression, suffix::AppendCount, ContentLimit, FileRotate};
use tracing::subscriber::set_global_default;
use tracing_appender::{non_blocking, non_blocking::WorkerGuard};
use tracing_subscriber::{filter::LevelFilter, fmt::time::OffsetTime};
use tracing_subscriber::{
filter::LevelFilter, fmt::layer, fmt::time::OffsetTime, layer::SubscriberExt,
};

use crate::constants::{APP_ID, CONFIG_PATH, CSS_PATH, LOG_PATH};
use crate::gui::{Greeter, GreeterInit};
Expand All @@ -42,6 +45,10 @@ struct Args {
#[arg(short, long, value_name = "LEVEL", default_value = "info")]
log_level: LogLevel,

/// Output all logs to stdout
#[arg(short, long)]
verbose: bool,

/// The path to the config file
#[arg(short, long, value_name = "PATH", default_value = CONFIG_PATH)]
config: PathBuf,
Expand All @@ -58,7 +65,7 @@ struct Args {
fn main() {
let args = Args::parse();
// Keep the guard alive till the end of the function, since logging depends on this.
let _guard = init_logging(&args.log_level);
let _guard = init_logging(&args.log_level, args.verbose);

let app = relm4::RelmApp::new(APP_ID);
app.run_async::<Greeter>(GreeterInit {
Expand Down Expand Up @@ -95,7 +102,7 @@ fn setup_log_file() -> IoResult<FileRotate<AppendCount>> {
}

/// Initialize logging with file rotation.
fn init_logging(log_level: &LogLevel) -> WorkerGuard {
fn init_logging(log_level: &LogLevel, stdout: bool) -> Vec<WorkerGuard> {
// Parse the log level string.
let filter = match log_level {
LogLevel::Off => LevelFilter::OFF,
Expand All @@ -106,27 +113,45 @@ fn init_logging(log_level: &LogLevel) -> WorkerGuard {
LogLevel::Trace => LevelFilter::TRACE,
};

// Load the timer before spawning threads, otherwise getting the local time offset will fail.
let timer = OffsetTime::local_rfc_3339().expect("Couldn't get local time offset");

// Set up the logger.
let logger = tracing_subscriber::fmt()
let builder = tracing_subscriber::fmt()
.with_max_level(filter)
// Load the timer before spawning threads, otherwise getting the local time offset will
// fail.
.with_timer(OffsetTime::local_rfc_3339().expect("Couldn't get local time offset"));
// The timer could be reused later.
.with_timer(timer.clone());

// Log in a separate non-blocking thread, then return the guard (otherise the non-blocking
// writer will immediately stop).
let guard = match setup_log_file() {
let mut guards = Vec::new();
match setup_log_file() {
Ok(file) => {
let (file, guard) = non_blocking(file);
// Disable colouring through ANSI escape sequences in log files.
logger.with_writer(file).with_ansi(false).init();
guard
guards.push(guard);
let builder = builder
.with_writer(file)
// Disable colouring through ANSI escape sequences in log files.
.with_ansi(false);

if stdout {
let (stdout, guard) = non_blocking(std::io::stdout());
guards.push(guard);
set_global_default(
builder
.finish()
.with(layer().with_writer(stdout).with_timer(timer)),
)
.unwrap();
} else {
builder.init();
};
}
Err(err) => {
println!("ERROR: Couldn't create log file '{LOG_PATH}': {err}");
Err(file_err) => {
let (file, guard) = non_blocking(std::io::stdout());
logger.with_writer(file).init();
guard
guards.push(guard);
builder.with_writer(file).init();
tracing::error!("Couldn't create log file '{LOG_PATH}': {file_err}");
}
};

Expand All @@ -136,5 +161,5 @@ fn init_logging(log_level: &LogLevel) -> WorkerGuard {
eprintln!("{panic}");
}));

guard
guards
}

0 comments on commit 5c933a1

Please sign in to comment.