Quellcode durchsuchen

Add syslog Logger.

Add rust's syslog as dependency and create a Logger to interface to it.
Note: systemd should take care already of logging into syslog, so I'm not sure
this thing I'm doing is really useful.
Michele Orrù vor 8 Jahren
Ursprung
Commit
e81aa2af2c
3 geänderte Dateien mit 93 neuen und 11 gelöschten Zeilen
  1. 46 0
      Cargo.lock
  2. 1 0
      Cargo.toml
  3. 46 11
      src/main.rs

+ 46 - 0
Cargo.lock

@@ -5,6 +5,7 @@ dependencies = [
  "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
  "mio 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)",
  "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
+ "syslog 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
 [[package]]
@@ -17,6 +18,11 @@ name = "cfg-if"
 version = "0.1.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 
+[[package]]
+name = "debug-builders"
+version = "0.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
 [[package]]
 name = "kernel32-sys"
 version = "0.2.2"
@@ -31,6 +37,11 @@ name = "lazycell"
 version = "0.4.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 
+[[package]]
+name = "libc"
+version = "0.1.12"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
 [[package]]
 name = "libc"
 version = "0.2.17"
@@ -119,6 +130,36 @@ name = "slab"
 version = "0.3.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 
+[[package]]
+name = "syslog"
+version = "3.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "libc 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)",
+ "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
+ "time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)",
+ "unix_socket 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "time"
+version = "0.1.35"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "libc 0.2.17 (registry+https://github.com/rust-lang/crates.io-index)",
+ "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "unix_socket"
+version = "0.4.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "debug-builders 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "libc 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
 [[package]]
 name = "void"
 version = "1.0.2"
