From f31fb2d73b2d1417b74fc6135b22b829e8834abf Mon Sep 17 00:00:00 2001 From: Aditya Mukhopadhyay Date: Fri, 7 Jul 2023 13:12:52 +0530 Subject: [PATCH] Debug via logger. --- Cargo.toml | 5 +++++ src/main.rs | 7 ++++--- src/server/logger.rs | 48 +++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 56 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 67b662b..825cf7e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,3 +5,8 @@ edition = "2021" [dependencies] graphql-parser = "0.4.0" +lazy_static = "1.4.0" + +[profile.release] +panic = "abort" + diff --git a/src/main.rs b/src/main.rs index 576a422..090d3ca 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,7 +11,7 @@ fn main() { // Get the schema file from the command line arguments or use the default. let default_schema_file = "schema.graphql".to_string(); let schema_file = &env::args().nth(1).unwrap_or(default_schema_file); - dbg!("schema_file: {}", schema_file); + logger::debug_log("schema_file", schema_file); // Read the schema file and parse it into a schema object. let schema_file = fs::read_to_string(schema_file).unwrap_or_else(|err| { @@ -31,7 +31,7 @@ fn main() { ); process::exit(error_codes::ERROR_INVALID_SCHEMA); }); - dbg!("schema: {:#?}", &schema); + logger::debug_log("schema", &schema); // Start a Read-Eval-Print-Loop (REPL) for the user to enter queries. loop { @@ -54,6 +54,7 @@ fn main() { ); continue; } - dbg!("query: {:#?}", &query); + let query = query.unwrap(); + logger::debug_log("query", &query); } } diff --git a/src/server/logger.rs b/src/server/logger.rs index 4602a33..3548d72 100644 --- a/src/server/logger.rs +++ b/src/server/logger.rs @@ -1,4 +1,39 @@ -use std::fmt::Display; +use std::env; +use std::fmt::{Debug, Display}; +use std::str::FromStr; + +use lazy_static::lazy_static; + +#[derive(PartialEq, PartialOrd)] +pub enum LogLevel { + Error, + Warn, + Info, + Debug, + Trace, +} + +impl FromStr for LogLevel { + type Err = (); + + fn from_str(s: &str) -> Result { + match s { + "Error" => Ok(LogLevel::Error), + "Warn" => Ok(LogLevel::Warn), + "Info" => Ok(LogLevel::Info), + "Debug" => Ok(LogLevel::Debug), + "Trace" => Ok(LogLevel::Trace), + _ => Err(()), + } + } +} + +lazy_static! { + static ref LOG_LEVEL: LogLevel = { + let log_level = env::var("LOG_LEVEL").unwrap_or("Info".to_string()); + log_level.parse().unwrap() + }; +} pub fn error_log(error_code: i32, message: &str, err: E) where @@ -6,3 +41,14 @@ where { eprintln!("ERROR {}: {}: {}", error_code, message, err); } + +pub fn debug_log(key: &str, object: &T) +where + T: Debug, +{ + if *LOG_LEVEL < LogLevel::Debug { + return; + } + + println!("DEBUG {}: {:#?}", key, object); +}