diff --git a/common/src/log.rs b/common/src/log.rs index 5c6157cac..d2df4e3c1 100644 --- a/common/src/log.rs +++ b/common/src/log.rs @@ -54,8 +54,15 @@ pub struct LogItem { pub line: String, } +const LOGLINE_MAX_CHARS: usize = 2048; +const TRUNC_MSG: &str = "... (truncated)"; + impl LogItem { - pub fn new(id: Uuid, internal_origin: Backend, line: String) -> Self { + pub fn new(id: Uuid, internal_origin: Backend, line: impl Into) -> Self { + let mut line: String = line.into(); + + Self::truncate_line(&mut line); + Self { id, internal_origin, @@ -63,6 +70,26 @@ impl LogItem { line, } } + + fn truncate_line(line: &mut String) { + // Check if it can be over the limit (assuming ascii only), no iteration + if line.len() > LOGLINE_MAX_CHARS { + // Then, check if it actually is over the limit. + // Find the char boundrary of the last char, but iterate no more than + // the max number of chars allowed. + let x = line + .char_indices() + .enumerate() + .find(|(i, _)| *i == LOGLINE_MAX_CHARS); + // If char iterator reached max iteration count + if let Some((_, (ci, _))) = x { + // Truncate to the char boundrary found + line.truncate(ci); + // New allocation unlikely since it keeps its capacity + write!(line, "{}", TRUNC_MSG).expect("write to string"); + } + } + } } #[cfg(feature = "display")] @@ -282,4 +309,30 @@ mod tests { assert!(log_line.contains(&utc_dt)); }); } + + #[test] + fn log_item_truncate() { + let mut l = "ΓΆl".repeat(100); + LogItem::truncate_line(&mut l); + assert_eq!(l.len(), 300); + assert_eq!(l.chars().count(), 200); + + let mut l = "πŸͺ".repeat(LOGLINE_MAX_CHARS); + LogItem::truncate_line(&mut l); + assert_eq!(l.len(), 4 * (LOGLINE_MAX_CHARS)); + assert_eq!(l.chars().count(), LOGLINE_MAX_CHARS); + + // one cookie should be truncated, and the suffix message should be appended + // ✨ = 3b, πŸͺ = 4b + + let mut l = format!("A{}", "πŸͺ".repeat(LOGLINE_MAX_CHARS)); + LogItem::truncate_line(&mut l); + assert_eq!(l.len(), 1 + 4 * (LOGLINE_MAX_CHARS - 1) + TRUNC_MSG.len()); + assert_eq!(l.chars().count(), LOGLINE_MAX_CHARS + TRUNC_MSG.len()); + + let mut l = format!("✨{}", "πŸͺ".repeat(LOGLINE_MAX_CHARS)); + LogItem::truncate_line(&mut l); + assert_eq!(l.len(), 3 + 4 * (LOGLINE_MAX_CHARS - 1) + TRUNC_MSG.len()); + assert_eq!(l.chars().count(), LOGLINE_MAX_CHARS + TRUNC_MSG.len()); + } } diff --git a/deployer/src/handlers/mod.rs b/deployer/src/handlers/mod.rs index 77aef5b50..449acefbd 100644 --- a/deployer/src/handlers/mod.rs +++ b/deployer/src/handlers/mod.rs @@ -354,6 +354,7 @@ pub async fn create_service( let pid = persistence.project_id(); span.in_scope(|| { + info!("Deployer version: {}", crate::VERSION); info!("Deployment ID: {}", id); info!("Service ID: {}", service.id); info!("Service name: {}", service.name); diff --git a/deployer/src/lib.rs b/deployer/src/lib.rs index 0e66d9346..fb88fafe6 100644 --- a/deployer/src/lib.rs +++ b/deployer/src/lib.rs @@ -26,6 +26,8 @@ pub use crate::persistence::Persistence; use crate::proxy::AddressGetter; pub use crate::runtime_manager::RuntimeManager; +const VERSION: &str = env!("CARGO_PKG_VERSION"); + pub async fn start( persistence: Persistence, runtime_manager: Arc>, diff --git a/runtime/src/alpha/mod.rs b/runtime/src/alpha/mod.rs index 87027941b..ad3bcdcf6 100644 --- a/runtime/src/alpha/mod.rs +++ b/runtime/src/alpha/mod.rs @@ -64,6 +64,11 @@ pub async fn start(loader: impl Loader + Send + 'static) { } }; + println!( + "shuttle-runtime executable started (version {})", + crate::VERSION + ); + // this is handled after arg parsing to not interfere with --version above #[cfg(feature = "setup-tracing")] { @@ -340,7 +345,6 @@ where &self, request: Request, ) -> Result, Status> { - println!("alpha runtime starting"); let service = self.service.lock().unwrap().deref_mut().take(); let service = service.unwrap(); @@ -349,7 +353,7 @@ where .context("invalid socket address") .map_err(|err| Status::invalid_argument(err.to_string()))?; - println!("starting on {service_address}"); + println!("Starting on {service_address}"); let (kill_tx, kill_rx) = tokio::sync::oneshot::channel(); *self.kill_tx.lock().unwrap() = Some(kill_tx); diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 77d69be5d..1b0c32413 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -241,10 +241,10 @@ pub use strfmt::strfmt; #[cfg(feature = "setup-tracing")] pub use {colored, tracing_subscriber}; +const NAME: &str = env!("CARGO_PKG_NAME"); +const VERSION: &str = env!("CARGO_PKG_VERSION"); + // Print the version of the runtime. pub fn print_version() { - let name = env!("CARGO_PKG_NAME"); - let version = env!("CARGO_PKG_VERSION"); - - println!("{name} {version}"); + println!("{NAME} {VERSION}"); }