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
#![crate_type = "lib"]
use std::{
    io::Write,
    net::{TcpStream, Ipv4Addr},
    time::{SystemTime, UNIX_EPOCH},
    error::Error, fmt::{self, Display},
};

use chrono::{DateTime, Utc};
use serde::{Serialize, Deserialize};
use serde_json::json;

struct Connection {
    ip: &'static str, //todo: accept ipv4, ipv6, or web domain
    port: u16,
}

impl Connection {
    //todo
    //default port of 3306
    //ip parameter should be able to take ipv4, ipv6, and web domains

    fn default(&self) -> Self {
        Connection { ip: "255.255.255.255", port: 3306 }
    }

}

struct Logger {
    id: u32,
    connection: Option<Connection>,
}

impl Logger {
    //todo
}

// Struct representing a log message.
#[derive(Serialize, Deserialize)]
struct LogMessage {
    timestamp: DateTime<Utc>,
    level: Level,
    message: String,
}

// Enum laying out possible message types
#[derive(Serialize, Deserialize, Clone)]
enum Level {
    INFO,
    WARNING,
    ERROR,
    DEBUG,
    Custom(String),
}

impl Display for Level {

    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Level::INFO => write!(f, "INFO"),
            Level::WARNING => write!(f, "WARNING"),
            Level::ERROR => write!(f, "ERROR"),
            Level::DEBUG => write!(f, "DEBUG"),
            Level::Custom(str) => write!(f, "{}", str),
        }
    }

}

// Function to send a log message to the graylog server.
fn send_log_message(log_message: &LogMessage) -> Result<(), Box<dyn Error>> {
    // Establish a connection to the graylog server.
    let mut stream = TcpStream::connect("graylog.example.com:12201")?;

    // Serialize the log message as JSON.
    let json_string = serde_json::to_string(log_message)?;

    // Write the serialized JSON to the stream.
    stream.write_all(json_string.as_bytes())?;
    Ok(())
}

// Function to create and send a log message with the specified message and level.
fn log(message: &str, level: Level) -> Result<(), Box<dyn Error>> {
    // Create a new log message with the current timestamp.
    let log_message = LogMessage {
        message: message.to_string(),
        timestamp: Utc::now(),
        level,
    };

    // Send the log message to the graylog server.
    send_log_message(&log_message)
}

// // Example usage of the logger.
// fn main() -> Result<(), Box<dyn Error>> {
    
//     log("Hello, world!", Level::INFO)?;
    
//     Ok(())
// }

#[test]
fn test_levels() {

    assert_eq!("INFO", Level::INFO.to_string());
    assert_eq!("WARNING", Level::WARNING.to_string());
    assert_eq!("ERROR", Level::ERROR.to_string());
    assert_eq!("DEBUG", Level::DEBUG.to_string());

}