Skip to content

Commit

Permalink
separate file and line in log
Browse files Browse the repository at this point in the history
  • Loading branch information
Congyuwang committed Oct 22, 2023
1 parent df6d4ea commit cb5e57b
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 14 deletions.
6 changes: 6 additions & 0 deletions include/socket_manager/socket_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down
4 changes: 4 additions & 0 deletions include/socket_manager_c_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
1 change: 1 addition & 0 deletions socket_manager/socket_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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),
};
}
Expand Down
25 changes: 13 additions & 12 deletions src/c_api/tracer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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) }
}
Expand Down
5 changes: 3 additions & 2 deletions tests/test_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<spdlog::level::level_enum>(data.level), "{}: {} {}",
data.target, data.file, data.message);
spdlog::log(static_cast<spdlog::level::level_enum>(data.level),
"{}: {}:{} {}", data.target, data.file, data.line,
data.message);
}
};

Expand Down

0 comments on commit cb5e57b

Please sign in to comment.