use my own logging package

This supports formats that I find more useful; one that mimicks the Google
glog package, and one that is similar but adapted for the systemd journal.
This commit is contained in:
Scott Lamb
2017-03-26 00:01:48 -07:00
parent c3cffb510b
commit bfc0e2abe8
14 changed files with 51 additions and 194 deletions

View File

@@ -98,7 +98,6 @@ struct File {
pub fn run() -> Result<(), Error> {
let args: Args = super::parse_args(USAGE)?;
super::install_logger(false);
let (_db_dir, conn) = super::open_conn(&args.flag_db_dir, super::OpenMode::ReadOnly)?;
let mut files = Vec::new();
for e in fs::read_dir(&args.flag_sample_file_dir)? {

View File

@@ -128,7 +128,6 @@ struct Args {
pub fn run() -> Result<(), Error> {
let args: Args = super::parse_args(USAGE)?;
super::install_logger(false);
let (_db_dir, conn) = super::open_conn(&args.flag_db_dir, super::OpenMode::ReadWrite)?;
let db = Arc::new(db::Database::new(conn)?);
//let dir = Arc::new(dir::Fd::open(&args.flag_sample_file_dir)?);

View File

@@ -53,7 +53,6 @@ struct Args {
pub fn run() -> Result<(), Error> {
let args: Args = super::parse_args(USAGE)?;
super::install_logger(false);
let (_db_dir, mut conn) = super::open_conn(&args.flag_db_dir, super::OpenMode::Create)?;
// Check if the database has already been initialized.

View File

@@ -33,10 +33,6 @@ use docopt;
use error::Error;
use libc;
use rusqlite;
use slog::{self, DrainExt};
use slog_envlogger;
use slog_stdlog;
use slog_term;
use std::path::Path;
mod check;
@@ -69,16 +65,6 @@ impl Command {
}
}
/// Initializes logging.
/// `async` should be true only for serving; otherwise logging can block useful work.
/// Sync logging should be preferred for other modes because async apparently is never flushed
/// before the program exits, and partial output from these tools is very confusing.
fn install_logger(async: bool) {
let drain = slog_term::StreamerBuilder::new().stderr();
let drain = slog_envlogger::new(if async { drain.async() } else { drain }.full().build());
slog_stdlog::set_logger(slog::Logger::root(drain.ignore_err(), None)).unwrap();
}
#[derive(PartialEq, Eq)]
enum OpenMode {
ReadOnly,

View File

@@ -77,8 +77,6 @@ fn setup_shutdown_future(h: &reactor::Handle) -> BoxFuture<(), ()> {
pub fn run() -> Result<(), Error> {
let args: Args = super::parse_args(USAGE)?;
super::install_logger(true);
let (_db_dir, conn) = super::open_conn(
&args.flag_db_dir,
if args.flag_read_only { super::OpenMode::ReadOnly } else { super::OpenMode::ReadWrite })?;
@@ -137,5 +135,5 @@ pub fn run() -> Result<(), Error> {
}
info!("Exiting.");
::std::process::exit(0);
Ok(())
}

View File

@@ -43,7 +43,6 @@ struct Args {
pub fn run() -> Result<(), Error> {
let arg: Args = super::parse_args(&USAGE)?;
super::install_logger(false);
for timestamp in &arg.arg_ts {
let t = recording::Time::parse(timestamp)?;
println!("{} == {}", t, t.0);

View File

@@ -86,7 +86,6 @@ fn set_journal_mode(conn: &rusqlite::Connection, requested: &str) -> Result<(),
pub fn run() -> Result<(), Error> {
let args: Args = super::parse_args(USAGE)?;
super::install_logger(false);
let (_db_dir, mut conn) = super::open_conn(&args.flag_db_dir, super::OpenMode::ReadWrite)?;
{

View File

@@ -47,6 +47,7 @@ extern crate reffers;
extern crate rusqlite;
extern crate memmap;
#[macro_use] extern crate mime;
extern crate mylog;
extern crate openssl;
extern crate parking_lot;
extern crate regex;
@@ -54,10 +55,6 @@ extern crate rustc_serialize;
extern crate serde;
#[macro_use] extern crate serde_derive;
extern crate serde_json;
extern crate slog;
extern crate slog_envlogger;
extern crate slog_stdlog;
extern crate slog_term;
extern crate smallvec;
extern crate time;
extern crate tokio_core;
@@ -96,9 +93,9 @@ Options:
Commands:
check Check database integrity
init Initialize a database
run Run the daemon: record from cameras and handle HTTP requests
run Run the daemon: record from cameras and serve HTTP
shell Start an interactive shell to modify the database
ts Translate between human-readable and numeric timestamps
ts Translate human-readable and numeric timestamps
upgrade Upgrade the database to the latest schema
";
@@ -118,6 +115,14 @@ fn version() -> String {
}
}
fn parse_fmt<S: AsRef<str>>(fmt: S) -> Option<mylog::Format> {
match fmt.as_ref() {
"google" => Some(mylog::Format::Google),
"google-systemd" => Some(mylog::Format::GoogleSystemd),
_ => None,
}
}
fn main() {
// Parse commandline arguments.
// (Note this differs from cmds::parse_args in that it specifies options_first.)
@@ -127,9 +132,18 @@ fn main() {
.decode())
.unwrap_or_else(|e| e.exit());
if let Err(e) = args.arg_command.unwrap().run() {
use std::io::Write;
writeln!(&mut ::std::io::stderr(), "{}", e).unwrap();
let mut h = mylog::Builder::new()
.set_format(::std::env::var("MOONFIRE_FORMAT")
.ok()
.and_then(parse_fmt)
.unwrap_or(mylog::Format::Google))
.set_spec(&::std::env::var("MOONFIRE_LOG").unwrap_or("info".to_owned()))
.build();
h.clone().install().unwrap();
if let Err(e) = { let _a = h.async(); args.arg_command.unwrap().run() } {
error!("{}", e);
::std::process::exit(1);
}
info!("Success.");
}

View File

@@ -37,10 +37,6 @@ use rusqlite;
use std::env;
use std::sync;
use std::thread;
use slog::{self, DrainExt};
use slog_envlogger;
use slog_stdlog;
use slog_term;
use time;
use uuid::Uuid;
@@ -61,9 +57,6 @@ pub const TEST_CAMERA_ID: i32 = 1;
/// results regardless of machine setup.)
pub fn init() {
INIT.call_once(|| {
let drain = slog_term::StreamerBuilder::new().async().full().build();
let drain = slog_envlogger::new(drain);
slog_stdlog::set_logger(slog::Logger::root(drain.ignore_err(), None)).unwrap();
env::set_var("TZ", "America/Los_Angeles");
time::tzset();
});