Skip to content

Commit

Permalink
support syslog and fluentd on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
zh-jq-b committed May 13, 2024
1 parent a3e6533 commit f09eaeb
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 22 deletions.
14 changes: 12 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ capnpc = "0.19"
#
libc = "0.2.147"
rustix = { version = "0.38", default-features = false }
gethostname = "0.4"
#
serde = "1.0"
yaml-rust = { package = "yaml-rust2", version = "0.8" }
Expand Down
2 changes: 2 additions & 0 deletions lib/g3-compat/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ rust-version = "1.74.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

[target.'cfg(unix)'.dependencies]
libc.workspace = true
2 changes: 1 addition & 1 deletion lib/g3-daemon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ clap.workspace = true
quinn = { workspace = true, optional = true, features = ["runtime-tokio", "ring"] }
g3-types = { workspace = true, features = ["async-log"] }
g3-stdlog.workspace = true
g3-syslog.workspace = true
g3-fluentd.workspace = true
g3-runtime.workspace = true
g3-yaml = { workspace = true, features = ["syslog", "fluentd", "statsd", "sched"] }
Expand All @@ -45,7 +46,6 @@ g3-http = { workspace = true, optional = true }

[target.'cfg(unix)'.dependencies]
daemonize = "0.5"
g3-syslog.workspace = true

[target.'cfg(target_os = "linux")'.dependencies]
g3-journal.workspace = true
Expand Down
5 changes: 0 additions & 5 deletions lib/g3-daemon/src/log/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use yaml_rust::Yaml;
use g3_fluentd::FluentdClientConfig;
#[cfg(target_os = "linux")]
use g3_journal::JournalConfig;
#[cfg(unix)]
use g3_syslog::SyslogBuilder;

const DEFAULT_CHANNEL_SIZE: usize = 4096;
Expand All @@ -35,7 +34,6 @@ pub enum LogConfigDriver {
Discard,
#[cfg(target_os = "linux")]
Journal(JournalConfig),
#[cfg(unix)]
Syslog(SyslogBuilder),
Fluentd(Arc<FluentdClientConfig>),
}
Expand Down Expand Up @@ -72,7 +70,6 @@ impl LogConfig {
)
}

