Skip to content

Commit

Permalink
splitting out into crates
Browse files Browse the repository at this point in the history
  • Loading branch information
jsoverson committed May 31, 2021
1 parent 2b9ca9e commit 7dd8f29
Show file tree
Hide file tree
Showing 69 changed files with 4,626 additions and 217 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
/target
**/target
15 changes: 9 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ readme = "README.md"

[dependencies]
wapc = { version = "0.10.1" }
vino-guest = { path="../vino-guest" }
vino-guest = { path = "../vino-guest" }
chrono = "0.4.19"
colored = "2.0.0"
log = "0.4.11"
Expand All @@ -24,7 +24,7 @@ actix-rt = "2.1.0"
nats = "0.8.6"
structopt = "0.3.21"
nkeys = "0.1.0"
futures="0.3.13"
futures = "0.3.13"
serde_json = "1.0.64"
serde_yaml = "0.8.17"
serdeconv = "0.4.0"
Expand All @@ -33,10 +33,11 @@ thiserror = "1.0.24"
oci-distribution = "0.6"
itertools = "0.10.0"
unsafe-io = "= 0.6.2"
vino-runtime = {path="./crates/vino-runtime"}
vino-runtime = { path = "./crates/vino-runtime" }
logger = { path = "./crates/logger" }

[dev-dependencies]

test_bin = "0.3.0"

[[bin]]
name = "vino"
Expand All @@ -45,6 +46,8 @@ path = "src/main.rs"

[workspace]
members = [
"crates/vino-runtime",
"crates/vinoctl",
"crates/logger",
"crates/vino-configfile",
"crates/vino-runtime",
]

16 changes: 16 additions & 0 deletions crates/logger/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "logger"
version = "0.1.0"
authors = ["Jarrod Overson <jsoverson@gmail.com>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
chrono = "0.4.19"
colored = "2.0.0"
log = "0.4.11"
env_logger = "0.8.3"
anyhow = "1.0.40"
serde_json = "1.0.64"
structopt = "0.3.21"
73 changes: 37 additions & 36 deletions src/logger.rs → crates/logger/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
pub mod options;

use chrono::{DateTime, Utc};
use colored::Colorize;
use env_logger::filter::{Builder, Filter};

use log::{Level, LevelFilter, Log, Metadata, Record, SetLoggerError};
use serde_json::json;
use structopt::lazy_static::lazy_static;

use crate::commands::LoggingOpts;
use anyhow::Result;
pub use options::LoggingOptions;

const FILTER_ENV: &str = "VINO_LOG";
use std::time::SystemTime;
Expand All @@ -17,43 +18,37 @@ pub struct Logger {
json: bool,
}

lazy_static! {
static ref RELEVANT_MODULES: Vec<&'static str> =
vec!["vino", "wasmcloud", "wasmcloud_host", "wapc"];
static ref CHATTY_MODULES: Vec<&'static str> = vec![
// "wasmcloud_host::capability::native_host",
// "wasmcloud_nats_kvcache"
];
}

