From 5b59ba3aee3e874b2e67cf529680d19910a3ec7d Mon Sep 17 00:00:00 2001 From: Ben Morrison Date: Tue, 27 Aug 2019 01:10:51 -0400 Subject: db connected, logging up --- src/db.rs | 29 ++++++++++++++++++++++++++--- src/logging.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 11 +++++++++++ 3 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 src/logging.rs (limited to 'src') diff --git a/src/db.rs b/src/db.rs index d3ecd6a..6c1214b 100644 --- a/src/db.rs +++ b/src/db.rs @@ -1,5 +1,8 @@ +use log::info; +use rand; use rusqlite; use std::sync::mpsc; +use std::time; #[derive(Debug)] pub struct Post { @@ -9,9 +12,10 @@ pub struct Post { body: String, } +#[derive(Debug)] pub struct Conn { db: rusqlite::Connection, - tx: mpsc::Sender, + rx: mpsc::Receiver, } #[derive(Debug)] @@ -24,6 +28,8 @@ pub enum Cmd { impl Conn { fn init() -> rusqlite::Connection { + let start = time::Instant::now(); + info!("Connecting to database"); let conn = rusqlite::Connection::open_with_flags( "/tmp/db.sql", rusqlite::OpenFlags::SQLITE_OPEN_FULL_MUTEX @@ -43,13 +49,18 @@ impl Conn { ) .expect("Could not initialize DB"); + info!( + "Database connection established in {}ms", + start.elapsed().as_millis() + ); + conn } - pub fn new(tx: mpsc::Sender) -> Self { + pub fn new(rx: mpsc::Receiver) -> Self { Conn { db: Conn::init(), - tx, + rx, } } } @@ -66,6 +77,18 @@ impl Cmd { } impl Post { + pub fn new(title: &str, author: &str, body: &str) -> Self { + let id = rand::random::(); + let title = title.to_string(); + let author = author.to_string(); + let body = body.to_string(); + Post { + id, + title, + author, + body, + } + } pub fn id(&self) -> String { format!("{}", self.id) } diff --git a/src/logging.rs b/src/logging.rs new file mode 100644 index 0000000..332de6c --- /dev/null +++ b/src/logging.rs @@ -0,0 +1,48 @@ +use std::fs; +use std::fs::File; + +use chrono::offset::Utc; +use simplelog::*; + +pub const FILE: &str = "/tmp/clinte.log"; + +pub fn init() { + // If the log file exists on startup, + // move and timestamp it so we get a + // fresh log file. + if fs::metadata(FILE).is_ok() { + let mut newpath = FILE.to_string(); + let time = Utc::now().to_rfc3339(); + newpath.push_str("."); + newpath.push_str(&time); + fs::rename(FILE, newpath).unwrap(); + } + + CombinedLogger::init(vec![ + TermLogger::new(LevelFilter::Warn, Config::default(), TerminalMode::Stderr).unwrap(), + WriteLogger::new( + LevelFilter::Info, + Config::default(), + File::create(FILE).unwrap(), + ), + ]) + .expect("Unable to initialize logging"); +} + +#[cfg(test)] +mod tests { + use super::*; + + use log::info; + + #[test] + fn init_logs() { + let blank = " ".bytes().collect::>(); + fs::write("/tmp/dirtmud.log", &blank).unwrap(); + init(); + + info!("TEST LOG MESSAGE"); + let logfile = fs::read_to_string("/tmp/dirtmud.log").unwrap(); + assert!(logfile.contains("TEST LOG MESSAGE")); + } +} diff --git a/src/main.rs b/src/main.rs index 9bc08ad..6c0d957 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,17 @@ +use log::info; +use std::sync::mpsc; + mod db; +mod logging; fn main() { + logging::init(); + info!("clinte starting up!"); println!("clinte-0.1-dev"); println!("a community notices system"); + + let (_tx, rx) = mpsc::channel::(); + let db = db::Conn::new(rx); + + println!("{:?}", db); } -- cgit 1.4.1-2-gfad0