<commit_before>use std::io::prelude::*;
use std::net;
use std::io;
use std::convert::From;

extern crate rustc_serialize;
use rustc_serialize::json;
use rustc_serialize::Encodable;

extern crate time;

pub struct Fluentd<'a, A: net::ToSocketAddrs> {
    pub address: A,
    pub tag: &'a str,
}

#[derive(Debug)]
pub enum FluentError {
    DecodeError(json::EncoderError),
    IoError(io::Error),
}

impl From<io::Error> for FluentError {
    fn from(err: io::Error) -> FluentError {
        FluentError::IoError(err)
    }
}

impl From<json::EncoderError> for FluentError {
    fn from(err: json::EncoderError) -> FluentError {
        FluentError::DecodeError(err)
    }
}


impl <'a, A: net::ToSocketAddrs> Fluentd<'a, A> {
    pub fn write<B: Encodable> (&self, object: &B) -> Result<(), FluentError> {
        let tag = try!(json::encode(&self.tag));
        let now = time::now();
        let record = try!(json::encode(object));
        let message = format!("[{},{},{}]", tag, now.to_timespec().sec, record);

        let mut stream = try!(net::TcpStream::connect(&self.address));
        let _ = stream.write(&message.into_bytes());
        drop(stream);

        Ok(())
    }
}<commit_msg>Handling of persistent connections<commit_after>use std::io::prelude::*;
use std::net;
use std::io;
use std::convert::From;

extern crate rustc_serialize;
use rustc_serialize::json;
use rustc_serialize::Encodable;

extern crate time;

pub struct Fluentd<A: net::ToSocketAddrs> {
    pub address: A,
    pub stream: net::TcpStream,
}

#[derive(Debug)]
pub enum FluentError {
    DecodeError(json::EncoderError),
    IoError(io::Error),
}

impl From<io::Error> for FluentError {
    fn from(err: io::Error) -> FluentError {
        FluentError::IoError(err)
    }
}

impl From<json::EncoderError> for FluentError {
    fn from(err: json::EncoderError) -> FluentError {
        FluentError::DecodeError(err)
    }
}


impl <A: net::ToSocketAddrs> Fluentd<A> {
    pub fn new<'a>(address: A) -> Result<Fluentd<A>, FluentError> {
        let stream = net::TcpStream::connect(&address).unwrap();
        let client = Fluentd {
            address: address,
            stream: stream,
        };
        Ok(client)
    }

    pub fn write<'a, B: Encodable> (&mut self, tag: &'a str, object: &B) -> Result<(), FluentError> {
        let tag = try!(json::encode(&tag));
        let now = time::now();
        let record = try!(json::encode(object));
        let message = format!("[{},{},{}]", tag, now.to_timespec().sec, record);

        let _ = self.stream.write(&message.into_bytes());
        Ok(())
    }
}
{'fraction_non_alphanumeric': 0.1694850115295926, 'fraction_numerical': 0.0, 'mean_word_length': 3.4571917808219177, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3245341', 'n_tokens_mistral': 887, 'n_tokens_neox': 884, 'n_words': 200}