Skip to content

Commit

Permalink
feat: add log to file capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
Geobert Quach committed Oct 20, 2022
1 parent 7134cb3 commit 19ce7e7
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 24 deletions.
14 changes: 13 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "emile"
version = "0.3.5"
version = "0.3.6"
authors = ["Geobert Quach <geobert@protonmail.com>"]
edition = "2021"

Expand All @@ -20,6 +20,7 @@ tracing = "0.1.37"
lazy_static = "1.4.0"
tokio = { version = "1.21.2", features = ["sync", "macros", "rt-multi-thread", "time"] }
notify-debouncer-mini = { version = "0.2.1", default-features = false }
tracing-appender = "0.2.2"

[dependencies.tracing-subscriber]
version = "0.3.16"
Expand Down
62 changes: 42 additions & 20 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,57 @@ mod publish;
mod scheduler;
mod watcher;

use opt::Opt;
use opt::{Commands, Opt};
use time::{macros::format_description, OffsetDateTime};
use tracing_subscriber::{
fmt::time::UtcTime, prelude::__tracing_subscriber_SubscriberExt, util::SubscriberInitExt,
EnvFilter,
};
use tracing::error;
use tracing_subscriber::{fmt::time::UtcTime, prelude::*, util::SubscriberInitExt, EnvFilter};
use watcher::SiteWatcher;

#[tokio::main]
async fn main() -> Result<()> {
tracing_subscriber::registry()
.with(
tracing_subscriber::fmt::layer()
.compact()
.with_timer(UtcTime::new(format_description!(
"[year repr:last_two]-[month]-[day] [hour]:[minute]:[second]"
)))
.with_target(false),
)
.with(EnvFilter::try_from_env("EMILE_LOG").or_else(|_| EnvFilter::try_new("info"))?)
.init();
let opt = Opt::parse();
// log setup
let _guard = if let Some(log_dir) = opt.log_dir {
if !log_dir.is_dir() {
error!("{} is not a valid directory", log_dir.to_string_lossy());
bail!("Invalid log dir");
}
let file_appender = tracing_appender::rolling::never(log_dir, "emile.log");
let (non_blocking, guard) = tracing_appender::non_blocking(file_appender);
tracing_subscriber::registry()
.with(
tracing_subscriber::fmt::layer()
.compact()
.with_timer(UtcTime::new(format_description!(
"[year repr:last_two]-[month]-[day] [hour]:[minute]:[second]"
)))
.with_writer(non_blocking)
.with_target(false),
)
.with(EnvFilter::try_from_env("EMILE_LOG").or_else(|_| EnvFilter::try_new("info"))?)
.init();
Some(guard)
} else {
tracing_subscriber::registry()
.with(
tracing_subscriber::fmt::layer()
.compact()
.with_timer(UtcTime::new(format_description!(
"[year repr:last_two]-[month]-[day] [hour]:[minute]:[second]"
)))
.with_target(false),
)
.with(EnvFilter::try_from_env("EMILE_LOG").or_else(|_| EnvFilter::try_new("info"))?)
.init();
None
};

match opt {
Opt::New { title } => {
match opt.command {
Commands::New { title } => {
let cfg = SiteConfigBuilder::get_config();
new::create_draft(&title, &cfg)
}
Opt::Publish { slug } => {
Commands::Publish { slug } => {
let cfg = SiteConfigBuilder::get_config();
let dest = publish::publish_post(&slug, &cfg.drafts_creation_dir, &cfg)?;
println!(
Expand All @@ -49,7 +71,7 @@ async fn main() -> Result<()> {
);
Ok(())
}
Opt::Watch { website } => {
Commands::Watch { website } => {
std::env::set_current_dir(website)?;
let cfg = Arc::new(SiteConfigBuilder::get_config());
tracing::debug!("{:?}", cfg);
Expand Down
13 changes: 11 additions & 2 deletions src/opt.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
use std::path::PathBuf;

use clap::Parser;
use clap::{Parser, Subcommand};

/// A workflow companion for zola (https://getzola.org)
#[derive(Debug, Parser)]
#[command(about, version)]
pub enum Opt {
pub struct Opt {
/// Log directory
#[arg(short, long, value_name = "DIR")]
pub log_dir: Option<PathBuf>,
#[command(subcommand)]
pub command: Commands,
}

#[derive(Subcommand, Debug)]
pub enum Commands {
/// Create a new post in drafts folder, with current date prefiled in the frontmatter.
/// The date can be modified with the `drafts_year_shift` configuration key
New {
Expand Down

0 comments on commit 19ce7e7

Please sign in to comment.