plog.rs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. use hbb_common::log;
  2. use std::ffi::c_char;
  3. const LOG_LEVEL_TRACE: &[u8; 6] = b"trace\0";
  4. const LOG_LEVEL_DEBUG: &[u8; 6] = b"debug\0";
  5. const LOG_LEVEL_INFO: &[u8; 5] = b"info\0";
  6. const LOG_LEVEL_WARN: &[u8; 5] = b"warn\0";
  7. const LOG_LEVEL_ERROR: &[u8; 6] = b"error\0";
  8. #[inline]
  9. fn is_level(level: *const c_char, level_bytes: &[u8]) -> bool {
  10. level_bytes == unsafe { std::slice::from_raw_parts(level as *const u8, level_bytes.len()) }
  11. }
  12. #[no_mangle]
  13. pub(super) extern "C" fn plugin_log(level: *const c_char, msg: *const c_char) {
  14. if level.is_null() || msg.is_null() {
  15. return;
  16. }
  17. if let Ok(msg) = super::cstr_to_string(msg) {
  18. if is_level(level, LOG_LEVEL_TRACE) {
  19. log::trace!("{}", msg);
  20. } else if is_level(level, LOG_LEVEL_DEBUG) {
  21. log::debug!("{}", msg);
  22. } else if is_level(level, LOG_LEVEL_INFO) {
  23. log::info!("{}", msg);
  24. } else if is_level(level, LOG_LEVEL_WARN) {
  25. log::warn!("{}", msg);
  26. } else if is_level(level, LOG_LEVEL_ERROR) {
  27. log::error!("{}", msg);
  28. }
  29. }
  30. }