Skip to content

Commit

Permalink
Timestamp logging and more macros
Browse files Browse the repository at this point in the history
  • Loading branch information
Stridsvagn69420 committed Dec 4, 2022
1 parent 90b5a12 commit 1ceca0a
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 18 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand All @@ -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"
Expand All @@ -53,7 +54,7 @@ required-features = []
[profile.release]
lto = true
strip = true
debug = true
debug = false

[package.metadata.docs.rs]
targets = [
Expand Down
2 changes: 1 addition & 1 deletion src/artist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<Artist>> {
let conv_paths: Vec<PathBuf> = paths.iter()
.map(|x| Path::new(x).join(".artists.json")).collect();
Expand Down
27 changes: 20 additions & 7 deletions src/bin/cyrkensia.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
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;
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
///
Expand Down Expand Up @@ -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
}

Expand All @@ -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);
}
10 changes: 4 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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;
2 changes: 1 addition & 1 deletion src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Uuid>
}

Expand Down
30 changes: 29 additions & 1 deletion src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,32 @@ impl CyrkensiaState {
accounts
})
}
}
}

/// 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;

0 comments on commit 1ceca0a

Please sign in to comment.