use tracing to print messages

This commit is contained in:
Kaan Barmore-Genç 2022-04-04 00:34:37 -04:00
parent 74f131f4be
commit cbaac7210e
3 changed files with 45 additions and 15 deletions

View File

@ -49,12 +49,12 @@ pub fn load_config(opts: &opts::Opts) -> anyhow::Result<Config> {
.ok_or(anyhow::anyhow!("Can't find config directory")); .ok_or(anyhow::anyhow!("Can't find config directory"));
confpath confpath
.and_then(|path| { .and_then(|path| {
println!("Checking for config: {}", path.to_string_lossy()); tracing::debug!("Checking for config: {}", path.to_string_lossy());
load_config_from(path) load_config_from(path)
}) })
.or_else(|_| { .or_else(|_| {
let path = PathBuf::from(".").join("gandi.toml"); let path = PathBuf::from(".").join("gandi.toml");
println!("Checking for config: {}", path.to_string_lossy()); tracing::debug!("Checking for config: {}", path.to_string_lossy());
load_config_from(path) load_config_from(path)
}) })
} }

View File

@ -2,15 +2,16 @@ use crate::config::Config;
use anyhow; use anyhow;
use clap::Parser; use clap::Parser;
use futures; use futures;
use opts::SilenceLevel;
use reqwest::{header, Client, ClientBuilder, StatusCode}; use reqwest::{header, Client, ClientBuilder, StatusCode};
use std::{collections::HashMap, num::NonZeroU32, sync::Arc, time::Duration}; use std::{collections::HashMap, num::NonZeroU32, sync::Arc, time::Duration};
use tokio::{self, task::JoinHandle}; use tokio::{self, task::JoinHandle};
use tracing::metadata::LevelFilter;
mod config; mod config;
mod opts; mod opts;
use die_exit::*; use die_exit::*;
use governor; use governor;
/// 30 requests per minute, see https://api.gandi.net/docs/reference/ /// 30 requests per minute, see https://api.gandi.net/docs/reference/
const GANDI_RATE_LIMIT: u32 = 30; const GANDI_RATE_LIMIT: u32 = 30;
/// If we hit the rate limit, wait up to this many seconds before next attempt /// If we hit the rate limit, wait up to this many seconds before next attempt
@ -43,30 +44,55 @@ async fn get_ip(api_url: &str) -> anyhow::Result<String> {
Ok(text) Ok(text)
} }
/// Sets up the logging based on the command line options given.
///
/// As a reminder, the use the following level should be used when printing
/// output:
/// - error: Error messages
/// - info: Regular operational messages
/// - debug: Any messages that contain private information, like domain names
///
fn setup_logging(level: Option<SilenceLevel>) {
tracing_subscriber::fmt()
.with_level(false)
.with_target(false)
.with_thread_ids(false)
.with_thread_names(false)
.with_max_level(match level {
Some(SilenceLevel::All) => LevelFilter::WARN,
Some(SilenceLevel::Domains) => LevelFilter::INFO,
None => LevelFilter::DEBUG,
})
.init();
}
#[tokio::main] #[tokio::main]
async fn main() -> anyhow::Result<()> { async fn main() -> anyhow::Result<()> {
let opts = opts::Opts::parse(); let opts = opts::Opts::parse();
setup_logging(opts.silent);
// setup_logging needs to come first, before anything else
let conf = config::load_config(&opts) let conf = config::load_config(&opts)
.die_with(|error| format!("Failed to read config file: {}", error)); .die_with(|error| format!("Failed to read config file: {}", error));
config::validate_config(&conf).die_with(|error| format!("Invalid config: {}", error)); config::validate_config(&conf).die_with(|error| format!("Invalid config: {}", error));
println!("Finding out the IP address..."); tracing::info!("Finding out the IP address...");
let ipv4_result = get_ip("https://api.ipify.org").await; let ipv4_result = get_ip("https://api.ipify.org").await;
let ipv6_result = get_ip("https://api6.ipify.org").await; let ipv6_result = get_ip("https://api6.ipify.org").await;
let ipv4 = ipv4_result.as_ref(); let ipv4 = ipv4_result.as_ref();
let ipv6 = ipv6_result.as_ref(); let ipv6 = ipv6_result.as_ref();
println!("Found these:"); tracing::debug!("Found these:");
match ipv4 { match ipv4 {
Ok(ip) => println!("\tIPv4: {}", ip), Ok(ip) => tracing::debug!("\tIPv4: {}", ip),
Err(err) => eprintln!("\tIPv4 failed: {}", err), Err(err) => tracing::error!("\tIPv4 failed: {}", err),
} }
match ipv6 { match ipv6 {
Ok(ip) => println!("\tIPv6: {}", ip), Ok(ip) => tracing::debug!("\tIPv6: {}", ip),
Err(err) => eprintln!("\tIPv6 failed: {}", err), Err(err) => tracing::error!("\tIPv6 failed: {}", err),
} }
let client = api_client(&conf.api_key)?; let client = api_client(&conf.api_key)?;
let mut tasks: Vec<JoinHandle<(StatusCode, String)>> = Vec::new(); let mut tasks: Vec<JoinHandle<(StatusCode, String)>> = Vec::new();
println!("Attempting to update DNS entries now"); tracing::info!("Attempting to update DNS entries now");
let governor = Arc::new(governor::RateLimiter::direct(governor::Quota::per_minute( let governor = Arc::new(governor::RateLimiter::direct(governor::Quota::per_minute(
NonZeroU32::new(GANDI_RATE_LIMIT).die("Governor rate is 0"), NonZeroU32::new(GANDI_RATE_LIMIT).die("Governor rate is 0"),
@ -89,7 +115,7 @@ async fn main() -> anyhow::Result<()> {
let task_governor = governor.clone(); let task_governor = governor.clone();
let task = tokio::task::spawn(async move { let task = tokio::task::spawn(async move {
task_governor.until_ready_with_jitter(retry_jitter).await; task_governor.until_ready_with_jitter(retry_jitter).await;
println!("Updating {}", &fqdn); tracing::debug!("Updating {}", &fqdn);
match req.send().await { match req.send().await {
Ok(response) => ( Ok(response) => (
response.status(), response.status(),
@ -106,10 +132,11 @@ async fn main() -> anyhow::Result<()> {
} }
let results = futures::future::try_join_all(tasks).await?; let results = futures::future::try_join_all(tasks).await?;
println!("Updates done for {} entries", results.len()); tracing::info!("Updates done for {} entries", results.len());
for (status, body) in results { results
println!("{} - {}", status, body); .into_iter()
} .filter(|(status, _)| !StatusCode::is_success(&status))
.for_each(|(status, body)| tracing::warn!("Error {}: {}", status, body));
return Ok(()); return Ok(());
} }

View File

@ -50,6 +50,9 @@ pub struct Opts {
#[clap(long)] #[clap(long)]
pub config: Option<String>, pub config: Option<String>,
/// Limit how much information gets printed out. Set to `all` to disable all
/// output (other than errors), or `domains` to disable printing the domain
/// names that were updated.
#[clap(long)] #[clap(long)]
pub silent: Option<SilenceLevel>, pub silent: Option<SilenceLevel>,
} }