1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
//
// Copyright (c) {{ "today" | date: "%Y" }} {{ authors }}
//
// Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
use anyhow::Result;
use clap::{ArgAction, Parser};
{% if project-diagnosis == "log" -%}
use log::{debug, info};
{% else -%}
use tracing::{debug, info};
use tracing_subscriber::EnvFilter;
{% endif %}
const VERSION: &str = concat!(
env!("CARGO_PKG_VERSION"),
" (",
env!("GIT_HASH", "unknown"),
" ",
env!("BUILD_DATE", "unknown"),
")",
);
#[derive(Debug, Parser)]
#[command(
name = "{{project-name}}",
about = "{{project-description}}",
version = VERSION,
author,
propagate_version = true
)]
struct Args {
/// Increase verbosity (use -v, -vv, ...).
///
/// When no RUST_LOG is set, a single -v switches the log level to DEBUG.
#[arg(short, long, global = true, action = ArgAction::Count)]
verbose: u8,
}
fn main() -> Result<()> {
let args = Args::parse();
init_logger(args.verbose);
info!("hello world cli");
debug!("hello world cli");
Ok(())
}
{% if project-diagnosis == "log" -%}
/// Initialize log based on RUST_LOG and the CLI verbosity.
///
/// Rules:
/// - If RUST_LOG is set, it is fully respected.
/// - If RUST_LOG is not set and verbose == 0 -> INFO level.
/// - If RUST_LOG is not set and verbose > 0 -> DEBUG level.
fn init_logger(verbose: u8) {
use std::io::Write;
use log::LevelFilter;
if std::env::var_os("RUST_LOG").is_some() {
env_logger::builder()
.format(|buf, record| {
writeln!(buf, "[{}]: {}", record.level(), record.args())
})
.init();
return;
}
let level =
if verbose > 0 { LevelFilter::Debug } else { LevelFilter::Info };
env_logger::builder()
.filter(None, level)
.format(|buf, record| {
writeln!(buf, "[{}]: {}", record.level(), record.args())
})
.init();
}
{% else -%}
/// Initialize tracing based on RUST_LOG and the CLI verbosity.
///
/// Rules:
/// - If RUST_LOG is set, it is fully respected.
/// - If RUST_LOG is not set and verbose == 0 -> INFO level.
/// - If RUST_LOG is not set and verbose > 0 -> DEBUG level.
fn init_logger(verbose: u8) {
if std::env::var_os("RUST_LOG").is_some() {
tracing_subscriber::fmt()
.with_env_filter(EnvFilter::from_default_env())
.init();
return;
}
let filter = if verbose > 0 {
EnvFilter::new("debug")
} else {
EnvFilter::new("info")
};
tracing_subscriber::fmt().with_env_filter(filter).init();
}
{% endif %}
|