use crate::ExportError;
use futures_channel::{mpsc::TrySendError, oneshot::Canceled};
use std::time::Duration;
use thiserror::Error;
mod logger;
mod noop;
mod record;
pub use logger::{Logger, LoggerProvider};
pub use noop::NoopLoggerProvider;
pub use record::{AnyValue, LogRecord, LogRecordBuilder, Severity, TraceContext};
pub type LogResult<T> = Result<T, LogError>;
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum LogError {
#[error("Exporter {} encountered the following errors: {0}", .0.exporter_name())]
ExportFailed(Box<dyn ExportError>),
#[error("Exporter timed out after {} seconds", .0.as_secs())]
ExportTimedOut(Duration),
#[error(transparent)]
Other(#[from] Box<dyn std::error::Error + Send + Sync + 'static>),
}
impl<T> From<T> for LogError
where
T: ExportError,
{
fn from(err: T) -> Self {
LogError::ExportFailed(Box::new(err))
}
}
impl<T> From<TrySendError<T>> for LogError {
fn from(err: TrySendError<T>) -> Self {
LogError::Other(Box::new(err.into_send_error()))
}
}
impl From<Canceled> for LogError {
fn from(err: Canceled) -> Self {
LogError::Other(Box::new(err))
}
}
impl From<String> for LogError {
fn from(err_msg: String) -> Self {
LogError::Other(Box::new(Custom(err_msg)))
}
}
impl From<&'static str> for LogError {
fn from(err_msg: &'static str) -> Self {
LogError::Other(Box::new(Custom(err_msg.into())))
}
}
#[derive(Error, Debug)]
#[error("{0}")]
struct Custom(String);