fn set_level(builder: &mut Builder, level: LevelFilter) {
for module in RELEVANT_MODULES.iter() {
builder.filter_module(module, level);
}

for module in CHATTY_MODULES.iter() {
builder.filter_module(module, log::LevelFilter::Off);
fn set_level<T: AsRef<str>>(builder: &mut Builder, priority_modules: &[T], level: LevelFilter) {
for module in priority_modules.iter() {
builder.filter_module(module.as_ref(), level);
}
}

impl Logger {
fn new(opts: &LoggingOpts) -> Logger {
fn new<T: AsRef<str>>(
opts: &LoggingOptions,
priority_modules: &[T],
chatty_modules: &[T],
) -> Logger {
let mut builder = Builder::new();

builder.filter_level(log::LevelFilter::Off);

if opts.quiet {
set_level(&mut builder, log::LevelFilter::Error);
} else if opts.trace {
set_level(&mut builder, log::LevelFilter::Trace);
} else if opts.debug {
set_level(&mut builder, log::LevelFilter::Debug);
} else {
set_level(&mut builder, log::LevelFilter::Info);
}

if let Ok(ref filter) = std::env::var(FILTER_ENV) {
builder.parse(filter);
} else {
builder.filter_level(log::LevelFilter::Off);
if opts.quiet {
set_level(&mut builder, priority_modules, log::LevelFilter::Error);
} else if opts.trace {
set_level(&mut builder, priority_modules, log::LevelFilter::Trace);
} else if opts.debug {
set_level(&mut builder, priority_modules, log::LevelFilter::Debug);
} else {
set_level(&mut builder, priority_modules, log::LevelFilter::Info);
}

for module in chatty_modules.iter() {
builder.filter_module(module.as_ref(), log::LevelFilter::Off);
}
}

Logger {
Expand All @@ -63,18 +58,24 @@ impl Logger {
}
}

pub fn init(opts: &LoggingOpts) -> Result<(), SetLoggerError> {
let logger = Self::new(opts);
pub fn init<T: AsRef<str>>(
opts: &LoggingOptions,
priority_modules: &[T],
chatty_modules: &[T],
) -> Result<(), SetLoggerError> {
let logger = Self::new(opts, priority_modules, chatty_modules);

log::set_max_level(logger.inner.filter());
log::set_boxed_logger(Box::new(logger))
log::set_boxed_logger(Box::new(logger))?;
log::trace!("logger initialized");
Ok(())
}

fn format(&self, record: &Record) -> String {
let datetime: DateTime<Utc> = SystemTime::now().into();
let datestring = datetime.format("%Y-%m-%d %T");
if self.json {
json!({ "timestamp": datetime.to_rfc3339(), "level": record.level(), "msg": format!("{}", record.args()) }).to_string()
json!({ "timestamp": datetime.to_rfc3339(), "level": record.level().to_string(), "msg": format!("{}", record.args()) }).to_string()
} else {
let msg = if self.verbose {
format!(
Expand Down Expand Up @@ -109,7 +110,7 @@ impl Log for Logger {
fn log(&self, record: &Record) {
// Check if the record is matched by the logger before logging
if self.inner.matches(record) {
println!("{}", self.format(record));
eprintln!("{}", self.format(record));
}
}

Expand Down
48 changes: 48 additions & 0 deletions crates/logger/src/options.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use anyhow::anyhow;
use env_logger::WriteStyle;
use structopt::StructOpt;

fn parse_write_style(spec: &str) -> anyhow::Result<WriteStyle> {
match spec {
"auto" => Ok(WriteStyle::Auto),
"always" => Ok(WriteStyle::Always),
"never" => Ok(WriteStyle::Never),
_ => Err(anyhow!("Configuration error")),
}
}

#[derive(StructOpt, Debug, Clone)]
pub struct LoggingOptions {
/// Disables logging
#[structopt(long = "quiet", short = "q")]
pub quiet: bool,

/// Outputs the version
#[structopt(long = "version", short = "v")]
pub version: bool,

/// Turns on verbose logging
#[structopt(long = "verbose", short = "V")]
pub verbose: bool,

/// Turns on debug logging
#[structopt(long = "debug")]
pub debug: bool,

/// Turns on trace logging
#[structopt(long = "trace")]
pub trace: bool,

/// Log as JSON
#[structopt(long = "json")]
pub json: bool,

/// Log style
#[structopt(
long = "log-style",
env = "VINO_LOG_STYLE",
parse(try_from_str = parse_write_style),
default_value="auto"
)]
pub log_style: WriteStyle,
}
17 changes: 17 additions & 0 deletions crates/vino-configfile/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "vino-configfile"
version = "0.1.0"
authors = ["Jarrod Overson <jsoverson@gmail.com>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
serde = { version = "1.0.126", features = ["derive"] }
config = "0.11.0"
directories-next = "2.0.0"
log = "0.4.14"
toml = "0.5.8"

[dev-dependencies]
env_logger = ""
40 changes: 40 additions & 0 deletions crates/vino-configfile/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use std::{io, path::PathBuf};

use directories_next::BaseDirs;
use serde::Deserialize;
#[derive(Debug, Deserialize, Default)]
pub struct VinoConfig {
pub cache_dir: Option<String>,
}

fn load_configfile<T: AsRef<str>>(file: T) -> VinoConfig {
let mut dir = match BaseDirs::new() {
Some(base_dirs) => base_dirs.home_dir().to_path_buf(),
None => PathBuf::new(),
};

dir.push(file.as_ref());
if let Ok(result) = std::fs::read_to_string(dir) {
let config: VinoConfig = toml::from_str(&result).unwrap_or_default();

println!("{:?}", config);
config
} else {
VinoConfig::default()
}
}

#[cfg(test)]
mod tests {

use crate::load_configfile;
fn init() {
env_logger::init();
}
#[test]
fn runs_crud_api_config() {
init();
let config = load_configfile(".vinocfg");
println!("{:?}", config);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "vino-runtime2"
name = "vino-runtime"
version = "0.1.0"
authors = ["Jarrod Overson <jsoverson@gmail.com>"]
edition = "2018"
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub use self::manifest::schematic_definition::SchematicDefinition;
use crate::dispatch::MessagePayload;
pub(crate) use native_component_actor::NativeComponentActor;

pub use crate::manifest::run_config::RunConfig;
pub use dispatch::{Invocation, InvocationResponse};
pub use serdes::{deserialize, serialize};

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 7dd8f29

Please sign in to comment.