Skip to content

Commit

Permalink
Add --logs to specify custom log file path
Browse files Browse the repository at this point in the history
  • Loading branch information
rharish101 committed Aug 3, 2024
1 parent 5c933a1 commit d2f4574
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,13 @@ 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.

Expand Down
13 changes: 8 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ 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,
Expand All @@ -65,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, args.verbose);
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 @@ -76,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 @@ -102,7 +105,7 @@ fn setup_log_file() -> IoResult<FileRotate<AppendCount>> {
}

/// Initialize logging with file rotation.
fn init_logging(log_level: &LogLevel, stdout: bool) -> Vec<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 @@ -125,7 +128,7 @@ fn init_logging(log_level: &LogLevel, stdout: bool) -> Vec<WorkerGuard> {
// Log in a separate non-blocking thread, then return the guard (otherise the non-blocking
// writer will immediately stop).
let mut guards = Vec::new();
match setup_log_file() {
match setup_log_file(log_path) {
Ok(file) => {
let (file, guard) = non_blocking(file);
guards.push(guard);
Expand Down

0 comments on commit d2f4574

Please sign in to comment.