Skip to content

Commit

Permalink
Refactored logger internals (#3428)
Browse files Browse the repository at this point in the history
  • Loading branch information
lminiero authored Oct 11, 2024
1 parent d51cf7c commit ed15555
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 216 deletions.
2 changes: 1 addition & 1 deletion src/janus-cfgconv.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ int lock_debug = 0;
/* Main Code */
int main(int argc, char *argv[])
{
janus_log_init(FALSE, TRUE, NULL);
janus_log_init(FALSE, TRUE, NULL, NULL);
atexit(janus_log_destroy);

JANUS_LOG(LOG_INFO, "Janus version: %d (%s)\n", janus_version, janus_version_string);
Expand Down
43 changes: 22 additions & 21 deletions src/janus.c
Original file line number Diff line number Diff line change
Expand Up @@ -4405,11 +4405,12 @@ gint main(int argc, char *argv[]) {
core_limits.rlim_cur = core_limits.rlim_max = RLIM_INFINITY;
setrlimit(RLIMIT_CORE, &core_limits);

g_print("Janus version: %d (%s)\n", janus_version, janus_version_string);
g_print("Janus commit: %s\n", janus_build_git_sha);
g_print("Compiled on: %s\n\n", janus_build_git_time);
janus_mark_started();

JANUS_PRINT("Janus version: %d (%s)\n", janus_version, janus_version_string);
JANUS_PRINT("Janus commit: %s\n", janus_build_git_sha);
JANUS_PRINT("Compiled on: %s\n\n", janus_build_git_time);

/* Initialize some command line options defaults */
options.debug_level = -1;
options.session_timeout = -1;
Expand Down Expand Up @@ -4443,19 +4444,19 @@ gint main(int argc, char *argv[]) {
}
if((config = janus_config_parse(config_file)) == NULL) {
/* We failed to load the libconfig configuration file, let's try the INI */
g_print("Failed to load %s, trying the INI instead...\n", config_file);
JANUS_PRINT("Failed to load %s, trying the INI instead...\n", config_file);
g_free(config_file);
char file[255];
g_snprintf(file, 255, "%s/janus.cfg", configs_folder);
config_file = g_strdup(file);
if((config = janus_config_parse(config_file)) == NULL) {
if(options.config_file) {
/* We only give up if the configuration file was explicitly provided */
g_print("Error reading configuration from %s\n", config_file);
JANUS_PRINT("Error reading configuration from %s\n", config_file);
janus_options_destroy();
exit(1);
}
g_print("Error reading/parsing the configuration file in %s, going on with the defaults and the command line arguments\n",
JANUS_PRINT("Error reading/parsing the configuration file in %s, going on with the defaults and the command line arguments\n",
configs_folder);
config = janus_config_create("janus.cfg");
if(config == NULL) {
Expand Down Expand Up @@ -4530,18 +4531,18 @@ gint main(int argc, char *argv[]) {
}
/* Daemonize now, if we need to */
if(daemonize) {
g_print("Running Janus as a daemon\n");
JANUS_PRINT("Running Janus as a daemon\n");

/* Create a pipe for parent<->child communication during the startup phase */
if(pipe(pipefd) == -1) {
g_print("pipe error!\n");
JANUS_PRINT("pipe error!\n");
exit(1);
}

/* Fork off the parent process */
pid_t pid = fork();
if(pid < 0) {
g_print("Fork error!\n");
JANUS_PRINT("Fork error!\n");
exit(1);
}
if(pid > 0) {
Expand Down Expand Up @@ -4570,7 +4571,7 @@ gint main(int argc, char *argv[]) {

/* Leave the parent and return the exit code we received from the child */
if(code)
g_print("Error launching Janus (error code %d), check the logs for more details\n", code);
JANUS_PRINT("Error launching Janus (error code %d), check the logs for more details\n", code);
exit(code);
}
/* Child here */
Expand All @@ -4582,13 +4583,13 @@ gint main(int argc, char *argv[]) {
/* Create a new SID for the child process */
pid_t sid = setsid();
if(sid < 0) {
g_print("Error setting SID!\n");
JANUS_PRINT("Error setting SID!\n");
exit(1);
}
/* Change the current working directory */
const char *cwd = options.cwd_path ? options.cwd_path : "/";
if((chdir(cwd)) < 0) {
g_print("Error changing the current working directory!\n");
JANUS_PRINT("Error changing the current working directory!\n");
exit(1);
}
/* We close stdin/stdout/stderr when initializing the logger */
Expand All @@ -4606,10 +4607,7 @@ gint main(int argc, char *argv[]) {
if(item && item->value && janus_is_true(item->value))
exit_on_dl_error = TRUE;

/* Initialize logger */
if(janus_log_init(daemonize, use_stdout, logfile) < 0)
exit(1);
/* Check if there are external loggers we need to load as well */
/* Check if there are external loggers we need to load */
const char *path = NULL;
DIR *dir = NULL;
/* External loggers are usually disabled by default: they need to be enabled in the configuration */
Expand Down Expand Up @@ -4664,16 +4662,16 @@ gint main(int argc, char *argv[]) {
memset(eventpath, 0, 1024);
g_snprintf(eventpath, 1024, "%s/%s", path, eventent->d_name);
void *event = dlopen(eventpath, RTLD_NOW | RTLD_GLOBAL);
if (!event) {
if(!event) {
JANUS_LOG(exit_on_dl_error ? LOG_FATAL : LOG_ERR, "\tCouldn't load logger plugin '%s': %s\n", eventent->d_name, dlerror());
if (exit_on_dl_error)
if(exit_on_dl_error)
exit(1);
} else {
create_l *create = (create_l*) dlsym(event, "create");
const char *dlsym_error = dlerror();
if (dlsym_error) {
if(dlsym_error) {
JANUS_LOG(exit_on_dl_error ? LOG_FATAL : LOG_ERR, "\tCouldn't load symbol 'create': %s\n", dlsym_error);
if (exit_on_dl_error)
if(exit_on_dl_error)
exit(1);
continue;
}
Expand Down Expand Up @@ -4717,7 +4715,10 @@ gint main(int argc, char *argv[]) {
if(disabled_loggers != NULL)
g_strfreev(disabled_loggers);
disabled_loggers = NULL;
janus_log_set_loggers(loggers);

/* Initialize logger */
if(janus_log_init(daemonize, use_stdout, logfile, loggers) < 0)
exit(1);

JANUS_PRINT("---------------------------------------------------\n");
JANUS_PRINT(" Starting Meetecho Janus (WebRTC Server) v%s\n", janus_version_string);
Expand Down
Loading

0 comments on commit ed15555

Please sign in to comment.