@@ -146,8 +187,10 @@ dependencies = [
 [metadata]
 "checksum bitflags 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8dead7461c1127cf637931a1e50934eb6eee8bff2f74433ac7909e9afcee04a3"
 "checksum cfg-if 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "de1e760d7b6535af4241fca8bd8adf68e2e7edacc6b29f5d399050c5e48cf88c"
+"checksum debug-builders 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0f5d8e3d14cabcb2a8a59d7147289173c6ada77a0bc526f6b85078f941c0cf12"
 "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"
 "checksum lazycell 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ce12306c4739d86ee97c23139f3a34ddf0387bbf181bc7929d287025a8c3ef6b"
+"checksum libc 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "e32a70cf75e5846d53a673923498228bbec6a8624708a9ea5645f075d6276122"
 "checksum libc 0.2.17 (registry+https://github.com/rust-lang/crates.io-index)" = "044d1360593a78f5c8e5e710beccdc24ab71d1f01bc19a29bcacdba22e8475d8"
 "checksum log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "ab83497bf8bf4ed2a74259c1c802351fcd67a65baa86394b6ba73c36f4838054"
 "checksum mio 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "410a1a0ff76f5a226f1e4e3ff1756128e65cd30166e39c3892283e2ac09d5b67"
@@ -158,6 +201,9 @@ dependencies = [
 "checksum rustc_version 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "c5f5376ea5e30ce23c03eb77cbe4962b988deead10910c372b226388b594c084"
 "checksum semver 0.1.20 (registry+https://github.com/rust-lang/crates.io-index)" = "d4f410fedcf71af0345d7607d246e7ad15faaadd49d240ee3b24e5dc21a820ac"
 "checksum slab 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "17b4fcaed89ab08ef143da37bc52adbcc04d4a69014f4c1208d6b51f0c47bc23"
+"checksum syslog 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7003b5cf4f953ef43d628e56ab42c06f23539b84d361d0c0ce330c8828c037e1"
+"checksum time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)" = "3c7ec6d62a20df54e07ab3b78b9a3932972f4b7981de295563686849eb3989af"
+"checksum unix_socket 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "d570eba7deb3197c04599645dd776ca8b7b9c3c623be7491c61d50ed5895f13d"
 "checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d"
 "checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"
 "checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc"

+ 1 - 0
Cargo.toml

@@ -7,3 +7,4 @@ authors = ["Michele Orrù <maker@tumbolandia.net>"]
 rand = "0.3.0"
 mio = "0.6.0"
 log = "0.3.6"
+syslog = "~3.1.0"

+ 46 - 11
src/main.rs

@@ -1,18 +1,53 @@
 extern crate mio;
 #[macro_use]
 extern crate log;
+extern crate syslog;
 
-use std::io::Write;
-//use std::net::SocketAddr;
+use log::*;
 use mio::*;
 use mio::tcp::TcpListener;
-use log::*;
+use std::io::Write;
+use syslog::{Facility,Severity};
 
 
 const LISTENADDR : &'static str = "127.0.0.1:13265";
 
 
+struct SyslogLogger {
+    writer: Box<syslog::Logger>
+}
+
+impl SyslogLogger {
+    fn init() -> Result<(), SetLoggerError> {
+        let writer = syslog::unix(Facility::LOG_USER)
+            .expect("impossible to connect to syslog.");
+
+        log::set_logger(|max_log_level| {
+            max_log_level.set(LogLevelFilter::Info);
+            Box::new(SyslogLogger {writer: writer} )
+        })
+    }
+}
+
+impl log::Log for SyslogLogger {
+    fn enabled(&self, metadata: &LogMetadata) -> bool {
+        metadata.level() <= LogLevel::Info
+    }
+
+    fn log(&self, record: &LogRecord) {
+        if self.enabled(record.metadata()) {
+            let message = format!("{} {}", record.level(), record.args());
+            let r = self.writer.send(Severity::LOG_ALERT, &message);
+            if r.is_err() {
+                println!("error sending the log {}", r.err().expect("got error"));
+            }
+        }
+    }
+}
+
+
 struct StderrLogger;
+
 impl StderrLogger {
     fn init() -> Result<(), SetLoggerError> {
         log::set_logger(|max_log_level| {
@@ -22,6 +57,7 @@ impl StderrLogger {
     }
 }
 
+
 impl log::Log for StderrLogger {
     fn enabled(&self, metadata: &LogMetadata) -> bool {
         metadata.level() <= LogLevel::Info
@@ -29,8 +65,8 @@ impl log::Log for StderrLogger {
 
     fn log(&self, record: &LogRecord) {
         if self.enabled(record.metadata()) {
-            writeln!(&mut std::io::stderr(), "{} {}", record.level(), record.args()).
-                expect("Can't log.")
+            writeln!(&mut std::io::stderr(), "{} {}", record.level(), record.args())
+                .expect("error sending the log")
         }
     }
 }
@@ -40,21 +76,20 @@ fn malformed_packet(err: std::io::Error) {
     info!("Somebody sent an invalid tcp packet: {}", err.to_string());
 }
 
-fn handle(mut stream: tcp::TcpStream) {
-    let _ = stream.write(&"go fuck you kthxbye".as_bytes());
+fn handle(stream: tcp::TcpStream) {
+//    let _ = stream.write(&"go fuck you kthxbye".as_bytes());
     match stream.local_addr() {
         Ok(addr) =>
-            info!("somebody opened a tcp connection to {}:{}", addr.ip(), addr.port()),
+            info!("new connection to {}:{}", addr.ip(), addr.port()),
         Err(err) => malformed_packet(err),
     }
 }
 
 fn main() {
-    StderrLogger::init().expect("can't set up logger.");
+    SyslogLogger::init().expect("can't set up logger.");
 
     info!("Starting up!");
 
-
     // Setup some tokens to allow us to identify which event is
     // for which socket.
     const SERVER: Token = Token(0);
@@ -65,7 +100,7 @@ fn main() {
         .expect(&format!("Can't bind to address {}", LISTENADDR));
 
     // Create an poll instance
-    let poll = Poll::new().unwrap();
+    let poll = Poll::new().expect("Cannot create pool.");
 
     // Start listening for incoming connections
     poll.register(&server, SERVER, Ready::all(), PollOpt::edge())