Replace all perror() calls with LOG()

This commit is contained in:
Thomas Kolb 2024-05-30 11:00:34 +02:00
parent 65daa182ef
commit 0e8e049e0a
6 changed files with 44 additions and 37 deletions

View file

@ -7,10 +7,11 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include "logger.h"
#include "jsonlogger.h" #include "jsonlogger.h"
#define LOG_OR_RETURN(...) \ #define LOG_OR_RETURN(...) \
@ -28,7 +29,7 @@ static bool start_message(void)
m_fifo_fd = open(m_fifo_path, O_WRONLY | O_NONBLOCK); m_fifo_fd = open(m_fifo_path, O_WRONLY | O_NONBLOCK);
if(m_fifo_fd < 0) { if(m_fifo_fd < 0) {
if(errno != ENXIO) { if(errno != ENXIO) {
perror("open"); LOG(LVL_ERR, "open: %s", strerror(errno));
} }
return false; return false;
} }
@ -36,7 +37,7 @@ static bool start_message(void)
// message to prevent corrupted JSON messages. 1048576 is the // message to prevent corrupted JSON messages. 1048576 is the
// current maximum as permitted by the Linux kernel. // current maximum as permitted by the Linux kernel.
if (fcntl(m_fifo_fd, F_SETPIPE_SZ, 1048576) < 0) { if (fcntl(m_fifo_fd, F_SETPIPE_SZ, 1048576) < 0) {
perror("fcntl"); LOG(LVL_ERR, "fcntl: %s", strerror(errno));
} }
} }
@ -53,7 +54,7 @@ static bool start_message(void)
break; break;
default: // other errors are logged to stderr default: // other errors are logged to stderr
perror("write"); LOG(LVL_ERR, "write: %s", strerror(errno));
break; break;
} }
@ -69,7 +70,7 @@ static bool end_message(void)
if(ret < 0) { if(ret < 0) {
// FIFO is not writable or other error // FIFO is not writable or other error
if(errno != EAGAIN) { if(errno != EAGAIN) {
perror("write"); LOG(LVL_ERR, "write: %s", strerror(errno));
} }
return false; return false;
@ -102,7 +103,7 @@ bool jsonlogger_init(const char *fifoname)
int ret = mkfifo(fifoname, 0666); int ret = mkfifo(fifoname, 0666);
if(ret < 0) { if(ret < 0) {
if(errno != EEXIST) { if(errno != EEXIST) {
perror("mkfifo"); LOG(LVL_ERR, "mkfifo: %s", strerror(errno));
return false; return false;
} }
} }

View file

@ -5,7 +5,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <stdio.h> #include <errno.h>
#include <string.h> #include <string.h>
#include "logger.h" #include "logger.h"
@ -16,7 +16,7 @@ int tundev_open(char *dev)
{ {
int tunfd = open("/dev/net/tun", O_RDWR); int tunfd = open("/dev/net/tun", O_RDWR);
if(tunfd < 0) { if(tunfd < 0) {
perror("tundev: open"); LOG(LVL_ERR, "tundev: open: %s", strerror(errno));
return -1; return -1;
} }
@ -29,7 +29,7 @@ int tundev_open(char *dev)
int ret = ioctl(tunfd, TUNSETIFF, &ifr); int ret = ioctl(tunfd, TUNSETIFF, &ifr);
if(ret < 0) { if(ret < 0) {
perror("tundev: ioctl"); LOG(LVL_ERR, "tundev: ioctl: %s", strerror(errno));
goto error; goto error;
} }
@ -80,7 +80,7 @@ result_t tundev_read_loop(int tunfd, tundev_read_callback_t callback)
LOG(LVL_DEBUG, "tundev: no more data."); LOG(LVL_DEBUG, "tundev: no more data.");
return OK; return OK;
} else { } else {
perror("tundev: read"); LOG(LVL_ERR, "tundev: read: %s", strerror(errno));
return ERR_SYSCALL; return ERR_SYSCALL;
} }
} }

