mirror of
https://github.com/velopack/velopack.git
synced 2025-10-25 15:19:22 +00:00
Add process name/pid to file logs
This commit is contained in:
1
src/Rust/Cargo.lock
generated
1
src/Rust/Cargo.lock
generated
@@ -1606,6 +1606,7 @@ dependencies = [
|
||||
"simplelog",
|
||||
"strum",
|
||||
"tempfile",
|
||||
"time 0.3.34",
|
||||
"ureq",
|
||||
"url",
|
||||
"wait-timeout",
|
||||
|
||||
@@ -77,6 +77,7 @@ sha1_smol = "1.0"
|
||||
url = "2.5"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
time = "0.3"
|
||||
|
||||
[target.'cfg(unix)'.dependencies]
|
||||
native-dialog = "0.7"
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
use anyhow::Result;
|
||||
use simplelog::*;
|
||||
use std::path::PathBuf;
|
||||
use time::format_description::{modifier, Component, FormatItem};
|
||||
|
||||
pub fn trace_logger() {
|
||||
TermLogger::init(LevelFilter::Trace, get_config(), TerminalMode::Mixed, ColorChoice::Never).unwrap();
|
||||
TermLogger::init(LevelFilter::Trace, get_config(None), TerminalMode::Mixed, ColorChoice::Never).unwrap();
|
||||
}
|
||||
|
||||
pub fn default_log_location() -> PathBuf {
|
||||
@@ -28,12 +29,12 @@ pub fn default_log_location() -> PathBuf {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn setup_logging(file: Option<&PathBuf>, console: bool, verbose: bool, nocolor: bool) -> Result<()> {
|
||||
pub fn setup_logging(process_name: &str, file: Option<&PathBuf>, console: bool, verbose: bool, nocolor: bool) -> Result<()> {
|
||||
let mut loggers: Vec<Box<dyn SharedLogger>> = Vec::new();
|
||||
let color_choice = if nocolor { ColorChoice::Never } else { ColorChoice::Auto };
|
||||
if console {
|
||||
let console_level = if verbose { LevelFilter::Debug } else { LevelFilter::Info };
|
||||
loggers.push(TermLogger::new(console_level, get_config(), TerminalMode::Mixed, color_choice));
|
||||
loggers.push(TermLogger::new(console_level, get_config(None), TerminalMode::Mixed, color_choice));
|
||||
}
|
||||
|
||||
if let Some(f) = file {
|
||||
@@ -46,16 +47,34 @@ pub fn setup_logging(file: Option<&PathBuf>, console: bool, verbose: bool, nocol
|
||||
#[cfg(unix)]
|
||||
None,
|
||||
);
|
||||
loggers.push(WriteLogger::new(file_level, get_config(), writer));
|
||||
loggers.push(WriteLogger::new(file_level, get_config(Some(process_name)), writer));
|
||||
}
|
||||
|
||||
CombinedLogger::init(loggers)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
fn get_config() -> Config {
|
||||
fn get_config(process_name: Option<&str>) -> Config {
|
||||
let mut c = ConfigBuilder::default();
|
||||
let mut prefix = "".to_owned();
|
||||
if let Some(pn) = process_name {
|
||||
prefix = format!("[{}:{}] ", pn, std::process::id());
|
||||
}
|
||||
|
||||
let prefix_heaped = Box::leak(prefix.into_boxed_str());
|
||||
|
||||
let time_format: &'static [FormatItem<'static>] = Box::leak(Box::new([
|
||||
FormatItem::Literal(prefix_heaped.as_bytes()),
|
||||
FormatItem::Literal(b"["),
|
||||
FormatItem::Component(Component::Hour(modifier::Hour::default())),
|
||||
FormatItem::Literal(b":"),
|
||||
FormatItem::Component(Component::Minute(modifier::Minute::default())),
|
||||
FormatItem::Literal(b":"),
|
||||
FormatItem::Component(Component::Second(modifier::Second::default())),
|
||||
FormatItem::Literal(b"]"),
|
||||
]));
|
||||
|
||||
c.set_time_format_custom(time_format);
|
||||
let _ = c.set_time_offset_to_local(); // might fail if local tz can't be determined
|
||||
c.build()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ fn main() -> Result<()> {
|
||||
let nocolor = matches.get_flag("nocolor");
|
||||
|
||||
shared::dialogs::set_silent(silent);
|
||||
logging::setup_logging(logfile, true, verbose, nocolor)?;
|
||||
logging::setup_logging("setup", logfile, true, verbose, nocolor)?;
|
||||
|
||||
info!("Starting Velopack Setup ({})", env!("NGBV_VERSION"));
|
||||
info!(" Location: {:?}", std::env::current_exe()?);
|
||||
|
||||
@@ -21,7 +21,7 @@ fn main() -> ExitCode {
|
||||
my_dir.join("Velopack.log")
|
||||
};
|
||||
|
||||
let _ = logging::setup_logging(Some(&default_log_file), false, false, false);
|
||||
let _ = logging::setup_logging("stub", Some(&default_log_file), false, false, false);
|
||||
|
||||
info!("--");
|
||||
info!("Starting Velopack Stub (at {:?})", my_path);
|
||||
|
||||
@@ -107,10 +107,10 @@ fn main() -> Result<()> {
|
||||
|
||||
dialogs::set_silent(silent);
|
||||
if let Some(log_file) = log_file {
|
||||
logging::setup_logging(Some(&log_file), true, verbose, nocolor)?;
|
||||
logging::setup_logging("update", Some(&log_file), true, verbose, nocolor)?;
|
||||
} else {
|
||||
let default_log_file = logging::default_log_location();
|
||||
logging::setup_logging(Some(&default_log_file), true, verbose, nocolor)?;
|
||||
logging::setup_logging("update", Some(&default_log_file), true, verbose, nocolor)?;
|
||||
}
|
||||
|
||||
// change working directory to the parent directory of the exe
|
||||
|
||||
Reference in New Issue
Block a user