Skip to content

Commit b94a5c3

Browse files
committed
refactor: follow how vector-rs handle unix stream syslog message
Signed-off-by: tison <[email protected]>
1 parent dd4a328 commit b94a5c3

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/sender/unix.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
use std::borrow::Cow;
1516
use std::io;
1617
use std::io::BufWriter;
1718
use std::io::Write;
@@ -50,9 +51,7 @@ pub fn unix(path: impl AsRef<Path>) -> io::Result<SyslogSender> {
5051

5152
pub fn unix_well_known() -> io::Result<SyslogSender> {
5253
cfg_if::cfg_if! {
53-
if #[cfg(target_os = "linux")] {
54-
unix("/dev/log")
55-
} else if #[cfg(target_os = "macos")] {
54+
if #[cfg(target_os = "macos")] {
5655
// NOTE: This may not work on Monterey (12.x) and above,
5756
// see also https://github.com/python/cpython/issues/91070.
5857
unix("/var/run/syslog")
@@ -112,6 +111,7 @@ impl_syslog_sender_common!(UnixDatagramSender);
112111
pub struct UnixStreamSender {
113112
writer: BufWriter<UnixStream>,
114113
context: SyslogContext,
114+
postfix: Cow<'static, str>,
115115
}
116116

117117
impl UnixStreamSender {
@@ -121,9 +121,17 @@ impl UnixStreamSender {
121121
Ok(Self {
122122
writer: BufWriter::new(socket),
123123
context: SyslogContext::default(),
124+
postfix: Cow::Borrowed("\r\n"),
124125
})
125126
}
126127

128+
/// Set the postfix when formatting Syslog message.
129+
///
130+
/// Default is "\r\n". You can use empty string to set no postfix.
131+
pub fn set_postfix(&mut self, postfix: impl Into<Cow<'static, str>>) {
132+
self.postfix = postfix.into();
133+
}
134+
127135
/// Set the context when formatting Syslog message.
128136
pub fn set_context(&mut self, context: SyslogContext) {
129137
self.context = context;
@@ -137,7 +145,7 @@ impl UnixStreamSender {
137145
/// Send a pre-formatted message.
138146
pub fn send_formatted(&mut self, formatted: &[u8]) -> io::Result<()> {
139147
self.writer.write_all(formatted)?;
140-
self.writer.write_all(&[0; 1])?;
148+
self.writer.write_all(self.postfix.as_bytes())?;
141149
Ok(())
142150
}
143151

0 commit comments

Comments
 (0)