From 5a7b7bceb51fd5fbebd10ba6a2ac038bd5ebb96d Mon Sep 17 00:00:00 2001 From: falkTX Date: Sat, 11 May 2024 01:38:43 +0200 Subject: [PATCH] custom log names for ipc errors Signed-off-by: falkTX --- src/ipc/ipc.h | 18 +++++++++++------- src/ipc/ipc_proc.h | 20 ++++++++++++-------- src/ipc/ipc_ring.h | 8 ++++++-- src/ipc/ipc_sem.h | 6 +++++- src/ipc/ipc_shm.h | 22 +++++++++++++--------- src/ui-client.c | 1 + src/ui-server.c | 1 + 7 files changed, 49 insertions(+), 27 deletions(-) diff --git a/src/ipc/ipc.h b/src/ipc/ipc.h index 601db18..0835379 100644 --- a/src/ipc/ipc.h +++ b/src/ipc/ipc.h @@ -3,6 +3,10 @@ #pragma once +#ifndef IPC_LOG_NAME + #define IPC_LOG_NAME "ipc" +#endif + #include "ipc_proc.h" #include "ipc_ring.h" #include "ipc_sem.h" @@ -115,7 +119,7 @@ ipc_server_t* ipc_server_start(const char* args[], const char* const name, const ipc_server_t* const server = (ipc_server_t*)calloc(1, sizeof(ipc_server_t)); if (server == NULL) { - fprintf(stderr, "[ipc] ipc_process_start failed: out of memory\n"); + fprintf(stderr, "[" IPC_LOG_NAME "] ipc_process_start failed: out of memory\n"); return NULL; } @@ -123,7 +127,7 @@ ipc_server_t* ipc_server_start(const char* args[], const char* const name, const if (! ipc_shm_server_create(&server->shm, name, shared_data_size, false)) { - fprintf(stderr, "[ipc] ipc_process_start failed: could not create shared memory segment\n"); + fprintf(stderr, "[" IPC_LOG_NAME "] ipc_process_start failed: could not create shared memory segment\n"); free(server); return NULL; } @@ -139,7 +143,7 @@ ipc_server_t* ipc_server_start(const char* args[], const char* const name, const if (! ipc_sem_create(&shared_data->sem_server)) { - fprintf(stderr, "[ipc] ipc_sem_create failed\n"); + fprintf(stderr, "[" IPC_LOG_NAME "] ipc_sem_create failed\n"); ipc_shm_server_destroy(&server->shm); free(server); return NULL; @@ -147,7 +151,7 @@ ipc_server_t* ipc_server_start(const char* args[], const char* const name, const if (! ipc_sem_create(&shared_data->sem_client)) { - fprintf(stderr, "[ipc] ipc_sem_create failed\n"); + fprintf(stderr, "[" IPC_LOG_NAME "] ipc_sem_create failed\n"); ipc_sem_destroy(&shared_data->sem_server); ipc_shm_server_destroy(&server->shm); free(server); @@ -170,7 +174,7 @@ ipc_server_t* ipc_server_start(const char* args[], const char* const name, const return server; } - fprintf(stderr, "[ipc] client side failed to start\n"); + fprintf(stderr, "[" IPC_LOG_NAME "] client side failed to start\n"); ipc_server_stop(server); return NULL; } @@ -240,7 +244,7 @@ ipc_client_t* ipc_client_attach(const char* const name, const uint32_t rbsize) if (client == NULL) { - fprintf(stderr, "[ipc] ipc_client_attach failed: out of memory\n"); + fprintf(stderr, "[" IPC_LOG_NAME "] ipc_client_attach failed: out of memory\n"); return NULL; } @@ -248,7 +252,7 @@ ipc_client_t* ipc_client_attach(const char* const name, const uint32_t rbsize) if (! ipc_shm_client_attach(&client->shm, name, shared_data_size, false)) { - fprintf(stderr, "[ipc] ipc_client_attach failed: could not attach shared memory segment\n"); + fprintf(stderr, "[" IPC_LOG_NAME "] ipc_client_attach failed: could not attach shared memory segment\n"); free(client); return NULL; } diff --git a/src/ipc/ipc_proc.h b/src/ipc/ipc_proc.h index 245f621..b6536fb 100644 --- a/src/ipc/ipc_proc.h +++ b/src/ipc/ipc_proc.h @@ -3,6 +3,10 @@ #pragma once +#ifndef IPC_LOG_NAME + #define IPC_LOG_NAME "ipc" +#endif + #ifdef __cplusplus #define IPC_STRUCT_INIT {} #include @@ -48,7 +52,7 @@ ipc_proc_t* ipc_proc_start(const char* const args[]) #else if (access(args[0], X_OK) != 0) { - fprintf(stderr, "[ipc] cannot exec: %s\n", strerror(errno)); + fprintf(stderr, "[" IPC_LOG_NAME "] cannot exec: %s\n", strerror(errno)); return NULL; } #endif @@ -57,7 +61,7 @@ ipc_proc_t* ipc_proc_start(const char* const args[]) ipc_proc_t* volatile const proc = (ipc_proc_t*)calloc(1, sizeof(ipc_proc_t)); if (proc == NULL) { - fprintf(stderr, "[ipc] ipc_proc_start failed: out of memory\n"); + fprintf(stderr, "[" IPC_LOG_NAME "] ipc_proc_start failed: out of memory\n"); return NULL; } @@ -74,7 +78,7 @@ ipc_proc_t* ipc_proc_start(const char* const args[]) wchar_t* const cmd = (wchar_t*)malloc(sizeof(wchar_t) * cmdlen); if (cmd == NULL) { - fprintf(stderr, "[ipc] ipc_proc_start failed: out of memory\n"); + fprintf(stderr, "[" IPC_LOG_NAME "] ipc_proc_start failed: out of memory\n"); free(proc); return NULL; } @@ -93,7 +97,7 @@ ipc_proc_t* ipc_proc_start(const char* const args[]) const DWORD wrtn = MultiByteToWideChar(CP_UTF8, 0, args[i], -1, cmdptr, cmdlen - (cmdptr - cmd) / sizeof(wchar_t)); if (wrtn <= 0) { - fprintf(stderr, "[ipc] ipc_proc_start failed: %s\n", StrError(GetLastError())); + fprintf(stderr, "[" IPC_LOG_NAME "] ipc_proc_start failed: %s\n", StrError(GetLastError())); free(cmd); free(proc); return NULL; @@ -107,7 +111,7 @@ ipc_proc_t* ipc_proc_start(const char* const args[]) *cmdptr = 0; - fprintf(stderr, "[ipc] ipc_proc_start trying to launch '%ls'\n", cmd); + fprintf(stderr, "[" IPC_LOG_NAME "] ipc_proc_start trying to launch '%ls'\n", cmd); STARTUPINFOW si = IPC_STRUCT_INIT; si.cb = sizeof(si); @@ -129,13 +133,13 @@ ipc_proc_t* ipc_proc_start(const char* const args[]) // child process case 0: execvp(args[0], (char* const*)args); - fprintf(stderr, "[ipc] exec failed: %s\n", strerror(errno)); + fprintf(stderr, "[" IPC_LOG_NAME "] exec failed: %s\n", strerror(errno)); _exit(1); return NULL; // error case -1: - fprintf(stderr, "[ipc] vfork failed: %s\n", strerror(errno)); + fprintf(stderr, "[" IPC_LOG_NAME "] vfork failed: %s\n", strerror(errno)); free(proc); return NULL; } @@ -228,7 +232,7 @@ void ipc_proc_stop(ipc_proc_t* const proc) break; } - fprintf(stderr, "[ipc] waitpid failed: %s\n", strerror(errno)); + fprintf(stderr, "[" IPC_LOG_NAME "] waitpid failed: %s\n", strerror(errno)); return; } #endif diff --git a/src/ipc/ipc_ring.h b/src/ipc/ipc_ring.h index 9cd0f6c..e39fafd 100644 --- a/src/ipc/ipc_ring.h +++ b/src/ipc/ipc_ring.h @@ -3,6 +3,10 @@ #pragma once +#ifndef IPC_LOG_NAME + #define IPC_LOG_NAME "ipc" +#endif + #ifdef __cplusplus #include #include @@ -69,7 +73,7 @@ bool ipc_ring_read(ipc_ring_t* ring, void* dst, uint32_t size) if ((ring->flags & ipc_ring_flag_error_reading) == 0) { ring->flags |= ipc_ring_flag_error_reading; - fprintf(stderr, "[ipc] ipc_ring_read failed: not enough space\n"); + fprintf(stderr, "[" IPC_LOG_NAME "] ipc_ring_read failed: not enough space\n"); } return false; } @@ -122,7 +126,7 @@ bool ipc_ring_write(ipc_ring_t* ring, const void* src, uint32_t size) if ((ring->flags & ipc_ring_flag_error_writing) == 0) { ring->flags |= ipc_ring_flag_error_writing; - fprintf(stderr, "[ipc] ipc_ring_write failed: not enough space\n"); + fprintf(stderr, "[" IPC_LOG_NAME "] ipc_ring_write failed: not enough space\n"); } ring->flags |= ipc_ring_flag_invalidate_commit; return false; diff --git a/src/ipc/ipc_sem.h b/src/ipc/ipc_sem.h index eb6b12a..6b2c67f 100644 --- a/src/ipc/ipc_sem.h +++ b/src/ipc/ipc_sem.h @@ -3,6 +3,10 @@ #pragma once +#ifndef IPC_LOG_NAME + #define IPC_LOG_NAME "ipc" +#endif + #ifdef __cplusplus #include #include @@ -68,7 +72,7 @@ bool ipc_sem_create(ipc_sem_t* const sem) #else if (sem_init(sem, 1, 0) == 0) return true; - fprintf(stderr, "[ipc] sem_init failed: %s\n", strerror(errno)); + fprintf(stderr, "[" IPC_LOG_NAME "] sem_init failed: %s\n", strerror(errno)); return false; #endif } diff --git a/src/ipc/ipc_shm.h b/src/ipc/ipc_shm.h index 3fa8a15..c69e683 100644 --- a/src/ipc/ipc_shm.h +++ b/src/ipc/ipc_shm.h @@ -3,6 +3,10 @@ #pragma once +#ifndef IPC_LOG_NAME + #define IPC_LOG_NAME "ipc" +#endif + #ifdef __cplusplus #define IPC_STRUCT_INIT {} #include @@ -101,14 +105,14 @@ bool ipc_shm_server_create(ipc_shm_server_t* const shm, const char* const name, shm->handle = CreateFileMappingA(INVALID_HANDLE_VALUE, &sa, PAGE_READWRITE|SEC_COMMIT, 0, (DWORD)size, shmname); if (shm->handle == NULL) { - fprintf(stderr, "[ipc] CreateFileMapping failed: %s\n", StrError(GetLastError())); + fprintf(stderr, "[" IPC_LOG_NAME "] CreateFileMapping failed: %s\n", StrError(GetLastError())); return false; } shm->ptr = (uint8_t*)MapViewOfFile(shm->handle, FILE_MAP_ALL_ACCESS, 0, 0, size); if (shm->ptr == NULL) { - fprintf(stderr, "[ipc] MapViewOfFile failed: %s\n", StrError(GetLastError())); + fprintf(stderr, "[" IPC_LOG_NAME "] MapViewOfFile failed: %s\n", StrError(GetLastError())); CloseHandle(shm->handle); return false; } @@ -119,13 +123,13 @@ bool ipc_shm_server_create(ipc_shm_server_t* const shm, const char* const name, shm->fd = shm_open(shmname, O_CREAT|O_EXCL|O_RDWR, 0666); if (shm->fd < 0) { - fprintf(stderr, "[ipc] shm_open failed: %s\n", strerror(errno)); + fprintf(stderr, "[" IPC_LOG_NAME "] shm_open failed: %s\n", strerror(errno)); return false; } if (ftruncate(shm->fd, (off_t)size) != 0) { - fprintf(stderr, "[ipc] ftruncate failed: %s\n", strerror(errno)); + fprintf(stderr, "[" IPC_LOG_NAME "] ftruncate failed: %s\n", strerror(errno)); close(shm->fd); shm_unlink(shmname); return false; @@ -147,7 +151,7 @@ bool ipc_shm_server_create(ipc_shm_server_t* const shm, const char* const name, if (shm->ptr == NULL || shm->ptr == MAP_FAILED) { - fprintf(stderr, "[ipc] mmap failed: %s\n", strerror(errno)); + fprintf(stderr, "[" IPC_LOG_NAME "] mmap failed: %s\n", strerror(errno)); close(shm->fd); shm_unlink(shmname); return false; @@ -188,14 +192,14 @@ bool ipc_shm_client_attach(ipc_shm_client_t* const shm, const char* const name, shm->handle = OpenFileMappingA(FILE_MAP_ALL_ACCESS, FALSE, shmname); if (shm->handle == NULL) { - fprintf(stderr, "[ipc] OpenFileMapping failed: %s\n", StrError(GetLastError())); + fprintf(stderr, "[" IPC_LOG_NAME "] OpenFileMapping failed: %s\n", StrError(GetLastError())); return false; } shm->ptr = (uint8_t*)MapViewOfFile(shm->handle, FILE_MAP_ALL_ACCESS, 0, 0, size); if (shm->ptr == NULL) { - fprintf(stderr, "[ipc] MapViewOfFile failed: %s\n", StrError(GetLastError())); + fprintf(stderr, "[" IPC_LOG_NAME "] MapViewOfFile failed: %s\n", StrError(GetLastError())); CloseHandle(shm->handle); return false; } @@ -206,7 +210,7 @@ bool ipc_shm_client_attach(ipc_shm_client_t* const shm, const char* const name, shm->fd = shm_open(shmname, O_RDWR, 0); if (shm->fd < 0) { - fprintf(stderr, "[ipc] shm_open failed: %s\n", strerror(errno)); + fprintf(stderr, "[" IPC_LOG_NAME "] shm_open failed: %s\n", strerror(errno)); return false; } @@ -226,7 +230,7 @@ bool ipc_shm_client_attach(ipc_shm_client_t* const shm, const char* const name, if (shm->ptr == NULL || shm->ptr == MAP_FAILED) { - fprintf(stderr, "[ipc] mmap failed: %s\n", strerror(errno)); + fprintf(stderr, "[" IPC_LOG_NAME "] mmap failed: %s\n", strerror(errno)); close(shm->fd); return false; } diff --git a/src/ui-client.c b/src/ui-client.c index 564115c..9a7e6f5 100644 --- a/src/ui-client.c +++ b/src/ui-client.c @@ -1,6 +1,7 @@ // Copyright 2024 Filipe Coelho // SPDX-License-Identifier: ISC +#define IPC_LOG_NAME "ipc-client" #include "ui-base.h" #include diff --git a/src/ui-server.c b/src/ui-server.c index 192fbee..e27a26c 100644 --- a/src/ui-server.c +++ b/src/ui-server.c @@ -1,6 +1,7 @@ // Copyright 2024 Filipe Coelho // SPDX-License-Identifier: ISC +#define IPC_LOG_NAME "ipc-server" #include "ui-base.h" #include