2022-10-02 00:47:11 -04:00
|
|
|
#include <unistd.h>
|
2024-05-14 01:23:16 -04:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
2022-10-02 00:47:11 -04:00
|
|
|
|
2022-09-22 17:43:21 -04:00
|
|
|
#include <libreborn/log.h>
|
2024-05-14 01:23:16 -04:00
|
|
|
#include <libreborn/exec.h>
|
2024-06-17 18:09:30 -04:00
|
|
|
#include <libreborn/env.h>
|
2022-09-22 17:43:21 -04:00
|
|
|
|
|
|
|
// Debug Tag
|
|
|
|
const char *reborn_debug_tag = "";
|
2022-10-02 00:47:11 -04:00
|
|
|
|
2024-05-14 01:23:16 -04:00
|
|
|
// Log File
|
|
|
|
static int log_fd = -1;
|
|
|
|
int reborn_get_log_fd() {
|
|
|
|
if (log_fd >= 0) {
|
|
|
|
return log_fd;
|
|
|
|
}
|
|
|
|
// Open Log File
|
2024-06-17 18:09:30 -04:00
|
|
|
const char *fd_str = getenv(_MCPI_LOG_FD_ENV);
|
|
|
|
log_fd = fd_str ? atoi(fd_str) : open("/dev/null", O_WRONLY | O_APPEND);
|
2024-05-14 01:23:16 -04:00
|
|
|
// Check FD
|
|
|
|
if (log_fd < 0) {
|
|
|
|
ERR("Unable To Open Log: %s", strerror(errno));
|
|
|
|
}
|
|
|
|
// Return
|
|
|
|
return reborn_get_log_fd();
|
|
|
|
}
|
2024-06-17 18:09:30 -04:00
|
|
|
void reborn_set_log(const int fd) {
|
2024-05-14 01:23:16 -04:00
|
|
|
// Set Variable
|
2024-06-17 18:09:30 -04:00
|
|
|
log_fd = -1;
|
|
|
|
char buf[128];
|
|
|
|
sprintf(buf, "%i", fd);
|
|
|
|
set_and_print_env(_MCPI_LOG_FD_ENV, buf);
|
2024-05-14 01:23:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Debug Logging
|
|
|
|
static int should_print_debug_to_stderr() {
|
|
|
|
return getenv(MCPI_DEBUG_ENV) != NULL;
|
|
|
|
}
|
2022-10-02 00:47:11 -04:00
|
|
|
int reborn_get_debug_fd() {
|
2024-05-14 01:23:16 -04:00
|
|
|
return should_print_debug_to_stderr() ? STDERR_FILENO : reborn_get_log_fd();
|
|
|
|
}
|