Skip to content

Commit

Permalink
linkd: Add logging
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Simmerl <[email protected]>
  • Loading branch information
xla committed Sep 7, 2021
1 parent 765aa89 commit f3233fe
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
2 changes: 2 additions & 0 deletions linkd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ test = false
[dependencies]
anyhow = "1.0"
base64 = "0.13"
env_logger = "0.9"
futures = "0.3"
lazy_static = "1.4"
log = "0.4"
nix = "0.22"
signal-hook = { version = "0.3", default-features = false }
signal-hook-tokio = { version = "0.3", features = [ "futures-v0_3" ] }
Expand Down
5 changes: 2 additions & 3 deletions linkd/src/bin/linkd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,17 @@ use signal_hook_tokio::Signals;
use structopt::StructOpt as _;
use tokio::{net::UnixStream, spawn, sync::mpsc};
use tracing::info;
use tracing_subscriber::FmtSubscriber;

use librad::{
crypto::BoxedSigner,
net::{discovery, peer::Peer},
};

use linkd::{args::Args, cfg::Cfg, protocol, signals, socket_activation};
use linkd::{args::Args, cfg::Cfg, logging, protocol, signals, socket_activation};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
tracing::subscriber::set_global_default(FmtSubscriber::builder().finish())?;
logging::init();

let args = Args::from_args();
let cfg: Cfg<BoxedSigner> = Cfg::from_args::<UnixStream>(args).await?;
Expand Down
1 change: 1 addition & 0 deletions linkd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

pub mod args;
pub mod cfg;
pub mod logging;
pub mod protocol;
pub mod signals;
pub mod socket_activation;
51 changes: 51 additions & 0 deletions linkd/src/logging.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright © 2021 The Radicle Link Contributors
//
// This file is part of radicle-link, distributed under the GPLv3 with Radicle
// Linking Exception. For full terms see the included LICENSE file.

use std::env;

use log::{log_enabled, Level};
use tracing::subscriber::set_global_default as set_subscriber;
use tracing_subscriber::{EnvFilter, FmtSubscriber};

/// Initialise logging / tracing
///
/// The `TRACING_FMT` environment variable can be used to control the log
/// formatting. Supported values:
///
/// * "pretty": [`tracing_subscriber::fmt::format::Pretty`]
/// * "compact": [`tracing_subscriber::fmt::format::Compact`]
/// * "json": [`tracing_subscriber::fmt::format::Json`]
///
/// If the variable is not set, or set to any other value, the
/// [`tracing_subscriber::fmt::format::Full`] format is used.
pub fn init() {
if env_logger::builder().try_init().is_ok() {
if env::var("RUST_LOG").is_err() {
env::set_var("RUST_LOG", "debug");
}

let mut builder = FmtSubscriber::builder()
.with_env_filter(EnvFilter::from_default_env())
.with_test_writer();
if log_enabled!(target: "librad", Level::Trace) {
builder = builder.with_thread_ids(true);
} else if env::var("TRACING_FMT").is_err() {
let default_format = if env::var("CI").is_ok() {
"compact"
} else {
"pretty"
};
env::set_var("TRACING_FMT", default_format);
}

match env::var("TRACING_FMT").ok().as_deref() {
Some("pretty") => set_subscriber(builder.pretty().finish()),
Some("compact") => set_subscriber(builder.compact().finish()),
Some("json") => set_subscriber(builder.json().flatten_event(true).finish()),
_ => set_subscriber(builder.finish()),
}
.expect("setting tracing subscriber failed")
}
}

0 comments on commit f3233fe

Please sign in to comment.