Skip to content

Commit

Permalink
src/format: use u32 and std::process::id not libc::getpid
Browse files Browse the repository at this point in the history
Since 0.3.2, libc::getpid is used to get the process id and store the
provided id in Formatters. This has the marked side effect of being
unsafe and adding an unnecessary crate dependency. As such, this change
moves to using std::process::id, which likely was not stable at the
time, for better stdlib integration.

Unfortunately, this is a breaking change, since the current i32s are
part of the external interface, but could be implemented as compatible
if the extant symbols were simply deprecated and new ones added.
  • Loading branch information
urandom2 committed Mar 9, 2020
1 parent 92a0871 commit 705b99b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
8 changes: 4 additions & 4 deletions src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub struct Formatter3164 {
pub facility: Facility,
pub hostname: Option<String>,
pub process: String,
pub pid: i32,
pub pid: u32,
}

impl<T: Display> LogFormat<T> for Formatter3164 {
Expand All @@ -88,7 +88,7 @@ pub struct Formatter5424 {
pub facility: Facility,
pub hostname: Option<String>,
pub process: String,
pub pid: i32,
pub pid: u32,
}

impl Formatter5424 {
Expand All @@ -110,8 +110,8 @@ impl Formatter5424 {
}
}

impl<T: Display> LogFormat<(i32, StructuredData, T)> for Formatter5424 {
fn format<W: Write>(&self, w: &mut W, severity: Severity, log_message: (i32, StructuredData, T)) -> Result<()> {
impl<T: Display> LogFormat<(u32, StructuredData, T)> for Formatter5424 {
fn format<W: Write>(&self, w: &mut W, severity: Severity, log_message: (u32, StructuredData, T)) -> Result<()> {
let (message_id, data, message) = log_message;

write!(w, "<{}> {} {} {} {} {} {} {} {}",
Expand Down
9 changes: 3 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ extern crate log;

use std::env;
use std::path::Path;
use std::process;
use std::fmt::{self,Arguments};
use std::io::{self, BufWriter, Write};
use std::sync::{Arc,Mutex};
use std::net::{SocketAddr,ToSocketAddrs,UdpSocket,TcpStream};
#[cfg(unix)]
use std::os::unix::net::{UnixDatagram, UnixStream};

use libc::getpid;
use log::{Log, Metadata, Record, Level};

mod facility;
Expand Down Expand Up @@ -459,13 +459,10 @@ pub fn init(facility: Facility, log_level: log::LevelFilter,
Ok(())
}

fn get_process_info() -> Result<(String,i32)> {
fn get_process_info() -> Result<(String,u32)> {
env::current_exe().chain_err(|| ErrorKind::Initialization).and_then(|path| {
path.file_name().and_then(|os_name| os_name.to_str()).map(|name| name.to_string())
.chain_err(|| ErrorKind::Initialization)
}).map(|name| {
let pid = unsafe { getpid() };
(name, pid)
})
}).map(|name| (name, process::id()))
}

0 comments on commit 705b99b

Please sign in to comment.