From cb5e57bf275a8998564b830959a843e0fccbc37f Mon Sep 17 00:00:00 2001 From: Congyu Date: Sun, 22 Oct 2023 21:40:31 +0800 Subject: [PATCH] separate file and line in log --- include/socket_manager/socket_manager.h | 6 ++++++ include/socket_manager_c_api.h | 4 ++++ socket_manager/socket_manager.cc | 1 + src/c_api/tracer.rs | 25 +++++++++++++------------ tests/test_utils.h | 5 +++-- 5 files changed, 27 insertions(+), 14 deletions(-) diff --git a/include/socket_manager/socket_manager.h b/include/socket_manager/socket_manager.h index 5150a2a..4ec7c11 100644 --- a/include/socket_manager/socket_manager.h +++ b/include/socket_manager/socket_manager.h @@ -17,9 +17,15 @@ namespace socket_manager { * This structure is used to pass log data from C to C++. */ struct LogData { + // log level SOCKET_MANAGER_C_API_TraceLevel level; + // module name std::string_view target; + // file name (empty if not available) std::string_view file; + // code line (-1 if not available) + int line; + // log message std::string_view message; }; diff --git a/include/socket_manager_c_api.h b/include/socket_manager_c_api.h index a1cfa0d..63b1d4f 100644 --- a/include/socket_manager_c_api.h +++ b/include/socket_manager_c_api.h @@ -209,6 +209,10 @@ struct SOCKET_MANAGER_C_API_LogData { size_t TargetN; const char *File; size_t FileN; + /** + * -1 if not available + */ + int Line; /** * The `message` pointer is only valid for the duration of the callback. */ diff --git a/socket_manager/socket_manager.cc b/socket_manager/socket_manager.cc index 456f0ee..16db18f 100644 --- a/socket_manager/socket_manager.cc +++ b/socket_manager/socket_manager.cc @@ -9,6 +9,7 @@ LogData from_c_log_data(SOCKET_MANAGER_C_API_LogData log_data) { log_data.Level, std::string_view(log_data.Target, log_data.TargetN), std::string_view(log_data.File, log_data.FileN), + log_data.Line, std::string_view(log_data.Message, log_data.MessageN), }; } diff --git a/src/c_api/tracer.rs b/src/c_api/tracer.rs index 3b43a61..074fc49 100644 --- a/src/c_api/tracer.rs +++ b/src/c_api/tracer.rs @@ -2,7 +2,11 @@ use super::utils::write_error_c_str; use crate::init_logger; use libc::size_t; -use std::{ffi::c_char, fmt, ptr::null_mut}; +use std::{ + ffi::{c_char, c_int}, + fmt, + ptr::null_mut, +}; use tracing::{field::Field, Level}; use tracing_subscriber::{filter::LevelFilter, Layer}; @@ -46,6 +50,8 @@ pub struct LogData { pub target_n: size_t, pub file: *const c_char, pub file_n: size_t, + /// -1 if not available + pub line: c_int, /// The `message` pointer is only valid for the duration of the callback. pub message: *const c_char, pub message_n: size_t, @@ -84,23 +90,18 @@ where ) { let mut get_msg = GetMsgVisitor(None); event.record(&mut get_msg); - let file = if let (Some(f), Some(l)) = (event.metadata().file(), event.metadata().line()) { - format!("{}:{}", f, l) - } else { - String::new() - }; + let file = event.metadata().file().unwrap_or(EMPTY); + let line = event.metadata().line().map(|l| l as c_int); + let message = get_msg.0.as_deref().unwrap_or(EMPTY); let data = LogData { level: event.metadata().level().into(), target: event.metadata().target().as_ptr() as *const c_char, target_n: event.metadata().target().len(), file: file.as_ptr() as *const c_char, file_n: file.len(), - message: get_msg - .0 - .as_ref() - .map(|s| s.as_ptr()) - .unwrap_or(EMPTY.as_ptr()) as *const c_char, - message_n: get_msg.0.as_ref().map(|s| s.len()).unwrap_or(0), + line: line.unwrap_or(-1), + message: message.as_ptr() as *const c_char, + message_n: message.len(), }; unsafe { self.0(data) } } diff --git a/tests/test_utils.h b/tests/test_utils.h index e8c3556..3385999 100644 --- a/tests/test_utils.h +++ b/tests/test_utils.h @@ -30,8 +30,9 @@ class SpdLogger { private: static void print_log(SOCKET_MANAGER_C_API_LogData log_data) { socket_manager::LogData data = socket_manager::from_c_log_data(log_data); - spdlog::log(static_cast(data.level), "{}: {} {}", - data.target, data.file, data.message); + spdlog::log(static_cast(data.level), + "{}: {}:{} {}", data.target, data.file, data.line, + data.message); } };