Start JSON debug output for context
This commit is contained in:
parent
78b5ebcd64
commit
b639d1d82b
|
@ -48,6 +48,7 @@ dependencies = [
|
||||||
"glob",
|
"glob",
|
||||||
"hostname",
|
"hostname",
|
||||||
"lazy_static",
|
"lazy_static",
|
||||||
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"structopt",
|
"structopt",
|
||||||
]
|
]
|
||||||
|
@ -166,6 +167,20 @@ name = "serde"
|
||||||
version = "1.0.106"
|
version = "1.0.106"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "36df6ac6412072f67cf767ebbde4133a5b2e88e76dc6187fa7104cd16f783399"
|
checksum = "36df6ac6412072f67cf767ebbde4133a5b2e88e76dc6187fa7104cd16f783399"
|
||||||
|
dependencies = [
|
||||||
|
"serde_derive",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde_derive"
|
||||||
|
version = "1.0.106"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9e549e3abf4fb8621bd1609f11dfc9f5e50320802273b12f3811a67e6716ea6c"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde_json"
|
name = "serde_json"
|
||||||
|
|
|
@ -14,3 +14,5 @@ hostname = "0.3"
|
||||||
# Translations
|
# Translations
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
structopt = "0.3"
|
structopt = "0.3"
|
||||||
|
# Debug Context for translations
|
||||||
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
|
|
46
src/log.rs
46
src/log.rs
|
@ -13,11 +13,11 @@ lazy_static! {
|
||||||
|
|
||||||
/// Lang configures how to deal with translations. It has three possible values:
|
/// Lang configures how to deal with translations. It has three possible values:
|
||||||
/// None: translations return their own key name
|
/// None: translations return their own key name
|
||||||
/// Replace: wtf to do with this?
|
|
||||||
/// Some(code): where code is the language code for the JSON translation file
|
/// Some(code): where code is the language code for the JSON translation file
|
||||||
|
/// JsonContext: debug output for Context as JSON
|
||||||
pub enum Lang {
|
pub enum Lang {
|
||||||
None,
|
None,
|
||||||
Replace,
|
JsonContext,
|
||||||
Some(String)
|
Some(String)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,9 +25,10 @@ impl Lang {
|
||||||
fn from_env() -> Lang {
|
fn from_env() -> Lang {
|
||||||
let lang =
|
let lang =
|
||||||
env::var("LANG").expect("$LANG not set in environment. Your machine is misconfigured!");
|
env::var("LANG").expect("$LANG not set in environment. Your machine is misconfigured!");
|
||||||
|
// TODO: do lowercase matching
|
||||||
match lang.as_str() {
|
match lang.as_str() {
|
||||||
"NONE" => Lang::None,
|
"NONE" => Lang::None,
|
||||||
"REPLACE" => Lang::Replace,
|
"JSON" => Lang::JsonContext,
|
||||||
_ => Lang::Some(lang[0..2].to_string())
|
_ => Lang::Some(lang[0..2].to_string())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -92,22 +93,27 @@ fn load_translations() -> HashMap<String, String> {
|
||||||
|
|
||||||
fn trans(key: &str) -> String {
|
fn trans(key: &str) -> String {
|
||||||
match &*LANG {
|
match &*LANG {
|
||||||
Lang::Some(lang) => match TRANSLATIONS.get(key) {
|
Lang::Some(lang) => {
|
||||||
Some(t) => t.to_string(),
|
if let Some(t) = TRANSLATIONS.get(key) {
|
||||||
None => {
|
t.to_string()
|
||||||
panic!("Unknown translation string in lang {}: {}", lang, key);
|
} else {
|
||||||
|
panic!("Missing translation for {} in lang {}", key, lang)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Lang::None => {
|
Lang::JsonContext => String::new(),
|
||||||
// Return the requested key itself (for output tests)
|
Lang::None => key.to_string(),
|
||||||
key.to_string()
|
|
||||||
},
|
|
||||||
Lang::Replace => {
|
|
||||||
unimplemented!("Don't know what to do with this yet but we need a way to test variable substitution");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn trans_context(key: &str, context: &Context) -> String {
|
||||||
|
match &*LANG {
|
||||||
|
Lang::JsonContext => {
|
||||||
|
// Serialize the context to JSON for debugging
|
||||||
|
serde_json::to_string(context).expect("Failed to serialize to JSON")
|
||||||
|
},
|
||||||
|
_ => expand(&trans(key), context)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
struct LogLevel {
|
struct LogLevel {
|
||||||
info: bool,
|
info: bool,
|
||||||
|
@ -158,28 +164,24 @@ fn expand(msg: &str, vars: &Context) -> String {
|
||||||
|
|
||||||
pub fn info(msg: &str, vars: &Context) {
|
pub fn info(msg: &str, vars: &Context) {
|
||||||
if LOGLEVEL.info {
|
if LOGLEVEL.info {
|
||||||
let t_msg = expand(&trans(msg), vars);
|
println!("[git-build] {}", trans_context(msg, vars));
|
||||||
println!("[git-build] {}", t_msg);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn error(msg: &str, vars: &Context) {
|
pub fn error(msg: &str, vars: &Context) {
|
||||||
if LOGLEVEL.error {
|
if LOGLEVEL.error {
|
||||||
let t_msg = expand(&trans(msg), vars);
|
eprintln!("{}{}", trans("error"), trans_context(msg, vars));
|
||||||
eprintln!("{}{}", trans("error"), t_msg);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn warn(msg: &str, vars: &Context) {
|
pub fn warn(msg: &str, vars: &Context) {
|
||||||
if LOGLEVEL.error {
|
if LOGLEVEL.error {
|
||||||
let t_msg = expand(&trans(msg), vars);
|
eprintln!("{}{}", trans("warning"), trans_context(msg, vars));
|
||||||
eprintln!("{}{}", trans("warning"), t_msg);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn debug(msg: &str, vars: &Context) {
|
pub fn debug(msg: &str, vars: &Context) {
|
||||||
if LOGLEVEL.debug {
|
if LOGLEVEL.debug {
|
||||||
let t_msg = expand(&trans(msg), vars);
|
println!("{}{}", trans("debug"), trans_context(msg, vars));
|
||||||
println!("{}{}", trans("debug"), t_msg);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
use std::collections::HashMap;
|
|
||||||
use std::env::{set_current_dir as cd, set_var};
|
use std::env::{set_current_dir as cd, set_var};
|
||||||
use structopt::StructOpt;
|
use structopt::StructOpt;
|
||||||
// For UNIX extended metadata
|
// For UNIX extended metadata
|
||||||
|
|
Loading…
Reference in New Issue