Skip to content

Commit

Permalink
feat!: use the standard rustlang log for all logging purposes
Browse files Browse the repository at this point in the history
BREAKING this replaces the custom logging implementation with the standard rustlang/log library that is implemented for many backends

https://github.com/rust-lang/log
  • Loading branch information
aminya committed Nov 27, 2023
1 parent dce5ae1 commit c38fe84
Show file tree
Hide file tree
Showing 17 changed files with 69 additions and 434 deletions.
9 changes: 4 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,17 @@ yaml-rust = "0.4.5"
bimap = "0.6.1"
clap = { version = "4.2.2", features = ["derive"] }
anymap2 = "0.13.0"
tokio = { version = "1.28", features = ["rt", "sync","rt-multi-thread"] }
derive ={ path = "derive", version = "0.3.0" }
tokio = { version = "1.28", features = ["rt", "sync", "rt-multi-thread"] }
derive = { path = "derive", version = "0.3.0" }
thiserror = "1.0.50"
log = "0.4"
env_logger = "0.10.1"

[dev-dependencies]
log = "0.4"
simplelog = "0.12"
criterion = { version = "0.5.1", features = ["html_reports"] }

[features]
default = ["logger"]
logger = []
yaml = []
derive = ["derive/derive"]

Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ The fourth step is to create a `Dag` and put all the defined tasks into the `Dag

Optional step: You can specify an environment variable for `Dag`. This environment variable is available in all tasks. In some specific tasks, this behavior can be useful.

Finally, don’t forget to initialize the logger, and then you can call the `start` function of `Dag` to start executing all tasks.
Finally, you can [initialize a logger](https://github.com/rust-lang/log#in-executables), and then you can call the `start` function of `Dag` to start executing all tasks.

You can refer to an example for the above complete steps: `examples/compute_dag.rs`

Expand All @@ -87,7 +87,7 @@ Here is the `examples/compute_dag.rs` example:
extern crate dagrs;

use std::sync::Arc;
use dagrs::{log, Complex, Dag, DefaultTask, EnvVar, Input, LogLevel, Output};
use dagrs::{Complex, Dag, DefaultTask, EnvVar, Input, Output};

struct Compute(usize);

Expand All @@ -104,7 +104,8 @@ impl Complex for Compute {

fn main() {
// initialization log.
let _initialized = log::init_logger(LogLevel::Info, None);
env_logger::init();

// generate some tasks.
let a = DefaultTask::with_action("Compute A", Compute(1));

Expand Down
10 changes: 4 additions & 6 deletions benches/compute_dag_bench.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use criterion::{criterion_group, criterion_main, Criterion};

use dagrs::{log, Dag, DefaultTask, EnvVar, Input, LogLevel, Output, Task};
use dagrs::{Dag, DefaultTask, EnvVar, Input, Output, Task};
use std::sync::Arc;

fn calc(input: Input, env: Arc<EnvVar>) -> Output {
Expand Down Expand Up @@ -31,17 +31,15 @@ fn compute_dag(tasks: Vec<DefaultTask>) {
}

fn compute_dag_bench(bencher: &mut Criterion) {
let _initialized = log::init_logger(LogLevel::Off, None);
env_logger::init();

let mut tasks = (0..50usize)
.into_iter()
.map(|i_task| DefaultTask::with_closure(&i_task.to_string(), calc))
.collect::<Vec<_>>();

// consider 8 dependency for each task (except first 20 tasks)
for i_task in 20..tasks.len() {
let predecessors_id = ((i_task - 8)..i_task)
.into_iter()
.map(|i_dep| tasks[i_dep].id())
.collect::<Vec<_>>();

Expand All @@ -54,8 +52,8 @@ fn compute_dag_bench(bencher: &mut Criterion) {
criterion_group!(
name = benches;
config = {
let criterion = Criterion::default().sample_size(4000).noise_threshold(0.05);
criterion

Criterion::default().sample_size(4000).noise_threshold(0.05)
};
targets = compute_dag_bench
);
Expand Down
5 changes: 3 additions & 2 deletions examples/compute_dag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
extern crate dagrs;

use dagrs::{log, Complex, Dag, DefaultTask, EnvVar, Input, LogLevel, Output};
use dagrs::{Complex, Dag, DefaultTask, EnvVar, Input, Output};
use std::sync::Arc;

struct Compute(usize);
Expand All @@ -29,7 +29,8 @@ impl Complex for Compute {

fn main() {
// initialization log.
let _initialized = log::init_logger(LogLevel::Info, None);
env_logger::init();

// generate some tasks.
let a = DefaultTask::with_action("Compute A", Compute(1));

Expand Down
57 changes: 5 additions & 52 deletions examples/custom_log.rs
Original file line number Diff line number Diff line change
@@ -1,65 +1,18 @@
//! Use the simplelog and log libraries to implement the Logger trait to customize the log manager.
//! Use the simplelog for logging.
extern crate dagrs;
extern crate log;
extern crate simplelog;

use std::collections::HashMap;

use dagrs::{Dag, LogLevel, Logger};
use dagrs::Dag;
use simplelog::*;

struct MyLogger {
level: LogLevel,
}

impl MyLogger {
/// Create MyLogger and set the log level of simplelog.
fn new(level: LogLevel) -> Self {
let filter = match level {
LogLevel::Debug => LevelFilter::Debug,
LogLevel::Info => LevelFilter::Info,
LogLevel::Warn => LevelFilter::Warn,
LogLevel::Error => LevelFilter::Error,
LogLevel::Off => LevelFilter::Off,
};
CombinedLogger::init(vec![TermLogger::new(
filter,
Config::default(),
TerminalMode::Mixed,
ColorChoice::Auto,
)])
.unwrap();
MyLogger { level }
}
}

/// In turn, use the corresponding logging macro of log to override the Logger method.
impl Logger for MyLogger {
fn level(&self) -> LogLevel {
self.level
}

fn debug(&self, msg: String) {
log::debug!("{}", msg);
}

fn info(&self, msg: String) {
log::info!("{}", msg);
}

fn warn(&self, msg: String) {
log::warn!("{}", msg);
}

fn error(&self, msg: String) {
log::error!("{}", msg);
}
}

fn main() {
// Initialize the global logger with a custom logger.
let _initialized = dagrs::log::init_custom_logger(MyLogger::new(LogLevel::Info));
// Initialize the global logger with a simplelogger as the logging backend.
let _ = SimpleLogger::init(LevelFilter::Info, Config::default());

let mut dag = Dag::with_yaml("tests/config/correct.yaml", HashMap::new()).unwrap();
assert!(dag.start().unwrap());
}
4 changes: 2 additions & 2 deletions examples/custom_parser_and_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use std::collections::HashMap;
use std::fmt::{Display, Formatter};
use std::{fs, sync::Arc};

use dagrs::{log, Action, CommandAction, Dag, LogLevel, ParseError, Parser, Task};
use dagrs::{Action, CommandAction, Dag, ParseError, Parser, Task};

struct MyTask {
tid: (String, usize),
Expand Down Expand Up @@ -138,7 +138,7 @@ impl Parser for ConfigParser {
}

fn main() {
let _initialized = log::init_logger(LogLevel::Info, None);
env_logger::init();
let file = "tests/config/custom_file_task.txt";
let mut dag =
Dag::with_config_file_and_parser(file, Box::new(ConfigParser), HashMap::new()).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions examples/dependencies.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use dagrs::{dependencies, log, Complex, EnvVar, Input, LogLevel, Output};
use dagrs::{dependencies, Complex, EnvVar, Input, Output};
use std::sync::Arc;

/// The `dependencies` macro allows users to specify all task dependencies in an easy-to-understand
Expand Down Expand Up @@ -43,7 +43,7 @@ impl Complex for Compute {
}

fn main() {
let _initialized = log::init_logger(LogLevel::Info, None);
env_logger::init();
let mut tasks = dependencies!(
a -> b c d,
b -> e g,
Expand Down
4 changes: 2 additions & 2 deletions examples/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ extern crate dagrs;

use std::collections::HashMap;

use dagrs::{log, Dag, DefaultTask, Engine, LogLevel, Output};
use dagrs::{Dag, DefaultTask, Engine, Output};
fn main() {
// initialization log.
let _initialized = log::init_logger(LogLevel::Info, None);
env_logger::init();
// Create an Engine.
let mut engine = Engine::default();

Expand Down
4 changes: 2 additions & 2 deletions examples/yaml_dag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
extern crate dagrs;

use dagrs::{log, Dag, LogLevel};
use dagrs::Dag;
use std::collections::HashMap;

fn main() {
let _initialized = log::init_logger(LogLevel::Info, None);
env_logger::init();
let mut job = Dag::with_yaml("tests/config/correct.yaml", HashMap::new()).unwrap();
assert!(job.start().unwrap());
}
44 changes: 24 additions & 20 deletions src/bin/dagrs.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::HashMap;
use std::{collections::HashMap, fs::File, str::FromStr};

use clap::Parser;
use dagrs::{log, Dag, LogLevel};
use dagrs::Dag;

#[derive(Parser, Debug)]
#[command(name = "dagrs", version = "0.2.0")]
Expand All @@ -12,31 +12,35 @@ struct Args {
/// yaml configuration file path.
#[arg(long)]
yaml: String,
/// Log level, the default is Info.
/// Log level, the default is 'info'.
#[arg(long)]
log_level: Option<String>,
}

fn main() {
let args = Args::parse();
let log_level = args
.log_level
.map_or(LogLevel::Info, |level| match level.as_str() {
"debug" => LogLevel::Debug,
"info" => LogLevel::Info,
"warn" => LogLevel::Warn,
"error" => LogLevel::Error,
"off" => LogLevel::Off,
_ => {
println!("The logging level can only be [debug,info,warn,error,off]");
std::process::abort();
}
});
let _initialized = match args.log_path {
None => log::init_logger(log_level, None),
Some(path) => log::init_logger(log_level, Some(std::fs::File::create(path).unwrap())),
};

init_logger(&args);

let yaml_path = args.yaml;
let mut dag = Dag::with_yaml(yaml_path.as_str(), HashMap::new()).unwrap();
assert!(dag.start().unwrap());
}

fn init_logger(args: &Args) {
let log_level = match &args.log_level {
Some(level_str) => log::LevelFilter::from_str(level_str).unwrap(),
None => log::LevelFilter::Info,
};
let mut logger_builder = env_logger::Builder::new();
logger_builder.filter_level(log_level);

// initialize the env_logger with the given log_path
if let Some(log_path) = &args.log_path {
logger_builder.target(env_logger::Target::Pipe(Box::new(
File::create(log_path).unwrap(),
)));
};

logger_builder.init();
}
33 changes: 11 additions & 22 deletions src/engine/dag.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use super::{graph::Graph, DagError};
use crate::{
task::{ExecState, Input, Task},
utils::{log, EnvVar},
utils::EnvVar,
Action, Parser,
};
use anymap2::any::CloneAnySendSync;
use log::{error, info};
use std::{
collections::HashMap,
panic::{self, AssertUnwindSafe},
Expand Down Expand Up @@ -36,9 +37,9 @@ use tokio::task::JoinHandle;
///
/// # Example
/// ```rust
/// use dagrs::{log,LogLevel,Dag, DefaultTask, Output,Input,EnvVar,Action};
/// use dagrs::{Dag, DefaultTask, Output,Input,EnvVar,Action};
/// use std::sync::Arc;
/// log::init_logger(LogLevel::Info,None);
/// env_logger::init();
/// let task=DefaultTask::with_closure("Simple Task",|_input,_env|{
/// Output::new(1)
/// });
Expand Down Expand Up @@ -219,7 +220,7 @@ impl Dag {
self.exe_sequence
.iter()
.for_each(|id| exe_seq.push_str(&format!(" -> {}", self.tasks[id].name())));
log::info(format!("{} -> [End]", exe_seq));
info!("{} -> [End]", exe_seq);
let mut handles = Vec::new();
self.exe_sequence.iter().for_each(|id| {
handles.push((*id, self.execute_task(&self.tasks[id])));
Expand All @@ -234,10 +235,7 @@ impl Dag {
}
}
Err(err) => {
log::error(format!(
"Task execution encountered an unexpected error! {}",
err
));
error!("Task execution encountered an unexpected error! {}", err);
self.handle_error(tid).await;
}
}
Expand Down Expand Up @@ -280,38 +278,29 @@ impl Dag {
}
}
}
log::info(format!(
"Executing task [name: {}, id: {}]",
task_name, task_id
));
info!("Executing task [name: {}, id: {}]", task_name, task_id);
// Concrete logical behavior for performing tasks.
panic::catch_unwind(AssertUnwindSafe(|| action.run(Input::new(inputs), env)))
.map_or_else(
|_| {
log::error(format!(
"Execution failed [name: {}, id: {}]",
task_name, task_id
));
error!("Execution failed [name: {}, id: {}]", task_name, task_id);
false
},
|out| {
// Store execution results
if out.is_err() {
log::error(format!(
error!(
"Execution failed [name: {}, id: {}]\nerr: {}",
task_name,
task_id,
out.get_err().unwrap()
));
);
false
} else {
execute_state.set_output(out);
execute_state.exe_success();
execute_state.semaphore().add_permits(task_out_degree);
log::info(format!(
"Execution succeed [name: {}, id: {}]",
task_name, task_id
));
info!("Execution succeed [name: {}, id: {}]", task_name, task_id);
true
}
},
Expand Down
Loading

0 comments on commit c38fe84

Please sign in to comment.