Skip to content

Commit

Permalink
Debug via logger.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aditya Mukhopadhyay committed Jul 7, 2023
1 parent d020fb5 commit f31fb2d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ edition = "2021"

[dependencies]
graphql-parser = "0.4.0"
lazy_static = "1.4.0"

[profile.release]
panic = "abort"

7 changes: 4 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand All @@ -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 {
Expand All @@ -54,6 +54,7 @@ fn main() {
);
continue;
}
dbg!("query: {:#?}", &query);
let query = query.unwrap();
logger::debug_log("query", &query);
}
}
48 changes: 47 additions & 1 deletion src/server/logger.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,54 @@
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<Self, Self::Err> {
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<E>(error_code: i32, message: &str, err: E)
where
E: Display,
{
eprintln!("ERROR {}: {}: {}", error_code, message, err);
}

pub fn debug_log<T>(key: &str, object: &T)
where
T: Debug,
{
if *LOG_LEVEL < LogLevel::Debug {
return;
}

println!("DEBUG {}: {:#?}", key, object);
}

0 comments on commit f31fb2d

Please sign in to comment.