use std::os::raw::{c_int, c_char};
use crate::pg_sys;
const ERR_DOMAIN: &[u8] = b"RUST\0";
#[derive(Clone, Copy)]
pub enum Level {
Debug5 = pg_sys::DEBUG5 as isize,
Debug4 = pg_sys::DEBUG4 as isize,
Debug3 = pg_sys::DEBUG3 as isize,
Debug2 = pg_sys::DEBUG2 as isize,
Debug1 = pg_sys::DEBUG1 as isize,
Log = pg_sys::LOG as isize,
LogServerOnly = pg_sys::LOG_SERVER_ONLY as isize,
Info = pg_sys::INFO as isize,
Notice = pg_sys::NOTICE as isize,
Warning = pg_sys::WARNING as isize,
Error = pg_sys::ERROR as isize,
Fatal = pg_sys::FATAL as isize,
Panic = pg_sys::PANIC as isize,
}
impl From<Level> for c_int {
fn from(level: Level) -> Self {
level as isize as c_int
}
}
pub fn log<T1, T2, T3>(level: Level, file: T1, line: u32, func_name: T2, msg: T3)
where
T1: Into<Vec<u8>>,
T2: Into<Vec<u8>>,
T3: Into<Vec<u8>>,
{
use std::ffi::CString;
let file = CString::new(file.into()).expect("this should not fail: file");
let line = line as c_int; let func_name = CString::new(func_name.into()).expect("this should not fail: func_name");
let msg = CString::new(msg.into()).or_else(|_| CString::new("failed to convert msg to a CString, check extension code for incompatibly `CString` messages")).expect("this should not fail: msg");
let file: *const c_char = file.as_ptr();
let func_name: *const c_char = func_name.as_ptr();
let msg: *const c_char = msg.as_ptr();
let errlevel: c_int = c_int::from(level);
unsafe {
if pg_sys::errstart(errlevel, file, line, func_name, ERR_DOMAIN.as_ptr() as *const c_char) {
let msg_result = pg_sys::errmsg(msg);
pg_sys::errfinish(msg_result);
}
}
}