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

Switched to Tracing framework for diagnostic information #211

Merged
merged 5 commits into from
Sep 29, 2023
Merged
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: 2 additions & 0 deletions dsc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ serde_json = { version = "1.0", features = ["preserve_order"] }
serde_yaml = { version = "0.9" }
syntect = { version = "5.0", features = ["default-fancy"], default-features = false }
thiserror = "1.0"
tracing = "0.1.37"
tracing-subscriber = "0.3.17"
17 changes: 11 additions & 6 deletions dsc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use atty::Stream;
use clap::Parser;
use std::io::{self, Read};
use std::process::exit;
use tracing::error;

#[cfg(debug_assertions)]
use crossterm::event;
Expand All @@ -22,8 +23,12 @@ fn main() {
#[cfg(debug_assertions)]
check_debug();

// create subscriber that writes all events to stderr
let subscriber = tracing_subscriber::fmt().pretty().with_writer(std::io::stderr).finish();
let _ = tracing::subscriber::set_global_default(subscriber).map_err(|_err| eprintln!("Unable to set global default subscriber"));

if ctrlc::set_handler(ctrlc_handler).is_err() {
eprintln!("Error: Failed to set Ctrl-C handler");
error!("Error: Failed to set Ctrl-C handler");
}

let args = Args::parse();
Expand All @@ -36,7 +41,7 @@ fn main() {
let input = match String::from_utf8(buffer) {
Ok(input) => input,
Err(e) => {
eprintln!("Invalid UTF-8 sequence: {e}");
error!("Invalid UTF-8 sequence: {e}");
exit(util::EXIT_INVALID_ARGS);
},
};
Expand All @@ -55,7 +60,7 @@ fn main() {
let json = match serde_json::to_string(&schema) {
Ok(json) => json,
Err(err) => {
eprintln!("JSON Error: {err}");
error!("JSON Error: {err}");
exit(util::EXIT_JSON_ERROR);
}
};
Expand All @@ -67,14 +72,14 @@ fn main() {
}

fn ctrlc_handler() {
eprintln!("Ctrl-C received");
error!("Ctrl-C received");
exit(util::EXIT_CTRL_C);
}

#[cfg(debug_assertions)]
fn check_debug() {
if env::var("DEBUG_DSC").is_ok() {
eprintln!("attach debugger to pid {} and press a key to continue", std::process::id());
error!("attach debugger to pid {} and press a key to continue", std::process::id());
loop {
let event = event::read().unwrap();
if let event::Event::Key(key) = event {
Expand All @@ -83,7 +88,7 @@ fn check_debug() {
break;
}
} else {
eprintln!("Unexpected event: {event:?}");
error!("Unexpected event: {event:?}");
continue;
}
}
Expand Down
57 changes: 26 additions & 31 deletions dsc/src/resource_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::util::{EXIT_DSC_ERROR, EXIT_INVALID_ARGS, EXIT_JSON_ERROR, add_type_n
use dsc_lib::configure::config_doc::Configuration;
use dsc_lib::configure::add_resource_export_results_to_configuration;
use dsc_lib::dscresources::invoke_result::GetResult;
use tracing::{error, debug};

use dsc_lib::{
dscresources::dscresource::{Invoke, DscResource},
Expand All @@ -17,40 +18,38 @@ pub fn get(dsc: &mut DscManager, resource: &str, input: &Option<String>, stdin:
// TODO: support streaming stdin which includes resource and input
let mut input = get_input(input, stdin);
let mut resource = get_resource(dsc, resource);
//TODO: add to debug stream: println!("handle_resource_get - {} implemented_as - {:?}", resource.type_name, resource.implemented_as);
debug!("resource.type_name - {} implemented_as - {:?}", resource.type_name, resource.implemented_as);
if let Some(requires) = resource.requires {
input = add_type_name_to_json(input, resource.type_name);
resource = get_resource(dsc, &requires);
}

//TODO: add to debug stream: println!("handle_resource_get - input - {}", input);

match resource.get(input.as_str()) {
Ok(result) => {
// convert to json
let json = match serde_json::to_string(&result) {
Ok(json) => json,
Err(err) => {
eprintln!("JSON Error: {err}");
error!("JSON Error: {err}");
exit(EXIT_JSON_ERROR);
}
};
write_output(&json, format);
}
Err(err) => {
eprintln!("Error: {err}");
error!("Error: {err}");
exit(EXIT_DSC_ERROR);
}
}
}

pub fn get_all(dsc: &mut DscManager, resource: &str, _input: &Option<String>, _stdin: &Option<String>, format: &Option<OutputFormat>) {
let resource = get_resource(dsc, resource);

debug!("resource.type_name - {} implemented_as - {:?}", resource.type_name, resource.implemented_as);
let export_result = match resource.export() {
Ok(export) => { export }
Err(err) => {
eprintln!("Error: {err}");
error!("Error: {err}");
exit(EXIT_DSC_ERROR);
}
};
Expand All @@ -64,7 +63,7 @@ pub fn get_all(dsc: &mut DscManager, resource: &str, _input: &Option<String>, _s
let json = match serde_json::to_string(&get_result) {
Ok(json) => json,
Err(err) => {
eprintln!("JSON Error: {err}");
error!("JSON Error: {err}");
exit(EXIT_JSON_ERROR);
}
};
Expand All @@ -75,35 +74,33 @@ pub fn get_all(dsc: &mut DscManager, resource: &str, _input: &Option<String>, _s
pub fn set(dsc: &mut DscManager, resource: &str, input: &Option<String>, stdin: &Option<String>, format: &Option<OutputFormat>) {
let mut input = get_input(input, stdin);
if input.is_empty() {
eprintln!("Error: Input is empty");
error!("Error: Input is empty");
exit(EXIT_INVALID_ARGS);
}

let mut resource = get_resource(dsc, resource);

//TODO: add to debug stream: println!("handle_resource_set - {} implemented_as - {:?}", resource.type_name, resource.implemented_as);
debug!("resource.type_name - {} implemented_as - {:?}", resource.type_name, resource.implemented_as);

if let Some(requires) = resource.requires {
input = add_type_name_to_json(input, resource.type_name);
resource = get_resource(dsc, &requires);
}

//TODO: add to debug stream: println!("handle_resource_get - input - {}", input);

match resource.set(input.as_str(), true) {
Ok(result) => {
// convert to json
let json = match serde_json::to_string(&result) {
Ok(json) => json,
Err(err) => {
eprintln!("JSON Error: {err}");
error!("JSON Error: {err}");
exit(EXIT_JSON_ERROR);
}
};
write_output(&json, format);
}
Err(err) => {
eprintln!("Error: {err}");
error!("Error: {err}");
exit(EXIT_DSC_ERROR);
}
}
Expand All @@ -113,29 +110,27 @@ pub fn test(dsc: &mut DscManager, resource: &str, input: &Option<String>, stdin:
let mut input = get_input(input, stdin);
let mut resource = get_resource(dsc, resource);

//TODO: add to debug stream: println!("handle_resource_test - {} implemented_as - {:?}", resource.type_name, resource.implemented_as);
debug!("resource.type_name - {} implemented_as - {:?}", resource.type_name, resource.implemented_as);

if let Some(requires) = resource.requires {
input = add_type_name_to_json(input, resource.type_name);
resource = get_resource(dsc, &requires);
}

//TODO: add to debug stream: println!("handle_resource_test - input - {}", input);

match resource.test(input.as_str()) {
Ok(result) => {
// convert to json
let json = match serde_json::to_string(&result) {
Ok(json) => json,
Err(err) => {
eprintln!("JSON Error: {err}");
error!("JSON Error: {err}");
exit(EXIT_JSON_ERROR);
}
};
write_output(&json, format);
}
Err(err) => {
eprintln!("Error: {err}");
error!("Error: {err}");
exit(EXIT_DSC_ERROR);
}
}
Expand All @@ -149,14 +144,14 @@ pub fn schema(dsc: &mut DscManager, resource: &str, format: &Option<OutputFormat
match serde_json::from_str::<serde_json::Value>(json.as_str()) {
Ok(_) => (),
Err(err) => {
eprintln!("Error: {err}");
error!("Error: {err}");
exit(EXIT_JSON_ERROR);
}
};
write_output(&json, format);
}
Err(err) => {
eprintln!("Error: {err}");
error!("Error: {err}");
exit(EXIT_DSC_ERROR);
}
}
Expand All @@ -170,15 +165,15 @@ pub fn export(dsc: &mut DscManager, resource: &str, format: &Option<OutputFormat
match add_resource_export_results_to_configuration(&dsc_resource, &mut conf) {
Ok(_) => (),
Err(err) => {
eprintln!("Error: {err}");
error!("Error: {err}");
exit(EXIT_DSC_ERROR);
}
}

let json = match serde_json::to_string(&conf) {
Ok(json) => json,
Err(err) => {
eprintln!("JSON Error: {err}");
error!("JSON Error: {err}");
exit(EXIT_JSON_ERROR);
}
};
Expand All @@ -191,26 +186,26 @@ pub fn get_resource(dsc: &mut DscManager, resource: &str) -> DscResource {
Ok(resource) => resource,
Err(err) => {
if resource.contains('{') {
eprintln!("Not valid resource JSON: {err}\nInput was: {resource}");
error!("Not valid resource JSON: {err}\nInput was: {resource}");
exit(EXIT_INVALID_ARGS);
}

match dsc.initialize_discovery() {
Ok(_) => (),
Err(err) => {
eprintln!("Error: {err}");
error!("Error: {err}");
exit(EXIT_DSC_ERROR);
}
};
let resources: Vec<DscResource> = dsc.find_resource(resource).collect();
match resources.len() {
0 => {
eprintln!("Error: Resource not found: '{resource}'");
error!("Error: Resource not found: '{resource}'");
exit(EXIT_INVALID_ARGS);
}
1 => resources[0].clone(),
_ => {
eprintln!("Error: Multiple resources found");
error!("Error: Multiple resources found");
exit(EXIT_INVALID_ARGS);
}
}
Expand All @@ -221,7 +216,7 @@ pub fn get_resource(dsc: &mut DscManager, resource: &str) -> DscResource {
fn get_input(input: &Option<String>, stdin: &Option<String>) -> String {
let input = match (input, stdin) {
(Some(_input), Some(_stdin)) => {
eprintln!("Error: Cannot specify both --input and stdin");
error!("Error: Cannot specify both --input and stdin");
exit(EXIT_INVALID_ARGS);
}
(Some(input), None) => input.clone(),
Expand All @@ -243,17 +238,17 @@ fn get_input(input: &Option<String>, stdin: &Option<String>) -> String {
match serde_json::to_string(&yaml) {
Ok(json) => json,
Err(err) => {
eprintln!("Error: Cannot convert YAML to JSON: {err}");
error!("Error: Cannot convert YAML to JSON: {err}");
exit(EXIT_INVALID_ARGS);
}
}
},
Err(err) => {
if input.contains('{') {
eprintln!("Error: Input is not valid JSON: {json_err}");
error!("Error: Input is not valid JSON: {json_err}");
}
else {
eprintln!("Error: Input is not valid YAML: {err}");
error!("Error: Input is not valid YAML: {err}");
}
exit(EXIT_INVALID_ARGS);
}
Expand Down
Loading