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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Copyright 2024 The gelf_logger Authors. All rights reserved.

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

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

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

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

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

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

/// Initialize the logger using a configuration file.
///
/// ### Warning
///
/// The logging system may only be initialized once.
///
/// ## Example
///
/// ```no_run
/// use gelf_logger::Config;
/// use log::info;
///
/// 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 !");
/// ```
#[cfg(feature = "yaml")]
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;
/// use log::info;
///
/// 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 processor = init_processor(&cfg)?;

    let log_level = log::Level::from(&cfg.level());
    let logger = GelfLogger::new(log_level);

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

    set_boxed_processor(Box::new(processor))?;

    Ok(())
}

/// Initialize the BatchProcessor.
pub fn init_processor(cfg: &Config) -> Result<BatchProcessor> {
    let (tx, rx): (SyncSender<Event>, Receiver<Event>) =
        sync_channel(cfg.async_buffer_size().unwrap_or(1000));

    let config = cfg.clone();

    thread::spawn(move || {
        let gelf_tcp_output = GelfTcpOutput::from(&config);
        Buffer::new(
            rx,
            gelf_tcp_output,
            config.buffer_size(),
            config.buffer_duration(),
        )
        .run();
    });

    let gelf_level = cfg.level();

    Ok(BatchProcessor::new(
        tx,
        gelf_level,
        cfg.full_buffer_policy()
            .unwrap_or(FullBufferPolicy::Discard),
    ))
}

/// 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;
/// use log::info;
///
/// 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()
}

/// Trait for async batch processing of `GelfRecord`.
pub trait Batch {
    /// Send the `GelfRecord` in the async batch processor
    ///
    /// Records will actually be sent depending on configuration options
    fn send(&self, rec: &GelfRecord) -> Result<()>;
    /// Flushes buffered records to the network
    fn flush(&self) -> Result<()>;
}

pub(crate) struct NoProcessor;

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

/// Struct to send event in thread
pub struct BatchProcessor {
    tx: SyncSender<Event>,
    level: GelfLevel,
    full_buffer_policy: FullBufferPolicy,
}

impl BatchProcessor {
    /// Create a ne processor
    pub fn new(
        tx: SyncSender<Event>,
        level: GelfLevel,
        full_buffer_policy: FullBufferPolicy,
    ) -> BatchProcessor {
        BatchProcessor {
            tx,
            level,
            full_buffer_policy,
        }
    }
}

impl Batch for BatchProcessor {
    fn send(&self, rec: &GelfRecord) -> Result<()> {
        if self.level >= rec.level() {
            match self.full_buffer_policy {
                FullBufferPolicy::Wait => self.tx.send(Event::Data(rec.clone()))?,
                FullBufferPolicy::Discard => self.tx.try_send(Event::Data(rec.clone()))?,
            }
        }
        Ok(())
    }
    fn flush(&self) -> Result<()> {
        self.tx.send(Event::Flush)?;
        // FIXME it would be nice to have something more deterministic
        thread::sleep(Duration::from_secs(2));
        Ok(())
    }
}

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