View file

@ -11,6 +11,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <poll.h> #include <poll.h>
#include <signal.h> #include <signal.h>
#include <errno.h>
#include "utils.h" #include "utils.h"
#include "logger.h" #include "logger.h"
@ -139,7 +140,7 @@ void cb_rx(rx_evt_t evt, const struct layer1_rx_s *rx, uint8_t *packet_data, siz
ret = write(m_tunfd, packet_data, packet_len); ret = write(m_tunfd, packet_data, packet_len);
if(ret < 0) { if(ret < 0) {
perror("write"); LOG(LVL_ERR, "write: %s", strerror(errno));
} }
break; break;
@ -219,12 +220,12 @@ int main(int argc, char **argv)
term_action.sa_sigaction = signal_handler; term_action.sa_sigaction = signal_handler;
if(sigaction(SIGTERM, &term_action, NULL) < 0) { if(sigaction(SIGTERM, &term_action, NULL) < 0) {
perror("sigaction"); LOG(LVL_ERR, "sigaction: %s", strerror(errno));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if(sigaction(SIGINT, &term_action, NULL) < 0) { if(sigaction(SIGINT, &term_action, NULL) < 0) {
perror("sigaction"); LOG(LVL_ERR, "sigaction: %s", strerror(errno));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -251,7 +252,7 @@ int main(int argc, char **argv)
if((now > next_tx_switch_time) && (on_air || !layer1_rx_is_busy(&rx))) { if((now > next_tx_switch_time) && (on_air || !layer1_rx_is_busy(&rx))) {
int ret = poll(&pfd, 1, 0); int ret = poll(&pfd, 1, 0);
if(ret < 0) { if(ret < 0) {
perror("poll"); LOG(LVL_ERR, "poll: %s", strerror(errno));
break; break;
} else if(ret > 0) { } else if(ret > 0) {
// there is a packet to be read. // there is a packet to be read.
@ -270,7 +271,7 @@ int main(int argc, char **argv)
uint8_t packetbuf[2048]; uint8_t packetbuf[2048];
ret = read(m_tunfd, packetbuf, sizeof(packetbuf)); ret = read(m_tunfd, packetbuf, sizeof(packetbuf));
if(ret < 0) { if(ret < 0) {
perror("read"); LOG(LVL_ERR, "read: %s", strerror(errno));
break; break;
} else if(ret == 0) { } else if(ret == 0) {
// no more data // no more data
@ -300,7 +301,7 @@ int main(int argc, char **argv)
int ret2 = poll(&pfd, 1, 0); int ret2 = poll(&pfd, 1, 0);
if(ret2 < 0) { if(ret2 < 0) {
perror("poll"); LOG(LVL_ERR, "poll: %s", strerror(errno));
break; break;
} else if(ret2 != 0 && (buffer_used_samples < min_required_samples)) { } else if(ret2 != 0 && (buffer_used_samples < min_required_samples)) {
// enqueue more packets before starting TX // enqueue more packets before starting TX

View file

@ -5,6 +5,7 @@
#include <math.h> #include <math.h>
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
#include <errno.h>
#include "config.h" #include "config.h"
#include "utils.h" #include "utils.h"
@ -28,7 +29,7 @@ static int rx_callback(hackrf_transfer *transfer)
size_t nsamples = transfer->valid_length / 2; size_t nsamples = transfer->valid_length / 2;
if(sem_wait(&sdr_ctx->buf_sem) < 0) { if(sem_wait(&sdr_ctx->buf_sem) < 0) {
perror("sem_wait failed. Samples lost. Error:"); LOG(LVL_ERR, "sem_wait failed. Samples lost. Error:: %s", strerror(errno));
return HACKRF_ERROR_OTHER; return HACKRF_ERROR_OTHER;
} }
@ -44,7 +45,7 @@ static int rx_callback(hackrf_transfer *transfer)
} }
if(sem_post(&sdr_ctx->buf_sem) < 0) { if(sem_post(&sdr_ctx->buf_sem) < 0) {
perror("sem_post"); LOG(LVL_ERR, "sem_post: %s", strerror(errno));
return HACKRF_ERROR_OTHER; return HACKRF_ERROR_OTHER;
} }
@ -77,7 +78,7 @@ static int tx_callback(hackrf_transfer *transfer)
} }
if(sem_wait(&sdr_ctx->buf_sem) < 0) { if(sem_wait(&sdr_ctx->buf_sem) < 0) {
perror("sem_wait"); LOG(LVL_ERR, "sem_wait: %s", strerror(errno));
return HACKRF_ERROR_OTHER; return HACKRF_ERROR_OTHER;
} }
@ -123,7 +124,7 @@ static int tx_callback(hackrf_transfer *transfer)
} }
if(sem_post(&sdr_ctx->buf_sem) < 0) { if(sem_post(&sdr_ctx->buf_sem) < 0) {
perror("sem_post"); LOG(LVL_ERR, "sem_post: %s", strerror(errno));
return HACKRF_ERROR_OTHER; return HACKRF_ERROR_OTHER;
} }
@ -169,7 +170,7 @@ result_t sdr_init(sdr_ctx_t *ctx)
result = sem_init(&ctx->buf_sem, 0, 1); result = sem_init(&ctx->buf_sem, 0, 1);
if(result < 0) { if(result < 0) {
perror("sem_init failed"); LOG(LVL_ERR, "sem_init failed: %s", strerror(errno));
return ERR_SDR; return ERR_SDR;
} }
@ -291,7 +292,7 @@ result_t sdr_transmit(sdr_ctx_t *ctx, const float complex *samples, size_t nsamp
(void)timeout_us; // not implemented yet (void)timeout_us; // not implemented yet
if(sem_wait(&ctx->buf_sem) < 0) { if(sem_wait(&ctx->buf_sem) < 0) {
perror("sem_wait failed. Samples lost. Error:"); LOG(LVL_ERR, "sem_wait failed. Samples lost. Error:: %s", strerror(errno));
return ERR_SDR; return ERR_SDR;
} }
@ -303,7 +304,7 @@ result_t sdr_transmit(sdr_ctx_t *ctx, const float complex *samples, size_t nsamp
} }
if(sem_post(&ctx->buf_sem) < 0) { if(sem_post(&ctx->buf_sem) < 0) {
perror("sem_post"); LOG(LVL_ERR, "sem_post: %s", strerror(errno));
return ERR_SDR; return ERR_SDR;
} }
@ -318,7 +319,7 @@ result_t sdr_receive(sdr_ctx_t *ctx, float complex *samples, size_t *nsamples, l
int result; int result;
if(sem_wait(&ctx->buf_sem) < 0) { if(sem_wait(&ctx->buf_sem) < 0) {
perror("sem_wait"); LOG(LVL_ERR, "sem_wait: %s", strerror(errno));
return ERR_SDR; return ERR_SDR;
} }
@ -351,7 +352,7 @@ result_t sdr_receive(sdr_ctx_t *ctx, float complex *samples, size_t *nsamples, l
} }
if(sem_post(&ctx->buf_sem) < 0) { if(sem_post(&ctx->buf_sem) < 0) {
perror("sem_post"); LOG(LVL_ERR, "sem_post: %s", strerror(errno));
return ERR_SDR; return ERR_SDR;
} }
@ -366,7 +367,7 @@ result_t sdr_flush_tx_buffer(sdr_ctx_t *ctx)
// block until all samples have been transmitted // block until all samples have been transmitted
while(true) { while(true) {
if(sem_wait(&ctx->buf_sem) < 0) { if(sem_wait(&ctx->buf_sem) < 0) {
perror("sem_wait"); LOG(LVL_ERR, "sem_wait: %s", strerror(errno));
return 0; return 0;
} }
@ -374,7 +375,7 @@ result_t sdr_flush_tx_buffer(sdr_ctx_t *ctx)
double end = ctx->tx_start_time + ctx->tx_duration; double end = ctx->tx_start_time + ctx->tx_duration;
if(sem_post(&ctx->buf_sem) < 0) { if(sem_post(&ctx->buf_sem) < 0) {
perror("sem_post"); LOG(LVL_ERR, "sem_post: %s", strerror(errno));
return 0; return 0;
} }
@ -394,14 +395,14 @@ size_t sdr_get_tx_buffer_free_space(sdr_ctx_t *ctx)
size_t free_samples = 0; size_t free_samples = 0;
if(sem_wait(&ctx->buf_sem) < 0) { if(sem_wait(&ctx->buf_sem) < 0) {
perror("sem_wait"); LOG(LVL_ERR, "sem_wait: %s", strerror(errno));
return 0; return 0;
} }
free_samples = cbuffercf_space_available(ctx->tx_buf); free_samples = cbuffercf_space_available(ctx->tx_buf);
if(sem_post(&ctx->buf_sem) < 0) { if(sem_post(&ctx->buf_sem) < 0) {
perror("sem_post"); LOG(LVL_ERR, "sem_post: %s", strerror(errno));
return 0; return 0;
} }
@ -414,14 +415,14 @@ size_t sdr_get_tx_buffer_used_space(sdr_ctx_t *ctx)
size_t used_samples = 0; size_t used_samples = 0;
if(sem_wait(&ctx->buf_sem) < 0) { if(sem_wait(&ctx->buf_sem) < 0) {
perror("sem_wait"); LOG(LVL_ERR, "sem_wait: %s", strerror(errno));
return 0; return 0;
} }
used_samples = cbuffercf_size(ctx->tx_buf); used_samples = cbuffercf_size(ctx->tx_buf);
if(sem_post(&ctx->buf_sem) < 0) { if(sem_post(&ctx->buf_sem) < 0) {
perror("sem_post"); LOG(LVL_ERR, "sem_post: %s", strerror(errno));
return 0; return 0;
} }

View file

@ -1,4 +1,7 @@
#include <stdio.h> #include <errno.h>
#include <string.h>
#include "logger.h"
#include "var_array.h" #include "var_array.h"
@ -35,7 +38,7 @@ bool var_array_cf_append(var_array_cf_t *array, float complex symbol)
float complex *new_symbols = reallocarray(array->symbols, new_size, sizeof(float complex)); float complex *new_symbols = reallocarray(array->symbols, new_size, sizeof(float complex));
if(!new_symbols) { if(!new_symbols) {
perror("reallocarray"); LOG(LVL_ERR, "reallocarray: %s", strerror(errno));
return false; return false;
} }

View file

@ -1,7 +1,8 @@
#include <signal.h> #include <signal.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <string.h>
#include <errno.h>
#include "logger.h" #include "logger.h"
@ -39,12 +40,12 @@ int main(void)
term_action.sa_sigaction = signal_handler; term_action.sa_sigaction = signal_handler;
if(sigaction(SIGTERM, &term_action, NULL) < 0) { if(sigaction(SIGTERM, &term_action, NULL) < 0) {
perror("sigaction"); LOG(LVL_ERR, "sigaction: %s", strerror(errno));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if(sigaction(SIGINT, &term_action, NULL) < 0) { if(sigaction(SIGINT, &term_action, NULL) < 0) {
perror("sigaction"); LOG(LVL_ERR, "sigaction: %s", strerror(errno));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -53,7 +54,7 @@ int main(void)
.sa_handler = SIG_IGN, .sa_handler = SIG_IGN,
}; };
if(sigaction(SIGPIPE, &term_action_ign, NULL) < 0) { if(sigaction(SIGPIPE, &term_action_ign, NULL) < 0) {
perror("sigaction"); LOG(LVL_ERR, "sigaction: %s", strerror(errno));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }