nexus\api/
log.rs

1//! Logging.
2
3use crate::{util::str_to_c, AddonApi};
4use std::ffi::c_char;
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
7#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
8#[cfg_attr(
9    feature = "strum",
10    derive(
11        strum::AsRefStr,
12        strum::Display,
13        strum::EnumCount,
14        strum::EnumIter,
15        strum::IntoStaticStr,
16        strum::VariantArray,
17        strum::VariantNames
18    )
19)]
20#[repr(C)]
21pub enum LogLevel {
22    Off = 0,
23    Critical = 1,
24    Warning = 2,
25    Info = 3,
26    Debug = 4,
27    Trace = 5,
28    All,
29}
30
31/// Previous log function.
32pub type RawLogOld = unsafe extern "C-unwind" fn(level: LogLevel, message: *const c_char);
33
34pub type RawLog =
35    unsafe extern "C-unwind" fn(level: LogLevel, channel: *const c_char, message: *const c_char);
36
37/// Logs a message to the given channel.
38///
39/// Supports custom coloring for addon window messages, for example `<c=#FF0000>this text is red</c>`.
40#[inline]
41pub fn log(level: LogLevel, channel_name: impl AsRef<str>, message: impl AsRef<str>) {
42    let AddonApi { log, .. } = AddonApi::get();
43    let channel = str_to_c(channel_name, "failed to convert log channel");
44    let message = str_to_c(message, "failed to convert log message");
45    unsafe { log(level, channel.as_ptr(), message.as_ptr()) }
46}