log: properly print strings

This commit is contained in:
chayleaf 2024-08-14 09:25:55 +07:00
parent 6a2b2b4c33
commit 81dd179302
Signed by: chayleaf
GPG key ID: 78171AD46227E68E

View file

@ -335,8 +335,15 @@ unsafe extern "C" fn sway_terminate(exit_code: libc::c_int) {
swayidle_finish();
exit(exit_code);
}
unsafe fn read_str(ptr: *const libc::c_char) -> Cow<'static, str> {
if ptr.is_null() {
"".into()
} else {
CStr::from_ptr(ptr).to_string_lossy()
}
}
unsafe extern "C" fn cmd_exec(param: *mut libc::c_char) {
log::debug!("Cmd exec {param:?}",);
log::debug!("Cmd exec {}", read_str(param));
let mut pid: pid_t = fork();
match pid {
0 => {
@ -359,11 +366,11 @@ unsafe extern "C" fn cmd_exec(param: *mut libc::c_char) {
ptr::null_mut(),
];
execvp(cmd[0 as libc::c_int as usize], cmd.as_ptr());
log::error!("execve failed!: {:?}", strerror(*__errno_location()),);
log::error!("execve failed!: {}", strerror(*__errno_location()),);
exit(1 as libc::c_int);
}
pid if pid < 0 => {
log::error!("fork failed: {:?}", strerror(*__errno_location()),);
log::error!("fork failed: {}", strerror(*__errno_location()),);
exit(1 as libc::c_int);
}
_ => {}
@ -371,12 +378,12 @@ unsafe extern "C" fn cmd_exec(param: *mut libc::c_char) {
exit(0 as libc::c_int);
}
pid if pid < 0 => {
log::error!("fork failed: {:?}", strerror(*__errno_location()),);
log::error!("fork failed: {}", strerror(*__errno_location()),);
}
_ => {
log::debug!("Spawned process {param:?}");
log::debug!("Spawned process {}", read_str(param));
if state.wait {
log::debug!("Blocking until process exits",);
log::debug!("Blocking until process exits");
}
let mut status: libc::c_int = 0 as libc::c_int;
waitpid(pid, &mut status, 0 as libc::c_int);
@ -425,27 +432,27 @@ unsafe extern "C" fn acquire_inhibitor_lock(
);
if ret < 0 as libc::c_int {
log::error!(
"Failed to send {:?} inhibit signal: {:?}",
type_0,
error.message,
"Failed to send {} inhibit signal: {}",
read_str(type_0),
read_str(error.message),
);
} else {
ret = sd_bus_message_read(msg, (b"h\0" as *const u8).cast::<libc::c_char>(), fd);
if ret < 0 as libc::c_int {
*__errno_location() = -ret;
log::error!(
"Failed to parse D-Bus response for {:?} inhibit: {:?}",
type_0,
"Failed to parse D-Bus response for {} inhibit: {}",
read_str(type_0),
strerror(*__errno_location()),
);
} else {
*fd = libc::fcntl(*fd, 1030 as libc::c_int, 3 as libc::c_int);
if *fd >= 0 as libc::c_int {
log::debug!("Got {:?} lock: {}", type_0, *fd,);
log::debug!("Got {} lock: {}", read_str(type_0), *fd,);
} else {
log::error!(
"Failed to copy {:?} lock fd: {:?}",
type_0,
"Failed to copy {} lock fd: {}",
read_str(type_0),
strerror(*__errno_location()),
);
}
@ -456,12 +463,12 @@ unsafe extern "C" fn acquire_inhibitor_lock(
}
unsafe extern "C" fn release_inhibitor_lock(fd: libc::c_int) {
if fd >= 0 as libc::c_int {
log::debug!("Releasing inhibitor lock {}", fd,);
log::debug!("Releasing inhibitor lock {}", fd);
close(fd);
}
}
unsafe extern "C" fn set_idle_hint(hint: bool) {
log::debug!("SetIdleHint {}", i32::from(hint),);
log::debug!("SetIdleHint {}", i32::from(hint));
let mut msg: *mut sd_bus_message = ptr::null_mut();
let mut error: sd_bus_error = sd_bus_error {
name: ptr::null(),
@ -480,7 +487,10 @@ unsafe extern "C" fn set_idle_hint(hint: bool) {
i32::from(hint),
);
if ret < 0 as libc::c_int {
log::error!("Failed to send SetIdleHint signal: {:?}", error.message,);
log::error!(
"Failed to send SetIdleHint signal: {}",
read_str(error.message)
);
}
sd_bus_error_free(&mut error);
sd_bus_message_unref(msg);
@ -514,7 +524,7 @@ unsafe extern "C" fn get_logind_idle_inhibit() -> bool {
sd_bus_message_unref(reply);
*__errno_location() = -ret;
log::error!(
"Failed to parse get BlockInhibited property: {:?}",
"Failed to parse get BlockInhibited property: {}",
strerror(*__errno_location())
);
0 as libc::c_int != 0
@ -533,11 +543,11 @@ unsafe extern "C" fn prepare_for_sleep(
if ret < 0 as libc::c_int {
*__errno_location() = -ret;
log::error!(
"Failed to parse D-Bus response for Inhibit: {:?}",
"Failed to parse D-Bus response for Inhibit: {}",
strerror(*__errno_location())
);
}
log::debug!("PrepareForSleep signal received {}", going_down,);
log::debug!("PrepareForSleep signal received {}", going_down);
if going_down == 0 {
acquire_inhibitor_lock(
(b"sleep\0" as *const u8).cast::<libc::c_char>(),
@ -613,7 +623,7 @@ unsafe extern "C" fn handle_property_changed(
(b"org.freedesktop.login1.Manager\0" as *const u8).cast::<libc::c_char>(),
) == 0
{
log::debug!("Got PropertyChanged: {:?}", name);
log::debug!("Got PropertyChanged: {}", read_str(name));
ret = sd_bus_message_enter_container(
msg,
'a' as i32 as libc::c_char,
@ -651,7 +661,7 @@ unsafe extern "C" fn handle_property_changed(
log::debug!("Logind idle inhibitor found");
disable_timeouts();
} else {
log::debug!("Logind idle inhibitor not found",);
log::debug!("Logind idle inhibitor not found");
enable_timeouts();
}
return 0 as libc::c_int;
@ -682,7 +692,7 @@ unsafe extern "C" fn handle_property_changed(
}
*__errno_location() = -ret;
log::error!(
"Failed to parse D-Bus response for PropertyChanged: {:?}",
"Failed to parse D-Bus response for PropertyChanged: {}",
strerror(*__errno_location())
);
0 as libc::c_int
@ -711,7 +721,7 @@ unsafe extern "C" fn dbus_event(
}
if count < 0 as libc::c_int {
log::error!(
"sd_bus_process failed, exiting: {:?}",
"sd_bus_process failed, exiting: {}",
strerror(*__errno_location())
);
sway_terminate(0 as libc::c_int);
@ -739,7 +749,7 @@ unsafe extern "C" fn set_session() {
(b"auto\0" as *const u8).cast::<libc::c_char>(),
);
if ret < 0 as libc::c_int {
log::debug!("GetSession failed: {:?}", error.message);
log::debug!("GetSession failed: {}", read_str(error.message));
sd_bus_error_free(&mut error);
sd_bus_message_unref(msg);
ret = sd_bus_call_method(
@ -754,7 +764,7 @@ unsafe extern "C" fn set_session() {
getpid(),
);
if ret < 0 as libc::c_int {
log::debug!("GetSessionByPID failed: {:?}", error.message);
log::debug!("GetSessionByPID failed: {}", read_str(error.message));
log::error!("Failed to find session");
current_block = 5197767760103794011;
} else {
@ -773,7 +783,7 @@ unsafe extern "C" fn set_session() {
log::error!("Failed to read session name");
} else {
session_name = strdup(session_name_tmp);
log::debug!("Using session: {session_name:?}");
log::debug!("Using session: {}", read_str(session_name));
}
}
sd_bus_error_free(&mut error);
@ -784,7 +794,7 @@ unsafe extern "C" fn connect_to_bus() {
if ret < 0 as libc::c_int {
*__errno_location() = -ret;
log::error!(
"Failed to open D-Bus connection: {:?}",
"Failed to open D-Bus connection: {}",
strerror(*__errno_location()),
);
return;
@ -820,7 +830,7 @@ unsafe extern "C" fn setup_sleep_listener() {
if ret < 0 as libc::c_int {
*__errno_location() = -ret;
log::error!(
"Failed to add D-Bus signal match : sleep: {:?}",
"Failed to add D-Bus signal match : sleep: {}",
strerror(*__errno_location())
);
return;
@ -852,7 +862,7 @@ unsafe extern "C" fn setup_lock_listener() {
if ret < 0 as libc::c_int {
*__errno_location() = -ret;
log::error!(
"Failed to add D-Bus signal match : lock: {:?}",
"Failed to add D-Bus signal match : lock: {}",
strerror(*__errno_location())
);
}
@ -878,7 +888,7 @@ unsafe extern "C" fn setup_unlock_listener() {
if ret < 0 as libc::c_int {
*__errno_location() = -ret;
log::error!(
"Failed to add D-Bus signal match : unlock: {:?}",
"Failed to add D-Bus signal match : unlock: {}",
strerror(*__errno_location())
);
}
@ -904,7 +914,7 @@ unsafe extern "C" fn setup_property_changed_listener() {
if ret < 0 as libc::c_int {
*__errno_location() = -ret;
log::error!(
"Failed to add D-Bus signal match : property changed: {:?}",
"Failed to add D-Bus signal match : property changed: {}",
strerror(*__errno_location())
);
}
@ -992,7 +1002,7 @@ unsafe extern "C" fn register_timeout(cmd: *mut swayidle_timeout_cmd, timeout: l
log::debug!("Not registering idle timeout");
return;
}
log::debug!("Register with timeout: {timeout:?}");
log::debug!("Register with timeout: {timeout}");
(*cmd).idle_notification =
ext_idle_notifier_v1_get_idle_notification(idle_notifier, timeout as u32, seat);
ext_idle_notification_v1_add_listener(
@ -1100,7 +1110,10 @@ unsafe extern "C" fn parse_command(
log::error!("Missing command");
return ptr::null_mut();
}
log::debug!("Command: {:?}", *argv.offset(0 as libc::c_int as isize));
log::debug!(
"Command: {}",
read_str(*argv.offset(0 as libc::c_int as isize))
);
strdup(*argv.offset(0 as libc::c_int as isize))
}
unsafe extern "C" fn build_timeout_cmd(
@ -1116,9 +1129,9 @@ unsafe extern "C" fn build_timeout_cmd(
) as libc::c_int;
if *__errno_location() != 0 as libc::c_int || i32::from(*endptr) != '\0' as i32 {
log::error!(
"Invalid {:?} parameter '{:?}', it should be a numeric value representing seconds",
*argv.offset(0 as libc::c_int as isize),
*argv.offset(1 as libc::c_int as isize),
"Invalid {} parameter '{}', it should be a numeric value representing seconds",
read_str(*argv.offset(0 as libc::c_int as isize)),
read_str(*argv.offset(1 as libc::c_int as isize)),
);
exit(-(1 as libc::c_int));
}
@ -1173,7 +1186,7 @@ unsafe extern "C" fn parse_sleep(argc: libc::c_int, argv: *mut *mut libc::c_char
&mut *argv.offset(1 as libc::c_int as isize),
);
if !(state.before_sleep_cmd).is_null() {
log::debug!("Setup sleep lock: {:?}", state.before_sleep_cmd);
log::debug!("Setup sleep lock: {}", read_str(state.before_sleep_cmd));
}
2 as libc::c_int
}
@ -1187,7 +1200,7 @@ unsafe extern "C" fn parse_resume(argc: libc::c_int, argv: *mut *mut libc::c_cha
&mut *argv.offset(1 as libc::c_int as isize),
);
if !(state.after_resume_cmd).is_null() {
log::debug!("Setup resume hook: {:?}", state.after_resume_cmd,);
log::debug!("Setup resume hook: {}", read_str(state.after_resume_cmd));
}
2 as libc::c_int
}
@ -1201,7 +1214,7 @@ unsafe extern "C" fn parse_lock(argc: libc::c_int, argv: *mut *mut libc::c_char)
&mut *argv.offset(1 as libc::c_int as isize),
);
if !(state.logind_lock_cmd).is_null() {
log::debug!("Setup lock hook: {:?}", state.logind_lock_cmd,);
log::debug!("Setup lock hook: {}", read_str(state.logind_lock_cmd));
}
2 as libc::c_int
}
@ -1215,7 +1228,7 @@ unsafe extern "C" fn parse_unlock(argc: libc::c_int, argv: *mut *mut libc::c_cha
&mut *argv.offset(1 as libc::c_int as isize),
);
if !(state.logind_unlock_cmd).is_null() {
log::debug!("Setup unlock hook: {:?}", state.logind_unlock_cmd,);
log::debug!("Setup unlock hook: {}", read_str(state.logind_unlock_cmd));
}
2 as libc::c_int
}
@ -1331,7 +1344,10 @@ unsafe extern "C" fn parse_args(
log::debug!("Got idlehint");
i += parse_idlehint(argc - i, &mut *argv.offset(i as isize));
} else {
log::error!("Unsupported command '{:?}'", *argv.offset(i as isize),);
log::error!(
"Unsupported command '{}'",
read_str(*argv.offset(i as isize))
);
return 1 as libc::c_int;
}
}
@ -1409,7 +1425,7 @@ unsafe extern "C" fn display_event(
}
if count < 0 as libc::c_int {
log::error!(
"wl_display_dispatch failed, exiting: {:?}",
"wl_display_dispatch failed, exiting: {}",
strerror(*__errno_location()),
);
sway_terminate(0 as libc::c_int);
@ -1522,7 +1538,7 @@ unsafe extern "C" fn load_config(config_path: *const libc::c_char) -> libc::c_in
parse_idlehint(p.we_wordc as libc::c_int, p.we_wordv);
} else {
*line.add(i) = 0 as libc::c_int as libc::c_char;
log::error!("Unexpected keyword {line:?} in line {lineno}");
log::error!("Unexpected keyword {} in line {lineno}", read_str(line));
free(line.cast::<libc::c_void>());
return -(22 as libc::c_int);
}
@ -1551,10 +1567,10 @@ unsafe fn main_0(argc: libc::c_int, argv: *mut *mut libc::c_char) -> libc::c_int
if config_load == -(2 as libc::c_int) {
log::debug!("No config file found.");
} else if config_load == -(22 as libc::c_int) {
log::error!("Config file {config_path:?} has errors, exiting.");
log::error!("Config file {} has errors, exiting.", read_str(config_path));
exit(-(1 as libc::c_int));
} else {
log::debug!("Loaded config at {config_path:?}",);
log::debug!("Loaded config at {}", read_str(config_path));
}
free(config_path.cast::<libc::c_void>());
state.event_loop = wl_event_loop_create();
@ -1619,7 +1635,7 @@ unsafe fn main_0(argc: libc::c_int, argv: *mut *mut libc::c_char) -> libc::c_int
if state.seat_name.is_null() {
log::error!("No seat found");
} else {
log::error!("Seat {:?} not found", state.seat_name,);
log::error!("Seat {} not found", read_str(state.seat_name));
}
swayidle_finish();
return -(5 as libc::c_int);