use cpp::cpp;
cpp! {{
#ifndef ODDITY_FFI_LOGGER
#define ODDITY_FFI_LOGGER
#include <mutex>
class Logger : public ILogger
{
public:
void log(Severity severity, const char* msg) noexcept override {
if (severity == Severity::kERROR || severity == Severity::kINTERNAL_ERROR) {
std::lock_guard<std::mutex> _lastErrorGuard(m_lastErrorMutex);
m_lastError = std::string(msg);
}
std::int32_t severity_val = static_cast<std::int32_t>(severity);
rust!(Logger_handleLogMessage [
severity_val : i32 as "std::int32_t",
msg : *const std::os::raw::c_char as "const char*"
] {
handle_log_message_raw(severity_val, msg);
});
}
const std::string getLastError() {
std::lock_guard<std::mutex> _lastErrorGuard(m_lastErrorMutex);
return m_lastError;
}
private:
std::mutex m_lastErrorMutex {};
std::string m_lastError = "";
}
GLOBAL_LOGGER;
#endif }}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Severity {
InternalError,
Error,
Warning,
Info,
Verbose,
Unknown,
}
impl From<i32> for Severity {
fn from(value: i32) -> Self {
match value {
0 => Severity::InternalError,
1 => Severity::Error,
2 => Severity::Warning,
3 => Severity::Info,
4 => Severity::Verbose,
_ => Severity::Unknown,
}
}
}
unsafe fn handle_log_message_raw(severity: i32, msg: *const std::os::raw::c_char) {
let msg_c_str: &std::ffi::CStr = std::ffi::CStr::from_ptr(msg);
let msg = msg_c_str.to_str().unwrap_or("");
if !msg.is_empty() {
match severity.into() {
Severity::InternalError | Severity::Error => {
tracing::error!(target: "tensorrt", "{msg}");
}
Severity::Warning => {
tracing::warn!(target: "tensorrt", "{msg}");
}
Severity::Info => {
tracing::trace!(target: "tensorrt", "{msg}");
}
_ => {}
}
}
}