Skip to content

Commit

Permalink
rust: use 'tracing' as the logs system
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed May 15, 2024
1 parent fc46649 commit 0961881
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 82 deletions.
69 changes: 5 additions & 64 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions rust/agama-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ anyhow = "1.0"
agama-locale-data = { path = "../agama-locale-data" }
agama-lib = { path = "../agama-lib" }
log = "0.4"
simplelog = "0.12.1"
systemd-journal-logger = "1.0"
zbus = { version = "3", default-features = false, features = ["tokio"] }
zbus_macros = "3"
uuid = { version = "1.3.4", features = ["v4"] }
Expand Down Expand Up @@ -53,7 +51,10 @@ openssl = "0.10.64"
hyper = "1.2.0"
hyper-util = "0.1.3"
tokio-openssl = "0.6.4"
futures-util = { version = "0.3.30", default-features = false, features = ["alloc"] }
futures-util = { version = "0.3.30", default-features = false, features = [
"alloc",
] }
libsystemd = "0.7.0"

[[bin]]
name = "agama-dbus-server"
Expand Down
3 changes: 0 additions & 3 deletions rust/agama-server/src/agama-web-server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ use openssl::ssl::{Ssl, SslAcceptor, SslFiletype, SslMethod};
use tokio::sync::broadcast::channel;
use tokio_openssl::SslStream;
use tower::Service;
use tracing_subscriber::prelude::*;
use utoipa::OpenApi;

const DEFAULT_WEB_UI_DIR: &str = "/usr/share/agama/web_ui";
Expand Down Expand Up @@ -294,8 +293,6 @@ async fn start_server(address: String, service: Router, ssl_acceptor: SslAccepto
/// `options`: command-line arguments.
async fn serve_command(args: ServeArgs) -> anyhow::Result<()> {
start_logging().context("Could not initialize the logger")?;
let journald = tracing_journald::layer().context("could not connect to journald")?;
tracing_subscriber::registry().with(journald).init();

let (tx, _) = channel(16);
run_monitor(tx.clone()).await?;
Expand Down
25 changes: 13 additions & 12 deletions rust/agama-server/src/logs.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
use log::{self, LevelFilter, SetLoggerError};
//! Functions to work with logs.

pub fn start_logging() -> Result<(), SetLoggerError> {
if systemd_journal_logger::connected_to_journal() {
// unwrap here is intentional as we are sure no other logger is active yet
systemd_journal_logger::JournalLog::default().install()?;
log::set_max_level(LevelFilter::Info); // log only info for journal logger
use anyhow::Context;
use libsystemd::logging;
use tracing_subscriber::prelude::*;

/// Initializes the logging mechanism.
///
/// It is based on [Tracing](https://github.com/tokio-rs/tracing), part of the Tokio ecosystem.
pub fn start_logging() -> anyhow::Result<()> {
if logging::connected_to_journal() {
let journald = tracing_journald::layer().context("could not connect to journald")?;
tracing_subscriber::registry().with(journald).init();
} else {
simplelog::TermLogger::init(
LevelFilter::Info, // lets use info, trace provides too much output from libraries
simplelog::Config::default(),
simplelog::TerminalMode::Stderr, // only stderr output for easier filtering
simplelog::ColorChoice::Auto,
)?;
tracing_subscriber::fmt::init();
}
Ok(())
}

0 comments on commit 0961881

Please sign in to comment.