diff options
author | Ben Morrison <ben@gbmor.dev> | 2019-08-27 01:10:51 -0400 |
---|---|---|
committer | Ben Morrison <ben@gbmor.dev> | 2019-08-27 01:10:51 -0400 |
commit | 5b59ba3aee3e874b2e67cf529680d19910a3ec7d (patch) | |
tree | af9d8c630c144bc875a75fb77f380812b68b69fa /src | |
parent | 9ee17f15bb380d31d55d83586ebdf3644cf326af (diff) | |
download | clinte-5b59ba3aee3e874b2e67cf529680d19910a3ec7d.tar.gz |
db connected, logging up
Diffstat (limited to 'src')
-rw-r--r-- | src/db.rs | 29 | ||||
-rw-r--r-- | src/logging.rs | 48 | ||||
-rw-r--r-- | src/main.rs | 11 |
3 files changed, 85 insertions, 3 deletions
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<Cmd>, + rx: mpsc::Receiver<Cmd>, } #[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<Cmd>) -> Self { + pub fn new(rx: mpsc::Receiver<Cmd>) -> 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::<u32>(); + 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::<Vec<u8>>(); + 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::<db::Cmd>(); + let db = db::Conn::new(rx); + + println!("{:?}", db); } |