#[cfg(unix)]
pub fn default_syslog(program_name: &'static str) -> Self {
Self::with_driver(
LogConfigDriver::Syslog(SyslogBuilder::with_ident(program_name)),
Expand All @@ -97,7 +94,6 @@ impl LogConfig {
"discard" => Ok(LogConfig::default_discard(program_name)),
#[cfg(target_os = "linux")]
"journal" => Ok(LogConfig::default_journal(program_name)),
#[cfg(unix)]
"syslog" => Ok(LogConfig::default_syslog(program_name)),
"fluentd" => Ok(LogConfig::default_fluentd(program_name)),
_ => Err(anyhow!("invalid log config")),
Expand All @@ -111,7 +107,6 @@ impl LogConfig {
LogConfigDriver::Journal(JournalConfig::with_ident(program_name));
Ok(())
}
#[cfg(unix)]
"syslog" => {
let builder = g3_yaml::value::as_syslog_builder(v, program_name)
.context("invalid syslog config")?;
Expand Down
2 changes: 1 addition & 1 deletion lib/g3-fluentd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ anyhow.workspace = true
thiserror.workspace = true
slog = { workspace = true, features = ["nested-values"] }
chrono = { workspace = true, features = ["clock"] }
rustix = { workspace = true, features = ["system"] }
flume = { workspace = true, features = ["async"] }
rmp.workspace = true
rmp-serde.workspace = true
Expand All @@ -24,5 +23,6 @@ digest.workspace = true
sha2.workspace = true
hex.workspace = true
log.workspace = true
gethostname.workspace = true
g3-types = { workspace = true, features = ["async-log", "rustls"] }
g3-socket.workspace = true
3 changes: 1 addition & 2 deletions lib/g3-fluentd/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ impl Default for FluentdClientConfig {

impl FluentdClientConfig {
pub fn new(server: SocketAddr) -> Self {
let uname = rustix::system::uname();
let hostname = uname.nodename().to_string_lossy().to_string();
let hostname = gethostname::gethostname().to_string_lossy().to_string();
FluentdClientConfig {
server_addr: server,
bind_ip: None,
Expand Down
2 changes: 1 addition & 1 deletion lib/g3-syslog/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ rust-version = "1.74.0"
[dependencies]
slog = { workspace = true, features = ["nested-values"] }
chrono = { workspace = true, features = ["clock"] }
rustix = { workspace = true, features = ["system"] }
itoa.workspace = true
ryu.workspace = true
flume.workspace = true
serde.workspace = true
serde_json.workspace = true
log.workspace = true
gethostname.workspace = true
g3-types = { workspace = true, features = ["async-log"] }
g3-datetime.workspace = true
36 changes: 29 additions & 7 deletions lib/g3-syslog/src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@

use std::io;
use std::net::{IpAddr, SocketAddr, UdpSocket};
#[cfg(unix)]
use std::os::unix::net::UnixDatagram;
#[cfg(unix)]
use std::path::PathBuf;

mod udp;
#[cfg(unix)]
mod unix_datagram;

pub(super) enum SyslogBackend {
Udp(UdpSocket),
#[cfg(unix)]
Unix(UnixDatagram),
}

Expand All @@ -37,6 +41,7 @@ impl io::Write for SyslogBackend {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
match self {
SyslogBackend::Udp(s) => s.send(buf),
#[cfg(unix)]
SyslogBackend::Unix(s) => s.send(buf),
}
}
Expand All @@ -48,22 +53,39 @@ impl io::Write for SyslogBackend {

#[derive(Clone, Debug)]
pub enum SyslogBackendBuilder {
Default,
/// unix socket with path
Unix(PathBuf),
#[cfg(unix)]
Unix(Option<PathBuf>),
/// udp socket with optional bind ip and remote address
Udp(Option<IpAddr>, SocketAddr),
}

#[cfg(unix)]
impl Default for SyslogBackendBuilder {
fn default() -> Self {
SyslogBackendBuilder::Unix(None)
}
}

#[cfg(not(unix))]
impl Default for SyslogBackendBuilder {
fn default() -> Self {
use std::net::Ipv4Addr;

SyslogBackendBuilder::Udp(None, SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 514))
}
}

impl SyslogBackendBuilder {
pub(super) fn build(&self) -> io::Result<SyslogBackend> {
match self {
SyslogBackendBuilder::Default => {
let socket = unix_datagram::default()?;
Ok(SyslogBackend::Unix(socket))
}
#[cfg(unix)]
SyslogBackendBuilder::Unix(path) => {
let socket = unix_datagram::custom(path)?;
let socket = if let Some(path) = path {
unix_datagram::custom(path)?
} else {
unix_datagram::default()?
};
Ok(SyslogBackend::Unix(socket))
}
SyslogBackendBuilder::Udp(bind_ip, server) => {
Expand Down
5 changes: 2 additions & 3 deletions lib/g3-syslog/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl SyslogBuilder {
SyslogBuilder {
ident,
facility: Facility::User,
backend: SyslogBackendBuilder::Default,
backend: SyslogBackendBuilder::default(),
format: SyslogFormatterKind::Rfc3164,
emit_hostname: false,
append_report_ts: false,
Expand Down Expand Up @@ -95,8 +95,7 @@ impl SyslogBuilder {

pub fn start_async(self, async_conf: &AsyncLogConfig) -> AsyncSyslogStreamer {
let hostname = if self.emit_hostname {
let uname = rustix::system::uname();
Some(uname.nodename().to_string_lossy().to_string())
Some(gethostname::gethostname().to_string_lossy().to_string())
} else {
None
};
Expand Down

0 comments on commit f09eaeb

Please sign in to comment.