Skip to content

Commit

Permalink
rust: use the same logging mechanism in both servers
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed May 15, 2024
1 parent de5b8f3 commit 28dd901
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 18 deletions.
20 changes: 2 additions & 18 deletions rust/agama-server/src/agama-dbus-server.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use agama_server::{
l10n::{self, helpers},
logs::start_logging,
questions,
};

use agama_lib::connection_to;
use anyhow::Context;
use log::{self, LevelFilter};
use std::future::pending;

const ADDRESS: &str = "unix:path=/run/agama/bus";
Expand All @@ -14,23 +14,7 @@ const SERVICE_NAME: &str = "org.opensuse.Agama1";
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let locale = helpers::init_locale()?;

// be smart with logging and log directly to journal if connected to it
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()
.unwrap();
log::set_max_level(LevelFilter::Info); // log only info for journal logger
} 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,
)
.unwrap(); // unwrap here as we are sure no other logger active
}
start_logging().context("Could not initialize the logger")?;

let connection = connection_to(ADDRESS)
.await
Expand Down
2 changes: 2 additions & 0 deletions rust/agama-server/src/agama-web-server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::{
use agama_lib::connection_to;
use agama_server::{
l10n::helpers,
logs::start_logging,
web::{self, generate_token, run_monitor},
};
use anyhow::Context;
Expand Down Expand Up @@ -292,6 +293,7 @@ async fn start_server(address: String, service: Router, ssl_acceptor: SslAccepto
/// Start serving the API.
/// `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();

Expand Down
1 change: 1 addition & 0 deletions rust/agama-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub mod cert;
pub mod dbus;
pub mod error;
pub mod l10n;
pub mod logs;
pub mod manager;
pub mod network;
pub mod questions;
Expand Down
17 changes: 17 additions & 0 deletions rust/agama-server/src/logs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use log::{self, LevelFilter, SetLoggerError};

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
} 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,
)?;
}
Ok(())
}

0 comments on commit 28dd901

Please sign in to comment.