move LoggerSink to util.rs

feat/vaults
Tomáš Mládek 2020-09-14 21:18:53 +02:00
parent 01a737285c
commit 36e3c4a145
3 changed files with 31 additions and 30 deletions

View File

@ -1,6 +1,7 @@
use crate::addressing::Address;
use crate::hash::{decode, hash, Hash, Hashable};
use crate::models;
use crate::util::LoggerSink;
use actix::prelude::*;
use actix_derive::Message;
use anyhow::{anyhow, Result};
@ -339,36 +340,6 @@ impl diesel::r2d2::CustomizeConnection<SqliteConnection, diesel::r2d2::Error>
}
}
#[derive(Default)]
struct LoggerSink {
buffer: Vec<u8>,
}
impl std::io::Write for LoggerSink {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.buffer.extend(buf.iter());
if self.buffer.ends_with(b"\n") {
self.flush()?;
}
Ok(buf.len())
}
fn flush(&mut self) -> std::io::Result<()> {
use std::str;
debug!(
"{}",
str::from_utf8(self.buffer.as_mut())
.map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?
.trim()
);
Ok(())
}
}
pub struct OpenResult {
pub pool: DbPool,
pub new: bool,

View File

@ -19,6 +19,7 @@ mod hash;
mod models;
mod routes;
mod schema;
mod util;
const VERSION: &str = env!("CARGO_PKG_VERSION");

29
src/util.rs Normal file
View File

@ -0,0 +1,29 @@
#[derive(Default)]
pub struct LoggerSink {
buffer: Vec<u8>,
}
impl std::io::Write for LoggerSink {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.buffer.extend(buf.iter());
if self.buffer.ends_with(b"\n") {
self.flush()?;
}
Ok(buf.len())
}
fn flush(&mut self) -> std::io::Result<()> {
use std::str;
debug!(
"{}",
str::from_utf8(self.buffer.as_mut())
.map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?
.trim()
);
Ok(())
}
}