Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ path = "../logger"
path = "../util"

[dependencies]
time = "0.1"
time = "0.3"
rand = "0.3"
4 changes: 2 additions & 2 deletions logger/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.1.0"
authors = ["Sebastian Waisbrot <seppo0010@gmail.com>"]

[target.x86_64-unknown-linux-gnu.dependencies]
syslog = "2.2.0"
syslog = "6.1"

[target.x86_64-apple-darwin.dependencies]
syslog = "2.2.0"
syslog = "6.1"
63 changes: 32 additions & 31 deletions logger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use std::process;
use std::sync::mpsc::{channel, Sender};
use std::thread;

use syslog::{Formatter3164, LoggerBackend};

/// Macro to log a message. Uses the `format!` syntax.
/// See `std::fmt` for more information.
///
Expand Down Expand Up @@ -177,7 +179,7 @@ pub struct Logger {
Option<Output>,
Option<Level>,
Option<String>,
Option<Option<Box<syslog::Logger>>>,
Option<Option<Box<syslog::Logger<LoggerBackend, Formatter3164>>>>,
Option<i32>,
)>,
}
Expand Down Expand Up @@ -207,13 +209,13 @@ impl Logger {
Option<Output>,
Option<Level>,
Option<String>,
Option<Option<Box<syslog::Logger>>>,
Option<Option<Box<syslog::Logger<LoggerBackend, Formatter3164>>>>,
Option<i32>,
)>();
{
let mut level = level;
let mut output = output;
let mut syslog_writer: Option<Box<syslog::Logger>> = None;
let mut syslog_writer: Option<Box<syslog::Logger<LoggerBackend, Formatter3164>>> = None;
thread::spawn(move || {
while let Ok((out, lvl, msg, syslog, code)) = rx.recv() {
match (out, lvl, msg, syslog) {
Expand All @@ -228,21 +230,15 @@ impl Logger {
}
};
if let Some(ref mut w) = syslog_writer {
match w.send_3164(
match lvl {
Level::Debug => syslog::Severity::LOG_DEBUG,
Level::Verbose => syslog::Severity::LOG_INFO,
Level::Notice => syslog::Severity::LOG_NOTICE,
Level::Warning => syslog::Severity::LOG_WARNING,
},
msg.clone(),
) {
Ok(_) => (),
Err(e) => {
// failing to log a message... will write straight to stderr
// if we cannot do that, we'll panic
eprint!("Failed to log {:?} {}", e, msg);
}
if let Err(e) = match lvl {
Level::Debug => w.debug(msg.clone()),
Level::Verbose => w.info(msg.clone()),
Level::Notice => w.notice(msg.clone()),
Level::Warning => w.warning(msg.clone()),
} {
// failing to log a message... will write straight to stderr
// if we cannot do that, we'll panic
eprint!("Failed to log {:?} {}", e, msg);
}
}
}
Expand Down Expand Up @@ -381,21 +377,26 @@ impl Logger {
/// Enables syslog.
#[cfg(unix)]
pub fn set_syslog(&mut self, ident: &str, facility: &str) {
let mut w = syslog::unix(match &*facility.to_ascii_lowercase() {
"local0" => syslog::Facility::LOG_LOCAL0,
"local1" => syslog::Facility::LOG_LOCAL1,
"local2" => syslog::Facility::LOG_LOCAL2,
"local3" => syslog::Facility::LOG_LOCAL3,
"local4" => syslog::Facility::LOG_LOCAL4,
"local5" => syslog::Facility::LOG_LOCAL5,
"local6" => syslog::Facility::LOG_LOCAL6,
"local7" => syslog::Facility::LOG_LOCAL7,
_ => syslog::Facility::LOG_USER,
})
let formatter = Formatter3164 {
facility: match &*facility.to_ascii_lowercase() {
"local0" => syslog::Facility::LOG_LOCAL0,
"local1" => syslog::Facility::LOG_LOCAL1,
"local2" => syslog::Facility::LOG_LOCAL2,
"local3" => syslog::Facility::LOG_LOCAL3,
"local4" => syslog::Facility::LOG_LOCAL4,
"local5" => syslog::Facility::LOG_LOCAL5,
"local6" => syslog::Facility::LOG_LOCAL6,
"local7" => syslog::Facility::LOG_LOCAL7,
_ => syslog::Facility::LOG_USER,
},
hostname: None,
process: ident.to_owned(),
pid: std::process::id(),
};
let w = syslog::unix(formatter)
.unwrap();
w.set_process_name(ident.to_owned());
self.tx
.send((None, None, None, Some(Some(w)), None))
.send((None, None, None, Some(Some(Box::new(w))), None))
.unwrap();
}

Expand Down
5 changes: 3 additions & 2 deletions util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
name = "util"
version = "0.1.0"
authors = ["Sebastian Waisbrot <seppo0010@gmail.com>"]
edition = "2021"

[dependencies]
time = "0.1"
time = "0.3"
libc = "0.1.8"
rand = "0.3"
rand = "0.8.5"
9 changes: 5 additions & 4 deletions util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ extern crate time;
use std::fmt;

use libc::types::os::arch::c95::c_int;
use rand::{thread_rng, Rng};
use time::get_time;
use rand::{thread_rng, RngCore};
use time::OffsetDateTime;


/// Are two chars the same? Optionally ignoring the case.
///
Expand Down Expand Up @@ -163,8 +164,8 @@ pub fn glob_match(pattern: &[u8], element: &[u8], ignore_case: bool) -> bool {

/// Current timestamp in microseconds
pub fn ustime() -> i64 {
let tv = get_time();
tv.sec * 1000000 + (tv.nsec / 1000) as i64
let now = OffsetDateTime::now_utc();
(now.unix_timestamp_nanos() / 1_000).try_into().unwrap()
}

/// Current timestamp in milliseconds
Expand Down