Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use custom logging facility instead of tracing #37

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions airupd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,3 @@ ciborium = "0.2"
libc = "0.2"
thiserror = "1"
tokio = { workspace = true }
tracing = "0.1"
tracing-subscriber = "0.3"
18 changes: 12 additions & 6 deletions airupd/src/ace/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,24 @@ pub fn console_setup(args: Vec<String>) -> JoinHandle<i32> {
}

pub fn console_info(args: Vec<String>) -> JoinHandle<i32> {
tracing::info!(target: "console", "{}", merge_args(&args));
tokio::spawn(async { 0 })
tokio::spawn(async move {
crate::inform!("{}", merge_args(&args));
0
})
}

pub fn console_warn(args: Vec<String>) -> JoinHandle<i32> {
tracing::warn!(target: "console", "{}", merge_args(&args));
tokio::spawn(async { 0 })
tokio::spawn(async move {
crate::warn!("{}", merge_args(&args));
0
})
}

pub fn console_error(args: Vec<String>) -> JoinHandle<i32> {
tracing::error!(target: "console", "{}", merge_args(&args));
tokio::spawn(async { 0 })
tokio::spawn(async move {
crate::report_error!("{}", merge_args(&args));
0
})
}

pub fn noop(_: Vec<String>) -> JoinHandle<i32> {
Expand Down
138 changes: 138 additions & 0 deletions airupd/src/log.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
//! Airupd logging facility.

use std::sync::OnceLock;
use tokio::{sync::mpsc, task::AbortHandle};

static LOGGER: OnceLock<Logger> = OnceLock::new();

#[macro_export]
macro_rules! inform {
($($arg:tt)*) => {
$crate::log::logger().inform(format!($($arg)*)).await
};
}

#[macro_export]
macro_rules! warn {
($($arg:tt)*) => {
$crate::log::logger().warn(format!($($arg)*)).await
};
}

#[macro_export]
macro_rules! report_error {
($($arg:tt)*) => {
$crate::log::logger().report_error(format!($($arg)*)).await
};
}

/// Builder of `airupd`-flavor tracing configuration.
#[derive(Debug, Clone)]
pub struct Builder {
quiet: bool,
verbose: bool,
color: bool,
}
impl Builder {
/// Creates a new [`Builder`] instance with default settings.
#[inline]
#[must_use]
pub fn new() -> Self {
Self::default()
}

/// Sets whether console output is disabled for the logger.
#[inline]
pub fn quiet(&mut self, val: bool) -> &mut Self {
self.quiet = val;
self
}

/// Sets whether console output is verbose for the logger.
#[inline]
pub fn verbose(&mut self, val: bool) -> &mut Self {
self.verbose = val;
self
}

/// Sets whether colorful console output is enabled for the logger.
#[inline]
pub fn color(&mut self, val: bool) -> &mut Self {
self.color = val;
self
}

/// Initializes the logger.
#[inline]
pub fn init(&mut self) {
let (tx, rx) = mpsc::channel(16);
let logger_impl = LoggerImpl { rx };
let background = tokio::spawn(logger_impl.run()).abort_handle();
LOGGER.set(Logger { tx, background }).unwrap();
}
}
impl Default for Builder {
fn default() -> Self {
Self {
quiet: false,
verbose: false,
color: true,
}
}
}

#[derive(Debug)]
pub struct Logger {
tx: mpsc::Sender<Request>,
background: AbortHandle,
}
impl Logger {
pub async fn inform(&self, s: String) {
_ = self.tx.send(Request::Inform(s)).await;
}

pub async fn warn(&self, s: String) {
_ = self.tx.send(Request::Warn(s)).await;
}

pub async fn report_error(&self, s: String) {
_ = self.tx.send(Request::ReportError(s)).await;
}
}
impl Drop for Logger {
fn drop(&mut self) {
self.background.abort();
}
}

pub fn logger() -> &'static Logger {
LOGGER.get().unwrap()
}

struct LoggerImpl {
rx: mpsc::Receiver<Request>,
}
impl LoggerImpl {
async fn run(mut self) {
while let Some(req) = self.rx.recv().await {
match req {
Request::Inform(s) => {
eprintln!("\x1b[32m * \x1b[0m {s}");
}
Request::Warn(s) => {
eprintln!("\x1b[33m * \x1b[0m {s}");
}
Request::ReportError(s) => {
eprintln!("\x1b[31m * \x1b[0m {s}");
}
}
}
}
}

#[derive(Debug, Clone)]
enum Request {
Inform(String),
Warn(String),
ReportError(String),
}
85 changes: 0 additions & 85 deletions airupd/src/logging.rs

This file was deleted.

5 changes: 2 additions & 3 deletions airupd/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod env;
mod events;
mod extension;
mod lifetime;
mod logging;
mod log;
mod milestones;
mod rpc;
mod storage;
Expand All @@ -20,8 +20,7 @@ async fn main() {
airupfx::init().await;
let cmdline = self::env::Cmdline::parse();

logging::Builder::new()
.name("airupd")
log::Builder::new()
.quiet(cmdline.quiet)
.color(!cmdline.no_color)
.verbose(cmdline.verbose)
Expand Down
Loading