@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
- New command `repo state` - by Awiteb
|
- New command `repo state` - by Awiteb
|
||||||
- Support pull requests - by Awiteb
|
- Support pull requests - by Awiteb
|
||||||
- Add `--personal-fork` flag to `repo announce` command - by Awiteb
|
- Add `--personal-fork` flag to `repo announce` command - by Awiteb
|
||||||
|
- Log to stderr and a file - by Awiteb
|
||||||
|
|
||||||
### Breaking Change
|
### Breaking Change
|
||||||
|
|
||||||
|
|||||||
@@ -14,7 +14,12 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with this program. If not, see <https://gnu.org/licenses/gpl-3.0.html>.
|
// along with this program. If not, see <https://gnu.org/licenses/gpl-3.0.html>.
|
||||||
|
|
||||||
use std::io::{self, Write};
|
use std::{
|
||||||
|
fs,
|
||||||
|
io::{self, Write},
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::error::{N34Error, N34Result};
|
||||||
|
|
||||||
/// Displays the given prompt and reads a line of input from the user.
|
/// Displays the given prompt and reads a line of input from the user.
|
||||||
pub fn read_line(prompt: &str) -> io::Result<String> {
|
pub fn read_line(prompt: &str) -> io::Result<String> {
|
||||||
@@ -43,3 +48,39 @@ pub fn prompt_bool(prompt: &str) -> io::Result<bool> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Opens the logs file for writing. If the file size exceeds 5MB, it is opened
|
||||||
|
/// in write mode, otherwise in append mode.
|
||||||
|
pub fn logs_file() -> N34Result<fs::File> {
|
||||||
|
const FIVE_MB: u64 = 1024 * 1024 * 5;
|
||||||
|
|
||||||
|
let logs_path = dirs::data_local_dir()
|
||||||
|
.ok_or(N34Error::CanNotFindDataPath)?
|
||||||
|
.join("n34")
|
||||||
|
.join("logs.log");
|
||||||
|
|
||||||
|
tracing::info!(path = %logs_path.display(), "Logs file");
|
||||||
|
|
||||||
|
if let Some(parent) = logs_path.parent()
|
||||||
|
&& !parent.exists()
|
||||||
|
{
|
||||||
|
fs::create_dir_all(parent)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = fs::File::create_new(&logs_path);
|
||||||
|
|
||||||
|
let is_large = if let Ok(file) = fs::File::open(&logs_path)
|
||||||
|
&& let Ok(metadata) = file.metadata()
|
||||||
|
{
|
||||||
|
metadata.len() >= FIVE_MB
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
};
|
||||||
|
|
||||||
|
fs::OpenOptions::new()
|
||||||
|
.write(true)
|
||||||
|
.append(!is_large)
|
||||||
|
.truncate(is_large)
|
||||||
|
.open(&logs_path)
|
||||||
|
.map_err(N34Error::from)
|
||||||
|
}
|
||||||
|
|||||||
@@ -55,6 +55,11 @@ pub enum N34Error {
|
|||||||
Keyring(#[from] nostr_keyring::Error),
|
Keyring(#[from] nostr_keyring::Error),
|
||||||
#[error("{0}")]
|
#[error("{0}")]
|
||||||
Config(#[from] ConfigError),
|
Config(#[from] ConfigError),
|
||||||
|
#[error(
|
||||||
|
"Could not determine the default data path: both `$XDG_CONFIG_HOME` and `$HOME` \
|
||||||
|
environment variables are missing or unset."
|
||||||
|
)]
|
||||||
|
CanNotFindDataPath,
|
||||||
#[error("No editor specified in the `EDITOR` environment variable")]
|
#[error("No editor specified in the `EDITOR` environment variable")]
|
||||||
EditorNotFound,
|
EditorNotFound,
|
||||||
#[error("The file you edited is empty. Please save your changes before exiting the editor.")]
|
#[error("The file you edited is empty. Please save your changes before exiting the editor.")]
|
||||||
|
|||||||
69
src/main.rs
69
src/main.rs
@@ -22,13 +22,14 @@ pub mod error;
|
|||||||
pub mod nostr_utils;
|
pub mod nostr_utils;
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
|
fs::File,
|
||||||
process::ExitCode,
|
process::ExitCode,
|
||||||
sync::atomic::{AtomicBool, Ordering},
|
sync::atomic::{AtomicBool, Ordering},
|
||||||
};
|
};
|
||||||
|
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use clap_verbosity_flag::Verbosity;
|
use clap_verbosity_flag::Verbosity;
|
||||||
use tracing::Level;
|
use tracing::{Level, level_filters::LevelFilter};
|
||||||
use tracing_subscriber::{Layer, filter, layer::SubscriberExt};
|
use tracing_subscriber::{Layer, filter, layer::SubscriberExt};
|
||||||
|
|
||||||
use self::cli::Cli;
|
use self::cli::Cli;
|
||||||
@@ -37,42 +38,64 @@ use self::cli::Cli;
|
|||||||
/// open.
|
/// open.
|
||||||
static EDITOR_OPEN: AtomicBool = AtomicBool::new(false);
|
static EDITOR_OPEN: AtomicBool = AtomicBool::new(false);
|
||||||
|
|
||||||
|
/// Returns the stderr log layer
|
||||||
|
fn stderr_log_layer<S>() -> impl Layer<S>
|
||||||
|
where
|
||||||
|
S: tracing::Subscriber + for<'a> tracing_subscriber::registry::LookupSpan<'a>,
|
||||||
|
{
|
||||||
|
tracing_subscriber::fmt::layer()
|
||||||
|
.with_ansi(true)
|
||||||
|
.with_writer(std::io::stderr)
|
||||||
|
.without_time()
|
||||||
|
}
|
||||||
|
|
||||||
/// Configures the logging level based on the provided verbosity.
|
/// Configures the logging level based on the provided verbosity.
|
||||||
///
|
///
|
||||||
/// When verbosity is set to TRACE, includes file and line numbers in logs.
|
/// When verbosity is set to TRACE, includes file and line numbers in logs.
|
||||||
fn set_log_level(verbosity: Verbosity) {
|
fn set_log_level(verbosity: Verbosity, logs_file: File) {
|
||||||
let is_trace = verbosity
|
let editor_filter = filter::dynamic_filter_fn(move |m, _| {
|
||||||
.tracing_level()
|
|
||||||
.is_some_and(|l| l == tracing::Level::TRACE);
|
|
||||||
|
|
||||||
let logs_filter = filter::dynamic_filter_fn(move |m, _| {
|
|
||||||
// Disable all logs while editor is open
|
// Disable all logs while editor is open
|
||||||
verbosity.tracing_level().unwrap_or(Level::ERROR) >= *m.level()
|
verbosity.tracing_level().unwrap_or(Level::ERROR) >= *m.level()
|
||||||
|
&& (m.name().starts_with("event src") || m.name().contains("nostr"))
|
||||||
&& !EDITOR_OPEN.load(Ordering::Relaxed)
|
&& !EDITOR_OPEN.load(Ordering::Relaxed)
|
||||||
});
|
});
|
||||||
|
|
||||||
let logs_layer = tracing_subscriber::fmt::layer()
|
let file_layer = tracing_subscriber::fmt::layer()
|
||||||
.with_file(is_trace)
|
.with_ansi(false)
|
||||||
.with_line_number(is_trace)
|
.with_writer(logs_file)
|
||||||
.without_time();
|
.with_file(true)
|
||||||
let subscriber = tracing_subscriber::registry().with(logs_layer.with_filter(logs_filter));
|
.with_line_number(true)
|
||||||
|
.with_filter(LevelFilter::TRACE);
|
||||||
|
|
||||||
|
let subscriber = tracing_subscriber::registry()
|
||||||
|
.with(stderr_log_layer().with_filter(editor_filter))
|
||||||
|
.with(file_layer);
|
||||||
tracing::subscriber::set_global_default(subscriber).ok();
|
tracing::subscriber::set_global_default(subscriber).ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn try_main() -> error::N34Result<()> {
|
||||||
|
// Initialize a thread-local subscriber for logging during CLI parsing and
|
||||||
|
// post-processing.
|
||||||
|
let guard =
|
||||||
|
tracing::subscriber::set_default(tracing_subscriber::registry().with(stderr_log_layer()));
|
||||||
|
|
||||||
|
let cli = cli::post_cli(Cli::parse())?;
|
||||||
|
let logs_file = cli::utils::logs_file()?;
|
||||||
|
|
||||||
|
// Replace the thread-local subscriber with a global default subscriber based on
|
||||||
|
// the CLI verbosity level.
|
||||||
|
drop(guard);
|
||||||
|
set_log_level(cli.verbosity, logs_file);
|
||||||
|
|
||||||
|
cli.run().await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> ExitCode {
|
async fn main() -> ExitCode {
|
||||||
let cli = match cli::post_cli(Cli::parse()) {
|
if let Err(err) = try_main().await {
|
||||||
Ok(cli) => cli,
|
|
||||||
Err(err) => {
|
|
||||||
eprintln!("{err}");
|
eprintln!("{err}");
|
||||||
return ExitCode::FAILURE;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
set_log_level(cli.verbosity);
|
|
||||||
|
|
||||||
if let Err(err) = cli.run().await {
|
|
||||||
tracing::error!("{err}");
|
|
||||||
return err.exit_code();
|
return err.exit_code();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user