Skip to content

Commit

Permalink
Merge branch 'feature/172-log-errno-log-levels'
Browse files Browse the repository at this point in the history
See !149
  • Loading branch information
felix-walter committed Dec 20, 2023
2 parents 6d6eac0 + 976563b commit 2601666
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 24 deletions.
2 changes: 1 addition & 1 deletion components/aap2/aap2_agent.c
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,7 @@ struct aap2_agent_config *aap2_agent_setup(
}

if (listen(config->listen_socket, AAP2_AGENT_BACKLOG) < 0) {
LOG_ERRNO(
LOG_ERRNO_ERROR(
"AAP2Agent",
"Error listening on provided address!",
errno
Expand Down
6 changes: 5 additions & 1 deletion components/agents/posix/application_agent.c
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,11 @@ struct application_agent_config *application_agent_setup(
}

if (listen(config->listen_socket, APPLICATION_AGENT_BACKLOG) < 0) {
LOG_ERROR("AppAgent: Error listening on provided address!");
LOG_ERRNO_ERROR(
"AppAgent",
"Error listening on provided address!",
errno
);
free(config);
return NULL;
}
Expand Down
2 changes: 1 addition & 1 deletion components/cla/posix/cla_tcp_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ enum ud3tn_result cla_tcp_read(struct cla_link *link,
} while (ret == -1 && errno == EINTR);

if (ret < 0) {
LOG_ERRNO("TCP", "recv()", errno);
LOG_ERRNO_INFO("TCP", "recv()", errno);
link->config->vtable->cla_disconnect_handler(link);
return UD3TN_FAIL;
} else if (ret == 0) {
Expand Down
4 changes: 2 additions & 2 deletions components/cla/posix/cla_tcp_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,13 @@ int create_tcp_socket(const char *const node, const char *const service,

if (client && connect(sock, e->ai_addr, e->ai_addrlen) < 0) {
error_code = errno;
LOG_ERRNO("TCP", "connect()", error_code);
LOG_ERRNO_INFO("TCP", "connect()", error_code);
close(sock);
continue;
} else if (!client &&
bind(sock, e->ai_addr, e->ai_addrlen) < 0) {
error_code = errno;
LOG_ERRNO("TCP", "bind()", error_code);
LOG_ERRNO_INFO("TCP", "bind()", error_code);
close(sock);
continue;
}
Expand Down
3 changes: 1 addition & 2 deletions components/cla/posix/cla_tcpclv3.c
Original file line number Diff line number Diff line change
Expand Up @@ -772,8 +772,7 @@ static void tcpclv3_begin_packet(struct cla_link *link, size_t length, char *cla

if (tcp_send_all(param->link.connection_socket,
header_buffer, sdnv_len + 1) == -1) {
LOG_ERRNO("TCPCLv3", "send(segment_header)",
errno);
LOG_ERRNO("TCPCLv3", "send(segment_header)", errno);
link->config->vtable->cla_disconnect_handler(link);
}
}
Expand Down
20 changes: 13 additions & 7 deletions components/platform/posix/hal_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@

#include "ud3tn/result.h"

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdarg.h>
#include <stdint.h>
#include <string.h>

static Semaphore_t log_io_semph;

Expand Down Expand Up @@ -71,15 +71,21 @@ int hal_io_log_printf(const int level, const char *const file, const int line,
return rc;
}

void hal_io_log_perror(const char *component, const char *file, int line,
const char *message, int error)
void hal_io_log_perror(int level, const char *component, const char *file,
int line, const char *message, int error)
{
hal_semaphore_take_blocking(log_io_semph);
hal_time_print_log_time_string();
fprintf(stderr, "[SYSTEM ERROR] in %s: ", component);
errno = error;
perror(message);
fprintf(stderr, " [%s:%d]\n", file, line);
fprintf(
stderr,
"[%s] System error reported in %s - %s: %s [%s:%d]\n",
get_log_level_name(level),
component,
message,
strerror(error),
file,
line
);
fflush(stderr);
hal_semaphore_release(log_io_semph);
}
32 changes: 22 additions & 10 deletions include/platform/hal_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,25 @@ do { \
#define LOG_DEBUG(message) ((void)0)
#endif /* DEBUG */

#define LOG_ERRNO(component_, msg_, errno_) \
hal_io_log_perror( \
component_, \
__FILE__, \
(int)(__LINE__), \
msg_, \
errno_ \
)
#define LOG_ERRNO_GENERIC(level, component_, msg_, errno_) \
do { \
__typeof__(level) level_ = (level); \
if (level_ <= LOG_LEVEL) { \
hal_io_log_perror( \
level_, \
component_, \
__FILE__, \
(int)(__LINE__), \
msg_, \
errno_ \
); \
} \
} while (0)

#define LOG_ERRNO_ERROR(...) LOG_ERRNO_GENERIC(1, __VA_ARGS__)
#define LOG_ERRNO_WARN(...) LOG_ERRNO_GENERIC(2, __VA_ARGS__)
#define LOG_ERRNO_INFO(...) LOG_ERRNO_GENERIC(3, __VA_ARGS__)
#define LOG_ERRNO LOG_ERRNO_WARN

/**
* @brief hal_io_init Initialization of underlying OS/HW for I/O
Expand Down Expand Up @@ -86,13 +97,14 @@ int hal_io_log_printf(int level, const char *file, int line,

/**
* @brief hal_io_log_perror Log a system error (i.e., saved errno).
* @param level The log level - a value between 1 (ERROR) and 4 (DEBUG).
* @param component The component in which the error occurred, e.g., "Router".
* @param file The file in which the error occurred.
* @param line The line in which the error occurred.
* @param message The message passed to perror().
* @param error The error number obtained from errno.
*/
void hal_io_log_perror(const char *component, const char *file, int line,
const char *message, int error);
void hal_io_log_perror(int level, const char *component, const char *file,
int line, const char *message, int error);

#endif /* HAL_IO_H_INCLUDED */

0 comments on commit 2601666

Please sign in to comment.