From 506c83c160b1b27a5adc4525050d2440026f091d Mon Sep 17 00:00:00 2001 From: Nick Alexander Date: Thu, 26 Jan 2017 10:43:48 -0800 Subject: [PATCH] Implement basic logging infrastructure. (#205) r=nalexander,victorporof Signed-off-by: Paul Lange --- Cargo.toml | 15 +++++++++------ src/lib.rs | 6 ++++++ src/main.rs | 32 ++++++++++++++++++++++++++++++-- 3 files changed, 45 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index dc3572ba..4c6a7a4c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,11 +1,14 @@ [package] +authors = ["Richard Newman ", "Nicholas Alexander "] name = "mentat" version = "0.4.0" -authors = ["Richard Newman ", "Nicholas Alexander "] [dependencies] clap = "2.19.3" nickel = "0.9.0" +slog = "1.4.0" +slog-scope = "0.2.2" +slog-term = "1.3.4" [dependencies.rusqlite] version = "0.9.3" @@ -13,16 +16,16 @@ version = "0.9.3" features = ["bundled"] [dependencies.edn] - path = "edn" +path = "edn" [dependencies.mentat_db] - path = "db" +path = "db" [dependencies.mentat_query] - path = "query" +path = "query" [dependencies.mentat_query_parser] - path = "query-parser" +path = "query-parser" [dependencies.mentat_tx_parser] - path = "tx-parser" +path = "tx-parser" diff --git a/src/lib.rs b/src/lib.rs index aeb98506..162177ee 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,6 +8,11 @@ // CONDITIONS OF ANY KIND, either express or implied. See the License for the // specific language governing permissions and limitations under the License. +#[macro_use] +extern crate slog; +#[macro_use] +extern crate slog_scope; + extern crate edn; extern crate mentat_query; extern crate mentat_query_parser; @@ -18,6 +23,7 @@ use rusqlite::Connection; pub mod ident; pub fn get_name() -> String { + info!("Called into mentat library"; "fn" => "get_name"); return String::from("mentat"); } diff --git a/src/main.rs b/src/main.rs index ebf34784..a0fc6447 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,7 +13,16 @@ extern crate clap; use nickel::{Nickel, HttpRouter}; +#[macro_use] +extern crate slog; +#[macro_use] +extern crate slog_scope; +extern crate slog_term; + +extern crate mentat; + use clap::{App, Arg, SubCommand, AppSettings}; +use slog::DrainExt; use std::u16; use std::str::FromStr; @@ -44,10 +53,29 @@ fn main() { let debug = matches.is_present("debug"); let port = u16::from_str(matches.value_of("port").unwrap()).expect("Port must be an integer"); if debug { - println!("This doesn't do anything yet, but it will eventually serve up the following database: {}", - matches.value_of("database").unwrap()); + println!("This doesn't do anything yet, but it will eventually serve up the following database: {} \ + on port: {}.", + matches.value_of("database").unwrap(), + matches.value_of("port").unwrap()); } + // Set up logging. + let log_level = if debug { + slog::Level::Debug + } else { + slog::Level::Warning + }; + let term_logger = slog_term::streamer().build().fuse(); + let log = slog::Logger::root(slog::LevelFilter::new(term_logger, log_level), + o!("version" => env!("CARGO_PKG_VERSION"))); + slog_scope::set_global_logger(log); + + info!("Serving database"; "database" => matches.value_of("database").unwrap(), + "port" => port, + "debug mode" => debug); + + error!("Calling a function: {}", mentat::get_name()); + let mut server = Nickel::new(); server.get("/", middleware!("This doesn't do anything yet")); server.listen(("127.0.0.1", port)).expect("Failed to launch server");