Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // src/program.rs
- use flexi_logger::{init as log_init, LogConfig, LogRecord};
- use time;
- use std::process::exit;
- fn app_format(record: &LogRecord) -> String {
- //2016-02-09 10:20:15,784 [myprog] INFO src/myprog.rs - Processing update events
- let tm = time::at(time::get_time());
- let time: String = time::strftime("%Y-%m-%d %H:%M:%S,%f", &tm).unwrap();
- format!( "{} [{}] {:<5} {} - {}",
- &time[..time.len() - 6],
- record.location().module_path(),
- record.level(),
- record.location().file(),
- &record.args())
- }
- pub fn init<S: Into<String>>(spec: Option<S>) {
- let mut log_config = LogConfig::new();
- log_config.format = app_format;
- log_init(log_config, spec.map(|s| s.into())).unwrap();
- }
- pub fn exit_with_error(msg: String, code: i32) -> ! {
- error!("Exiting: {}", msg);
- exit(code);
- }
- // src/myprog.rs
- #[macro_use]
- extern crate clap;
- #[macro_use]
- extern crate log;
- extern crate flexi_logger;
- extern crate time;
- use clap::{ArgMatches, App, Arg};
- mod program;
- fn myprog(args: ArgMatches) -> Result<(), (String, i32)> {
- Ok(())
- }
- fn main() {
- let args = App::new("My App")
- .version(crate_version!())
- .author("Jakub Pastuszek <jpastuszek@whatclinic.com>")
- .about("Does stuff")
- .arg(Arg::with_name("log-spec")
- .short("l")
- .long("log-sepc")
- .value_name("LOG_LEVEL_SPEC")
- .help("Logging level specification, e.g: myprog=info")
- .takes_value(true))
- .get_matches();
- program::init(args.value_of("log-spec"));
- myprog(args).unwrap_or_else(|(err, code)| program::exit_with_error(err, code));
- }
- // Cargo.toml
- [package]
- name = "myprog"
- version = "0.1.0"
- authors = ["Jakub Pastuszek <jpastuszek@gmail.com>"]
- [[bin]]
- name = "myprog"
- path = "src/myprog.rs"
- [dependencies]
- clap = "~2.0"
- log = "~0.3"
- flexi_logger = "~0.3"
- time = "~0.1"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement