1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Copyright 2009 The gelf_logger Authors. All rights reserved.

use std::sync::{Arc, Mutex};
use std::sync::mpsc::{Receiver, sync_channel, SyncSender};
use std::thread;
use std::time::Duration;

use serde_gelf::{GelfLevel, GelfRecord, GelfRecordGetter};

use crate::buffer::{Buffer, Event, Metronome};
use crate::config::Config;
use crate::logger::GelfLogger;
use crate::output::GelfTcpOutput;
use crate::result::Result;

static mut BATCH_PROCESSOR: &'static Batch = &NoProcessor;

pub fn set_boxed_processor(processor: Box<Batch>) -> Result<()> {
    set_processor_inner(|| unsafe { &*Box::into_raw(processor) })
}

fn set_processor_inner<F>(make_processor: F) -> Result<()> where F: FnOnce() -> &'static Batch {
    unsafe {
        BATCH_PROCESSOR = make_processor();
        Ok(())
    }
}

/// Initialize the logger using a configuration file.
///
/// ### Warning
///
/// The logging system may only be initialized once.
///
/// ## Example
///
/// ```rust
/// use gelf_logger::Config;
///
/// fn main() {
///     let cfg = Config::try_from_yaml("/tmp/myconfig.yml").unwrap();
///     gelf_logger::init(cfg).unwrap();
///
///     info!("hello");
///
///     gelf_logger::flush().expect("Failed to send buffer, log records can be lost !");
/// }
/// ```
///
pub fn init_from_file(path: &str) -> Result<()> {
    init(Config::try_from_yaml(path)?)
}

/// Initialize the logger using the given [`Config`](struct.Config.html).
///
/// ### Warning
///
/// The logging system may only be initialized once.
///
/// ## Example
///
/// ```rust
/// use gelf_logger::Config;
///
/// fn main() {
///     let cfg = Config::builder()
///         .set_hostname("myhost.com".into())
///         .set_port(12202)
///         .build();
///
///     gelf_logger::init(cfg).unwrap();
///
///     info!("hello");
///
///     gelf_logger::flush().expect("Failed to send buffer, log records can be lost !");
/// }
/// ```
///
pub fn init(cfg: Config) -> Result<()> {
    let (tx, rx): (SyncSender<Event>, Receiver<Event>) = sync_channel(10_000_000);

    if let &Some(duration) = cfg.buffer_duration() {
        let ctx = tx.clone();
        Metronome::start(duration, ctx);
    }

    let gelf_level = cfg.level().clone();
    let log_level = log::Level::from(&gelf_level);

    let logger = GelfLogger::new(log_level);
    let arx = Arc::new(Mutex::new(rx));
    thread::spawn(move || {
        let _ = Buffer::new(arx, GelfTcpOutput::from(&cfg)).run();
    });

    log::set_boxed_logger(Box::new(logger)).unwrap();
    log::set_max_level(log_level.to_level_filter());

    let _ = set_boxed_processor(Box::new(BatchProcessor::new(tx, gelf_level)))?;

    Ok(())
}

/// Force current logger record buffer to be sent to the remote server.
///
/// It can be useful to perform a flush just before program exit.
///
/// ## Example
///
/// ```rust
/// use gelf_logger::Config;
///
/// fn main() {
///     let cfg = Config::builder()
///         .set_hostname("myhost.com".into())
///         .set_port(12202)
///         .build();
///
///     gelf_logger::init(cfg).unwrap();
///
///     info!("hello");
///
///     gelf_logger::flush().expect("Failed to send buffer, log records can be lost !");
/// }
/// ```
///
pub fn flush() -> Result<()> {
    processor().flush()
}

pub trait Batch {
    fn send(&self, rec: &GelfRecord) -> Result<()>;
    fn flush(&self) -> Result<()>;
}


pub struct NoProcessor;

impl Batch for NoProcessor {
    fn send(&self, _rec: &GelfRecord) -> Result<()> { Ok(()) }
    fn flush(&self) -> Result<()> { Ok(()) }
}


pub struct BatchProcessor {
    tx: SyncSender<Event>,
    level: GelfLevel,
}

impl BatchProcessor {
    pub fn new(tx: SyncSender<Event>, level: GelfLevel) -> BatchProcessor {
        BatchProcessor { tx, level }
    }
}

impl Batch for BatchProcessor {
    fn send(&self, rec: &GelfRecord) -> Result<()> {
        if self.level >= rec.level() {
            self.tx.send(Event::Data(rec.clone()))?;
        }
        Ok(())
    }
    fn flush(&self) -> Result<()> {
        let _ = self.tx.send(Event::Send)?;
        Ok(thread::sleep(Duration::from_secs(2)))
    }
}

/// Returns a reference to the batch processor.
///
/// If a logger has not been set, a no-op implementation is returned.
#[doc(hidden)]
pub fn processor() -> &'static Batch { unsafe { BATCH_PROCESSOR } }