Skip to content

Commit

Permalink
Merge branch 'main' into bump-deps
Browse files Browse the repository at this point in the history
  • Loading branch information
Gigas002 authored Aug 14, 2024
2 parents 568be62 + d2f4574 commit aac6cb5
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 20 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,21 @@ The cache is are stored in `/var/cache/regreet/cache.toml` (configurable during
It contains the last authenticated user and the last used session per user, which are automatically selected on next login.
If the greeter is unable to write to this file, then it reverts to the default behaviour.

The log file is stored in `/var/log/regreet/log` (configurable during installation).
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.
By default, the logs are stored in `/var/log/regreet/log` (configurable during installation).
You can use a log file in a different location with the `--logs` argument as follows:
```sh
regreet --logs /path/to/custom/regreet/logs
```

Once the log file reaches a limit, it is compressed and rotated to `log.X.gz` in the same directory, 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
64 changes: 46 additions & 18 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 @@ -38,10 +41,18 @@ enum LogLevel {
#[derive(Parser, Debug)]
#[command(author, version, about)]
struct Args {
/// The path to the log file
#[arg(short, long, value_name = "PATH", default_value = LOG_PATH)]
logs: PathBuf,

/// The verbosity level of the logs
#[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 +69,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.logs, &args.log_level, args.verbose);

let app = relm4::RelmApp::new(APP_ID);
app.run_async::<Greeter>(GreeterInit {
Expand All @@ -69,8 +80,7 @@ fn main() {
}

/// Initialize the log file with file rotation.
fn setup_log_file() -> IoResult<FileRotate<AppendCount>> {
let log_path = Path::new(LOG_PATH);
fn setup_log_file(log_path: &Path) -> IoResult<FileRotate<AppendCount>> {
if !log_path.exists() {
if let Some(log_dir) = log_path.parent() {
create_dir_all(log_dir)?;
Expand All @@ -95,7 +105,7 @@ fn setup_log_file() -> IoResult<FileRotate<AppendCount>> {
}

/// Initialize logging with file rotation.
fn init_logging(log_level: &LogLevel) -> WorkerGuard {
fn init_logging(log_path: &Path, 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 +116,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(log_path) {
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 +164,5 @@ fn init_logging(log_level: &LogLevel) -> WorkerGuard {
eprintln!("{panic}");
}));

guard
guards
}

0 comments on commit aac6cb5

Please sign in to comment.