Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. // src/program.rs
  2.  
  3. use flexi_logger::{init as log_init, LogConfig, LogRecord};
  4. use time;
  5. use std::process::exit;
  6.  
  7. fn app_format(record: &LogRecord) -> String {
  8. //2016-02-09 10:20:15,784 [myprog] INFO src/myprog.rs - Processing update events
  9. let tm = time::at(time::get_time());
  10. let time: String = time::strftime("%Y-%m-%d %H:%M:%S,%f", &tm).unwrap();
  11. format!( "{} [{}] {:<5} {} - {}",
  12. &time[..time.len() - 6],
  13. record.location().module_path(),
  14. record.level(),
  15. record.location().file(),
  16. &record.args())
  17. }
  18.  
  19. pub fn init<S: Into<String>>(spec: Option<S>) {
  20. let mut log_config = LogConfig::new();
  21. log_config.format = app_format;
  22. log_init(log_config, spec.map(|s| s.into())).unwrap();
  23. }
  24.  
  25. pub fn exit_with_error(msg: String, code: i32) -> ! {
  26. error!("Exiting: {}", msg);
  27. exit(code);
  28. }
  29.  
  30. // src/myprog.rs
  31. #[macro_use]
  32. extern crate clap;
  33. #[macro_use]
  34. extern crate log;
  35. extern crate flexi_logger;
  36. extern crate time;
  37.  
  38. use clap::{ArgMatches, App, Arg};
  39.  
  40. mod program;
  41.  
  42. fn myprog(args: ArgMatches) -> Result<(), (String, i32)> {
  43. Ok(())
  44. }
  45.  
  46. fn main() {
  47. let args = App::new("My App")
  48. .version(crate_version!())
  49. .author("Jakub Pastuszek <jpastuszek@whatclinic.com>")
  50. .about("Does stuff")
  51. .arg(Arg::with_name("log-spec")
  52. .short("l")
  53. .long("log-sepc")
  54. .value_name("LOG_LEVEL_SPEC")
  55. .help("Logging level specification, e.g: myprog=info")
  56. .takes_value(true))
  57. .get_matches();
  58.  
  59. program::init(args.value_of("log-spec"));
  60.  
  61. myprog(args).unwrap_or_else(|(err, code)| program::exit_with_error(err, code));
  62. }
  63.  
  64. // Cargo.toml
  65. [package]
  66. name = "myprog"
  67. version = "0.1.0"
  68. authors = ["Jakub Pastuszek <jpastuszek@gmail.com>"]
  69.  
  70. [[bin]]
  71. name = "myprog"
  72. path = "src/myprog.rs"
  73.  
  74. [dependencies]
  75. clap = "~2.0"
  76. log = "~0.3"
  77. flexi_logger = "~0.3"
  78. time = "~0.1"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement