aboutsummaryrefslogtreecommitdiff
path: root/src/utils
diff options
context:
space:
mode:
authornotgne22020-12-27 10:48:18 -0700
committernotgne22020-12-27 10:52:37 -0700
commit4461d1ea3f494778f07041164f6b8a252bd26ea2 (patch)
tree85fc2196e5c6869947c5f2d92713ac2b3bde8e2b /src/utils
parent91e934fd9e1e06044f75a3e3ad998813c2a90fb5 (diff)
Replace logger, add more logging options including files
Diffstat (limited to '')
-rw-r--r--src/utils/deploy.rs20
-rw-r--r--src/utils/mod.rs95
2 files changed, 110 insertions, 5 deletions
diff --git a/src/utils/deploy.rs b/src/utils/deploy.rs
index 14a44a0..3aced6c 100644
--- a/src/utils/deploy.rs
+++ b/src/utils/deploy.rs
@@ -16,6 +16,8 @@ fn build_activate_command(
temp_path: &Cow<str>,
confirm_timeout: u16,
magic_rollback: bool,
+ debug_logs: bool,
+ log_dir: Option<&str>,
) -> String {
let mut self_activate_command = format!(
"{}/activate-rs '{}' '{}' --temp-path {} --confirm-timeout {}",
@@ -30,6 +32,14 @@ fn build_activate_command(
self_activate_command = format!("{} --auto-rollback", self_activate_command);
}
+ if debug_logs {
+ self_activate_command = format!("{} --debug-logs", self_activate_command);
+ }
+
+ if let Some(log_dir) = log_dir {
+ self_activate_command = format!("{} --log-file {}", self_activate_command, log_dir);
+ }
+
if let Some(sudo_cmd) = &sudo {
self_activate_command = format!("{} {}", sudo_cmd, self_activate_command);
}
@@ -47,6 +57,8 @@ fn test_activation_command_builder() {
let temp_path = &"/tmp".into();
let confirm_timeout = 30;
let magic_rollback = true;
+ let debug_logs = true;
+ let log_dir = Some("/tmp/something.txt");
assert_eq!(
build_activate_command(
@@ -56,9 +68,11 @@ fn test_activation_command_builder() {
auto_rollback,
temp_path,
confirm_timeout,
- magic_rollback
+ magic_rollback,
+ debug_logs,
+ log_dir
),
- "sudo -u test /nix/store/blah/etc/activate-rs '/blah/profiles/test' '/nix/store/blah/etc' --temp-path /tmp --confirm-timeout 30 --magic-rollback --auto-rollback"
+ "sudo -u test /nix/store/blah/etc/activate-rs '/blah/profiles/test' '/nix/store/blah/etc' --temp-path /tmp --confirm-timeout 30 --magic-rollback --auto-rollback --debug-logs --log-file /tmp/something.txt"
.to_string(),
);
}
@@ -107,6 +121,8 @@ pub async fn deploy_profile(
&temp_path,
confirm_timeout,
magic_rollback,
+ deploy_data.debug_logs,
+ deploy_data.log_dir,
);
debug!("Constructed activation command: {}", self_activate_command);
diff --git a/src/utils/mod.rs b/src/utils/mod.rs
index 5770f98..42c81f7 100644
--- a/src/utils/mod.rs
+++ b/src/utils/mod.rs
@@ -3,14 +3,14 @@
//
// SPDX-License-Identifier: MPL-2.0
-use rnix::{types::*, NodeOrToken, SyntaxKind::*, SyntaxNode};
-
-use std::path::PathBuf;
+use rnix::{types::*, SyntaxKind::*};
use merge::Merge;
use thiserror::Error;
+use flexi_logger::*;
+
#[macro_export]
macro_rules! good_panic {
($($tts:tt)*) => {{
@@ -19,6 +19,87 @@ macro_rules! good_panic {
}}
}
+pub fn logger_formatter_activate(
+ w: &mut dyn std::io::Write,
+ _now: &mut DeferredNow,
+ record: &Record,
+) -> Result<(), std::io::Error> {
+ let level = record.level();
+
+ write!(
+ w,
+ "⭐ REMOTE ⭐ {0} {1} {0} {2}",
+ match level {
+ log::Level::Error => "❌",
+ log::Level::Warn => "⚠️",
+ log::Level::Info => "ℹ️",
+ log::Level::Debug => "❓",
+ log::Level::Trace => "🖊️",
+ },
+ style(level, level.to_string()),
+ record.args()
+ )
+}
+
+pub fn logger_formatter_deploy(
+ w: &mut dyn std::io::Write,
+ _now: &mut DeferredNow,
+ record: &Record,
+) -> Result<(), std::io::Error> {
+ let level = record.level();
+
+ write!(
+ w,
+ "🚀 {0} {1} {0} {2}",
+ match level {
+ log::Level::Error => "❌",
+ log::Level::Warn => "⚠️",
+ log::Level::Info => "ℹ️",
+ log::Level::Debug => "❓",
+ log::Level::Trace => "🖊️",
+ },
+ style(level, level.to_string()),
+ record.args()
+ )
+}
+
+pub fn init_logger(
+ debug_logs: bool,
+ log_dir: Option<&str>,
+ activate: bool,
+) -> Result<(), FlexiLoggerError> {
+ let logger_formatter = match activate {
+ true => logger_formatter_activate,
+ false => logger_formatter_deploy,
+ };
+
+ if let Some(log_dir) = log_dir {
+ Logger::with_env_or_str("debug")
+ .log_to_file()
+ .format_for_stderr(logger_formatter)
+ .set_palette("196;208;51;7;8".to_string())
+ .directory(log_dir)
+ .discriminant(if activate { "activate" } else { "deploy" })
+ .duplicate_to_stderr(match debug_logs {
+ true => Duplicate::Debug,
+ false => Duplicate::Info,
+ })
+ .print_message()
+ .start()?;
+ } else {
+ Logger::with_env_or_str(match debug_logs {
+ true => "debug",
+ false => "info",
+ })
+ .log_target(LogTarget::StdErr)
+ .format(logger_formatter_deploy)
+ .set_palette("196;208;51;7;8".to_string())
+ .start()?;
+ }
+
+ Ok(())
+}
+
pub mod data;
pub mod deploy;
pub mod push;
@@ -191,6 +272,9 @@ pub struct DeployData<'a> {
pub cmd_overrides: &'a CmdOverrides,
pub merged_settings: data::GenericSettings,
+
+ pub debug_logs: bool,
+ pub log_dir: Option<&'a str>,
}
#[derive(Debug)]
@@ -259,6 +343,8 @@ pub fn make_deploy_data<'a, 's>(
profile: &'a data::Profile,
profile_name: &'a str,
cmd_overrides: &'a CmdOverrides,
+ debug_logs: bool,
+ log_dir: Option<&'a str>,
) -> DeployData<'a> {
let mut merged_settings = top_settings.clone();
merged_settings.merge(node.generic_settings.clone());
@@ -292,6 +378,9 @@ pub fn make_deploy_data<'a, 's>(
cmd_overrides,
merged_settings,
+
+ debug_logs,
+ log_dir,
}
}