|
@@ -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())
|