Skip to content

Commit 399e424

Browse files
authored
Expose more fields and types for callers that want more control. (#17)
This PR makes access to fields of the logger and process config public and provides a limited set of constructor functions. This will allow using those types with your own configuration machinery.
1 parent 22d0a44 commit 399e424

File tree

4 files changed

+54
-14
lines changed

4 files changed

+54
-14
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ jobs:
77
strategy:
88
matrix:
99
os: [ubuntu-latest, windows-latest, macOS-latest]
10-
rust: [1.81.0, stable, beta, nightly]
10+
rust: [1.82.0, stable, beta, nightly]
1111
steps:
1212
- name: Checkout repository
1313
uses: actions/checkout@v1

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "daemonbase"
33
version = "0.1.4"
44
edition = "2021"
5-
rust-version = "1.81"
5+
rust-version = "1.82"
66
authors = ["NLnet Labs <rust-team@nlnetlabs.nl>"]
77
description = "A library for providing the foundation for daemon processes."
88
documentation = "https://docs.rs/daemonbase/"

src/logging.rs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,17 @@ use std::path::PathBuf;
77
use std::str::FromStr;
88
use std::sync::{Mutex, MutexGuard, OnceLock};
99
use clap::ArgAction;
10-
use log::LevelFilter;
1110
use log::error;
1211
use serde::{Deserialize, Deserializer, Serialize, Serializer};
1312
use crate::config::{ConfigFile, ConfigPath};
1413
use crate::error::{ExitError, Failed};
1514

15+
// Export LevelFilter for clients that don't use log, e.g. if they use tracing
16+
// instead.
17+
pub use log::LevelFilter;
18+
19+
#[cfg(unix)]
20+
pub use syslog::Facility;
1621

1722
//------------ Logger --------------------------------------------------------
1823

@@ -27,6 +32,13 @@ pub struct Logger {
2732
}
2833

2934
impl Logger {
35+
pub fn new(level: LevelFilter, target: Target) -> Self {
36+
Self {
37+
level,
38+
target,
39+
}
40+
}
41+
3042
/// Initialize logging.
3143
///
3244
/// Initializes the logging system so it can be used before having
@@ -381,11 +393,25 @@ pub struct Args {
381393
}
382394

383395
impl Args {
384-
pub fn to_config(&self) -> Config {
385-
Config::from_args(self)
396+
#[cfg(unix)]
397+
pub fn is_syslog(&self) -> bool {
398+
self.syslog
399+
}
400+
401+
pub fn is_stderr(&self) -> bool {
402+
self.stderr
386403
}
387404

388-
fn opt_level(&self) -> Option<LevelFilter> {
405+
pub fn log_file(&self) -> Option<&LogPath> {
406+
self.logfile.as_ref()
407+
}
408+
409+
#[cfg(unix)]
410+
pub fn syslog_facility(&self) -> Option<&unix::FacilityArg> {
411+
self.syslog_facility.as_ref()
412+
}
413+
414+
pub fn opt_level(&self) -> Option<LevelFilter> {
389415
if self.verbose > 1 {
390416
Some(LevelFilter::Debug)
391417
}
@@ -402,6 +428,10 @@ impl Args {
402428
None
403429
}
404430
}
431+
432+
pub fn to_config(&self) -> Config {
433+
Config::from_args(self)
434+
}
405435
}
406436

407437

src/process.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Process management.
22
33
#[cfg(unix)]
4-
pub use self::unix::{Args, Config, Process};
4+
pub use self::unix::{Args, Config, Process, UserId, GroupId};
55

66
#[cfg(target_os = "linux")]
77
pub use self::linux::EnvSockets;
@@ -491,10 +491,20 @@ mod unix {
491491
Ok(self)
492492
}
493493

494+
pub fn with_user_id(mut self, user_id: UserId) -> Self {
495+
self.user = Some(user_id);
496+
self
497+
}
498+
494499
pub fn with_group(mut self, v: &str) -> Result<Self, String> {
495500
self.group = Some(GroupId::from_str(v)?);
496501
Ok(self)
497502
}
503+
504+
pub fn with_group_id(mut self, group_id: GroupId) -> Self {
505+
self.group = Some(group_id);
506+
self
507+
}
498508
}
499509

500510

@@ -505,23 +515,23 @@ mod unix {
505515
pub struct Args {
506516
/// The file for keep the daemon process's PID in
507517
#[arg(long, value_name = "PATH")]
508-
pid_file: Option<ConfigPath>,
518+
pub pid_file: Option<ConfigPath>,
509519

510520
/// The working directory of the daemon process
511521
#[arg(long, value_name = "PATH")]
512-
working_dir: Option<ConfigPath>,
522+
pub working_dir: Option<ConfigPath>,
513523

514524
/// Root directory for the daemon process
515525
#[arg(long, value_name = "PATH")]
516-
chroot: Option<ConfigPath>,
526+
pub chroot: Option<ConfigPath>,
517527

518528
/// User for the daemon process
519529
#[arg(long, value_name = "UID")]
520-
user: Option<UserId>,
530+
pub user: Option<UserId>,
521531

522532
/// Group for the daemon process
523533
#[arg(long, value_name = "GID")]
524-
group: Option<GroupId>,
534+
pub group: Option<GroupId>,
525535
}
526536

527537
impl Args {
@@ -536,7 +546,7 @@ mod unix {
536546
/// A user ID in configuration.
537547
#[derive(Clone, Debug, Deserialize, Serialize)]
538548
#[serde(try_from = "String", into = "String", expecting = "a user name")]
539-
struct UserId {
549+
pub struct UserId {
540550
/// The user name.
541551
///
542552
/// This is used for error reporting.
@@ -601,7 +611,7 @@ mod unix {
601611
/// A user ID in configuration.
602612
#[derive(Clone, Debug, Deserialize, Serialize)]
603613
#[serde(try_from = "String", into = "String", expecting = "a user name")]
604-
struct GroupId {
614+
pub struct GroupId {
605615
/// The group name.
606616
name: String,
607617

0 commit comments

Comments
 (0)