Skip to content

Commit

Permalink
Refactored logger internals (#3428)
Browse files Browse the repository at this point in the history
  • Loading branch information
lminiero committed Oct 11, 2024
1 parent 5b0dfd6 commit b019606
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 220 deletions.
2 changes: 1 addition & 1 deletion 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
42 changes: 22 additions & 20 deletions janus.c
Original file line number Diff line number Diff line change
Expand Up @@ -4239,10 +4239,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 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);

struct gengetopt_args_info args_info;
/* Let's call our cmdline parser */
if(cmdline_parser(argc, argv, &args_info) != 0)
Expand All @@ -4264,18 +4266,18 @@ 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(args_info.config_given) {
/* 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);
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 @@ -4349,18 +4351,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 @@ -4389,7 +4391,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 @@ -4401,13 +4403,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 = (args_info.cwd_path_given) ? args_info.cwd_path_arg : "/";
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 @@ -4427,10 +4429,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 @@ -4485,16 +4484,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 @@ -4538,7 +4537,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 b019606

Please sign in to comment.