Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix loglevel compare, clippy errors, reformat with rustfmt #11

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 35 additions & 26 deletions lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
#![warn(missing_docs)]

use slog::{Drain, Level, OwnedKVList, Record};
use std::{fmt, io};
use std::sync::Mutex;
use std::cell::RefCell;
use std::path::{Path, PathBuf};
use std::net::SocketAddr;
use std::io::{Error, ErrorKind};
use std::net::SocketAddr;
use std::path::{Path, PathBuf};
use std::sync::Mutex;
use std::{fmt, io};

use slog::KV;

Expand All @@ -44,12 +44,15 @@ thread_local! {
type SysLogger = syslog::Logger<syslog::LoggerBackend, syslog::Formatter3164>;

#[inline]
fn handle_syslog_error(e: syslog::Error) -> io::Error
{
fn handle_syslog_error(e: syslog::Error) -> io::Error {
Error::new(ErrorKind::Other, e.to_string())
}

fn log_with_level(level: slog::Level, mut io: std::sync::MutexGuard<Box<SysLogger>>, buf: &str) -> io::Result<()> {
fn log_with_level(
level: slog::Level,
mut io: std::sync::MutexGuard<Box<SysLogger>>,
buf: &str,
) -> io::Result<()> {
let err = match level {
Level::Critical => io.crit(&buf),
Level::Error => io.err(&buf),
Expand All @@ -61,16 +64,19 @@ fn log_with_level(level: slog::Level, mut io: std::sync::MutexGuard<Box<SysLogge
err.map_err(handle_syslog_error)
}

/// Create a formatter with runtime metadata filled in.
/// Create a formatter with runtime metadata filled in.
///
/// This follows ``get_process_info()`` in the syslog crate to some extent
/// which is private.
fn syslog_format3164(facility: syslog::Facility, hostname: Option<String>) -> syslog::Formatter3164 {
let path = std::env::current_exe()
.unwrap_or_else(|_| PathBuf::new());
let process = path.file_name()
fn syslog_format3164(
facility: syslog::Facility,
hostname: Option<String>,
) -> syslog::Formatter3164 {
let path = std::env::current_exe().unwrap_or_else(|_| PathBuf::new());
let process = path
.file_name()
.map(|file| file.to_string_lossy().into_owned())
.unwrap_or_else(|| String::new());
.unwrap_or_default();

syslog::Formatter3164 {
facility,
Expand Down Expand Up @@ -102,7 +108,8 @@ fn get_default_level() -> Level {
Level::Warning
} else if cfg!(feature = "max_level_error") {
Level::Error
} else { // max_level_off
} else {
// max_level_off
Level::Critical
}
}
Expand All @@ -119,7 +126,8 @@ fn get_default_level() -> Level {
Level::Warning
} else if cfg!(feature = "release_max_level_error") {
Level::Error
} else { // release_max_level_off
} else {
// release_max_level_off
Level::Critical
}
}
Expand All @@ -146,16 +154,16 @@ impl Drain for Streamer3164 {
type Ok = ();

fn log(&self, info: &Record, logger_values: &OwnedKVList) -> io::Result<()> {
if self.level > info.level() {
return Ok(())
if self.level.as_usize() < info.level().as_usize() {
return Ok(());
}
TL_BUF.with(|buf| {
let mut buf = buf.borrow_mut();
let res = {
|| {
self.format.format(&mut *buf, info, logger_values)?;
let io =
self.io
let io = self
.io
.lock()
.map_err(|_| Error::new(ErrorKind::Other, "locking error"))?;

Expand All @@ -171,6 +179,7 @@ impl Drain for Streamer3164 {
}

/// Formatter to format defined in RFC 3164
#[derive(Default)]
pub struct Format3164;

impl Format3164 {
Expand All @@ -187,7 +196,7 @@ impl Format3164 {
) -> io::Result<()> {
write!(io, "{}", record.msg())?;

let mut ser = KSV::new(io);
let mut ser = KeyValueSerializer::new(io);
{
logger_kv.serialize(record, &mut ser)?;
record.kv().serialize(record, &mut ser)?;
Expand All @@ -197,17 +206,17 @@ impl Format3164 {
}

/// Key-Separator-Value serializer
struct KSV<W: io::Write> {
struct KeyValueSerializer<W: io::Write> {
io: W,
}

impl<W: io::Write> KSV<W> {
impl<W: io::Write> KeyValueSerializer<W> {
fn new(io: W) -> Self {
KSV { io: io }
KeyValueSerializer { io }
}
}

impl<W: io::Write> slog::Serializer for KSV<W> {
impl<W: io::Write> slog::Serializer for KeyValueSerializer<W> {
fn emit_arguments(&mut self, key: &str, val: &fmt::Arguments) -> slog::Result {
write!(self.io, ", {}: {}", key, val)?;
Ok(())
Expand Down Expand Up @@ -326,11 +335,11 @@ impl SyslogBuilder {
} => {
let format = syslog_format3164(facility, Some(hostname));
syslog::udp(format, local, host).map_err(handle_syslog_error)?
},
}
SyslogKind::Tcp { server, hostname } => {
let format = syslog_format3164(facility, Some(hostname));
syslog::tcp(format, server).map_err(handle_syslog_error)?
},
}
};
Ok(Streamer3164::new_with_level(Box::new(log), self.level))
}
Expand Down