loglink 0.1.0

Send logs to a remote server
Documentation
use serde::{Deserialize, Serialize, de::DeserializeOwned};
use std::time::{SystemTime, UNIX_EPOCH};

use crate::{LogLevel, ToPayload};

#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct LogEvent<T>
where
    T: ToPayload,
{
    pub level: LogLevel,
    pub timestamp: u64, // millis since epoch
    #[serde(bound(deserialize = "T: DeserializeOwned"))]
    pub payload: T,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tag: Option<String>,
}

unsafe impl<T: ToPayload> Send for LogEvent<T> {}

impl<T: ToPayload> LogEvent<T> {
    pub fn new(level: LogLevel, payload: T) -> Self {
        let timestamp = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_millis() as u64;

        Self {
            tag: None,
            level,
            timestamp,
            payload,
        }
    }

    pub(crate) fn new_with_tag_opt(level: LogLevel, payload: T, tag: Option<String>) -> Self {
        let timestamp = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_millis() as u64;

        Self {
            tag,
            level,
            timestamp,
            payload,
        }
    }
}