From 1ceca0a3fba034c532ad877ee9ff8e70e9c0ab85 Mon Sep 17 00:00:00 2001 From: Stridsvagn69420 Date: Sun, 4 Dec 2022 14:34:01 +0100 Subject: [PATCH] Timestamp logging and more macros --- Cargo.toml | 5 +++-- src/artist.rs | 2 +- src/bin/cyrkensia.rs | 27 ++++++++++++++++++++------- src/lib.rs | 10 ++++------ src/metadata.rs | 2 +- src/server/mod.rs | 30 +++++++++++++++++++++++++++++- 6 files changed, 58 insertions(+), 18 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2008932..f0cd0eb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,6 +27,7 @@ kagero = { version = "0.4", default-features = false, features = ["printer"] } actix-web = { version = "4", optional = true } actix-cors = { version = "0.6", optional = true } blake3 = { version = "1.3", optional = true } +chrono = { version = "*", optional = true, features = ["std", "clock"] } # Accounts only argon2 = { version = "0.4", optional = true, features = ["std", "password-hash"] } @@ -35,7 +36,7 @@ rand = { version = "0.8", optional = true, features = ["std"] } [features] default = ["server", "accounts"] accounts = ["argon2", "rand"] -server = ["actix-web", "actix-cors", "blake3"] +server = ["actix-web", "actix-cors", "blake3", "chrono"] [build-dependencies] chrono = "0.4" @@ -53,7 +54,7 @@ required-features = [] [profile.release] lto = true strip = true -debug = true +debug = false [package.metadata.docs.rs] targets = [ diff --git a/src/artist.rs b/src/artist.rs index ddf10d9..6856dc9 100644 --- a/src/artist.rs +++ b/src/artist.rs @@ -61,7 +61,7 @@ impl Artist { /// Read multiple folders' .artists.json /// - /// Reads the .artists.json of multiple folders. Essentially like [load_multiple_artists], but with `.artists.json` appended. + /// Reads the .artists.json of multiple folders. Essentially like `load_multiple_artists`, but with `.artists.json` appended. pub fn read_multiple(paths: &[String]) -> io::Result> { let conv_paths: Vec = paths.iter() .map(|x| Path::new(x).join(".artists.json")).collect(); diff --git a/src/bin/cyrkensia.rs b/src/bin/cyrkensia.rs index 5e07ec5..d6e7c13 100644 --- a/src/bin/cyrkensia.rs +++ b/src/bin/cyrkensia.rs @@ -1,6 +1,6 @@ use std::{env, io}; use std::process::exit; -use cyrkensia::Config; +use cyrkensia::{Config, timelog, timestamp}; use cyrkensia::meta::WIKI_HELP_URL; use cyrkensia::server::CyrkensiaState; use cyrkensia::server::redirect::trail_slash; @@ -8,6 +8,7 @@ use cyrkensia::server::routes::{index, hostinfo}; use cyrkensia::server::middleware::{cors_everywhere, source_headers, license_headers}; use actix_web::{web, App, HttpServer}; use kagero::printer::{Printer, Colors}; +use chrono::Local; /// Init /// @@ -67,7 +68,8 @@ async fn server(cfg: Config) -> io::Result<()> { let server = unbound_server.bind(bindaddr)?; // ---- Ignite ---- - printer.println("Cyrkensia server successfully started!", Colors::CyanBright); + printer.print(timelog!(), Colors::Cyan) + .println("Cyrkensia server successfully started!", Colors::CyanBright); server.run().await } @@ -77,17 +79,28 @@ async fn main() { let mut console = Printer::default(); let Ok(config) = init() else { console.errorln("Failed to read the config file for Cyrkensia!", Colors::RedBright); - console.errorln(&("See ".to_owned() + WIKI_HELP_URL + " for more."), Colors::YellowBright); + morehelp(&mut console); exit(1); }; // Start - if let Err(serv) = server(config).await { - console.errorln("An error occured while running the server:", Colors::RedBright); - eprintln!("{serv}"); + if let Err(segv) = server(config).await { + console.error(timelog!(), Colors::Red) + .errorln("An error occured while running the server:", Colors::RedBright); + eprintln!("{segv}"); + morehelp(&mut console); + exit(1); } // Exit - console.println("Cyrkensia server successfully stopped!", Colors::CyanBright); + console.print(timelog!(), Colors::Cyan) + .println("Cyrkensia server successfully stopped!", Colors::CyanBright); exit(0) +} + +/// More help +/// +/// Tells you to google the error. +fn morehelp(cmd: &mut Printer) { + cmd.errorln(&("See ".to_owned() + WIKI_HELP_URL + " for more."), Colors::YellowBright); } \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 2accb82..aced3a6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,10 +12,10 @@ //! If you're searching for Cyrkensia as a binary, see the [crates.io](https://crates.io/crates/cyrkensia) or [GitHub](https://github.com/Stridsvagn69420/Cyrkensia) page for more. //! //! ## Features -//! By default, the `server` feature is enabled. The latter is only relevant for people who write a custom Cyrkensia server. +//! By default, the `accounts` and `server` features are enabled. The latter is only relevant for people who write a custom Cyrkensia server. //! You can disable `server` with this: //! ```toml -//! cyrkensia = { version = "1", default-features = false } +//! cyrkensia = { version = "1", default-features = false, features = ["accounts"] } //! ``` //! //! ## Examples @@ -30,8 +30,6 @@ //! let artists = Artist::read_multiple(&config.root).unwrap(); //! // Generate the corresponding Hostinfo //! let mut hostinfo = Hostinfo::generate(&config, &artists).unwrap(); -//! // Optionally set the origin -//! hostinfo.set_origin("https://foo.bar/my-hostinfo.json".to_string()); //! ``` /// Owner struct @@ -92,5 +90,5 @@ pub mod server; #[cfg(feature = "accounts")] /// Account database /// -/// Submodule containing the [Account](accounts::Account) struct and related cryptographic and wrapper functions. -pub mod account; +/// Submodule containing the [Account](account::Account) struct and related cryptographic and wrapper functions. +pub mod account; \ No newline at end of file diff --git a/src/metadata.rs b/src/metadata.rs index c57429c..d0223f1 100644 --- a/src/metadata.rs +++ b/src/metadata.rs @@ -26,7 +26,7 @@ pub struct Metadata { /// Artists /// - /// Represents the UUIDv4 of the [Artist] of this album + /// Represents the UUIDv4 of the [Artist](super::Artist) of this album pub artists: Vec } diff --git a/src/server/mod.rs b/src/server/mod.rs index 5510c4f..e176360 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -111,4 +111,32 @@ impl CyrkensiaState { accounts }) } -} \ No newline at end of file +} + +/// Timestamp +/// +/// Creates the current timestamp formatted as `[%Y-%m-%d %H:%M:%S]`. +/// Requires [chrono::Local]. +/// Evaluates to `Local::now().format("[%Y-%m-%d %H:%M:%S]").to_string()`. +#[macro_export] +macro_rules! timestamp { + () => { + Local::now().format("[%Y-%m-%d %H:%M:%S]").to_string() + }; +} +pub use timestamp; + +/// Timelog +/// +/// Formats a given message with a timestamp. +/// If no message is provided, it just return [timestamp!], but with a whitespace added. +#[macro_export] +macro_rules! timelog { + () => { + (timestamp!() + " ").as_str() + }; + ($msg:expr) => { + (timestamp!() + " " + $msg).as_str() + }; +} +pub use timelog; \ No newline at end of file