51 lines
1.1 KiB
C++
Raw Normal View History

2022-10-02 00:47:11 -04:00
#include <unistd.h>
2024-05-14 01:23:16 -04:00
#include <fcntl.h>
2024-11-17 22:48:25 -05:00
#include <string>
2022-10-02 00:47:11 -04:00
2022-09-22 17:43:21 -04:00
#include <libreborn/log.h>
#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-11-17 22:48:25 -05:00
// /dev/null FD
static int null_fd = -1;
static void setup_null_fd() {
if (null_fd == -1) {
null_fd = open("/dev/null", O_WRONLY | O_APPEND);
}
}
__attribute__((destructor)) static void close_null_fd() {
close(null_fd);
}
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
const char *fd_str = getenv(_MCPI_LOG_FD_ENV);
2024-11-17 22:48:25 -05:00
if (fd_str) {
log_fd = std::stoi(fd_str);
} else {
setup_null_fd();
log_fd = null_fd;
2024-05-14 01:23:16 -04:00
}
// Return
return reborn_get_log_fd();
}
void reborn_set_log(const int fd) {
2024-05-14 01:23:16 -04:00
// Set Variable
log_fd = -1;
2024-11-21 21:45:57 -05:00
set_and_print_env(_MCPI_LOG_FD_ENV, fd >= 0 ? std::to_string(fd).c_str() : nullptr);
2024-05-14 01:23:16 -04:00
}
// Debug Logging
2024-11-17 22:48:25 -05:00
static bool should_print_debug_to_stderr() {
return getenv(MCPI_DEBUG_ENV) != nullptr;
2024-05-14 01:23:16 -04:00
}
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();
}