Revision f82a8605

View differences:

README.md
229 229
	-V, --version                 Print version and exit
230 230
	-b, --daemon                  Launch Janus in background as a daemon
231 231
                                  (default=off)
232
	-N, --disable-stdout          Disable stdout based logging  (default=off)
233
	-L, --log-file=path           Log to the specified file (default=stdout only)
232 234
	-i, --interface=ipaddress     Interface to use (will be the public IP)
233 235
	-P, --plugins-folder=path     Plugins folder (default=./plugins)
234 236
	-C, --config=filename         Configuration file to use
conf/janus.cfg.sample.in
1 1
; General configuration: folders where the configuration and the plugins
2
; can be found, default interface to use, debug/logging level and, if
3
; needed, shared apisecret between application(s) and Janus.
2
; can be found, how output should be logged, whether Janus should run as
3
; a daemon or in foreground, default interface to use, debug/logging level
4
; and, if needed, shared apisecret and/or token authentication mechanism
5
; between application(s) and Janus.
4 6
[general]
5 7
configs_folder = @confdir@		; Configuration files folder
6 8
plugins_folder = @plugindir@		; Plugins folder
9
;log_to_stdout = false			; Whether the Janus output should be written
10
								; to stdout or not (default=true)
11
;log_to_file = /tmp/janus.log	; Whether to use a log file or not
12
;daemonize = true				; Whether Janus should run as a daemon
13
								; or not (default=run in foreground)
7 14
;interface = 1.2.3.4		; Interface to use (will be used in SDP)
8 15
debug_level = 4				; Debug/logging level, valid values are 0-7
9 16
;debug_timestamps = yes		; Whether to show a timestamp for each log line
janus.c
113 113
	json_object_set_new(info, "version", json_integer(JANUS_VERSION));
114 114
	json_object_set_new(info, "version_string", json_string(JANUS_VERSION_STRING));
115 115
	json_object_set_new(info, "author", json_string(JANUS_AUTHOR));
116
	json_object_set_new(info, "log-to-stdout", json_string(janus_log_is_stdout_enabled() ? "true" : "false"));
117
	json_object_set_new(info, "log-to-file", json_string(janus_log_is_logfile_enabled() ? "true" : "false"));
118
	if(janus_log_is_logfile_enabled())
119
		json_object_set_new(info, "log-path", json_string(janus_log_get_logfile_path()));
116 120
#ifdef HAVE_SCTP
117 121
	json_object_set_new(info, "data_channels", json_string("true"));
118 122
#else
......
2117 2121
	if(!request)
2118 2122
		return -1;
2119 2123
	gchar *error_string = NULL;
2120
	gchar  error_buf[512];
2124
	gchar error_buf[512];
2121 2125
	if(format == NULL) {
2122 2126
		/* No error string provided, use the default one */
2123 2127
		error_string = (gchar *)janus_get_api_error(error);
......
2897 2901
	core_limits.rlim_cur = core_limits.rlim_max = RLIM_INFINITY;
2898 2902
	setrlimit(RLIMIT_CORE, &core_limits);
2899 2903

  
2900
	/* TODO Check if we need to log to console and/or file, assuming console only for now */
2901
	if(janus_log_init(TRUE, NULL) < 0)
2902
		exit(1);
2903

  
2904 2904
	struct gengetopt_args_info args_info;
2905 2905
	/* Let's call our cmdline parser */
2906 2906
	if(cmdline_parser(argc, argv, &args_info) != 0)
2907 2907
		exit(1);
2908 2908
	
2909
	JANUS_PRINT("---------------------------------------------------\n");
2910
	JANUS_PRINT("  Starting Meetecho Janus (WebRTC Gateway) v%s\n", JANUS_VERSION_STRING);
2911
	JANUS_PRINT("---------------------------------------------------\n\n");
2912
	
2913
	/* Handle SIGINT (CTRL-C), SIGTERM (from service managers) */
2914
	signal(SIGINT, janus_handle_signal);
2915
	signal(SIGTERM, janus_handle_signal);
2909
	/* Any configuration to open? */
2910
	if(args_info.config_given) {
2911
		config_file = g_strdup(args_info.config_arg);
2912
	}
2913
	if(args_info.configs_folder_given) {
2914
		configs_folder = g_strdup(args_info.configs_folder_arg);
2915
	} else {
2916
		configs_folder = g_strdup (CONFDIR);
2917
	}
2918
	if(config_file == NULL) {
2919
		char file[255];
2920
		g_snprintf(file, 255, "%s/janus.cfg", configs_folder);
2921
		config_file = g_strdup(file);
2922
	}
2923
	if((config = janus_config_parse(config_file)) == NULL) {
2924
		if(args_info.config_given) {
2925
			/* We only give up if the configuration file was explicitly provided */
2926
			g_print("Error reading configuration from %s\n", config_file);
2927
			exit(1);
2928
		}
2929
		g_print("Error reading/parsing the configuration file, going on with the defaults and the command line arguments\n");
2930
		config = janus_config_create("janus.cfg");
2931
		if(config == NULL) {
2932
			/* If we can't even create an empty configuration, something's definitely wrong */
2933
			exit(1);
2934
		}
2935
	}
2916 2936

  
2917
	/* Setup Glib */
2918
#if !GLIB_CHECK_VERSION(2, 36, 0)
2919
	g_type_init();
2920
#endif
2937
	/* Check if we need to log to console and/or file */
2938
	gboolean use_stdout = TRUE;
2939
	if(args_info.disable_stdout_given) {
2940
		use_stdout = FALSE;
2941
	} else {
2942
		/* Check if the configuration file is saying anything about this */
2943
		janus_config_item *item = janus_config_get_item_drilldown(config, "general", "log_to_stdout");
2944
		if(item && item->value && !janus_is_true(item->value))
2945
			use_stdout = FALSE;
2946
	}
2947
	const char *logfile = NULL;
2948
	if(args_info.log_file_given) {
2949
		logfile = args_info.log_file_arg;
2950
	} else {
2951
		/* Check if the configuration file is saying anything about this */
2952
		janus_config_item *item = janus_config_get_item_drilldown(config, "general", "log_to_file");
2953
		if(item && item->value)
2954
			logfile = item->value;
2955
	}
2921 2956

  
2957
	/* Check if we're going to daemonize Janus */
2958
	gboolean daemonize = FALSE;
2922 2959
	if(args_info.daemon_given) {
2923
		JANUS_PRINT("Running Janus as a daemon\n");
2924
		/* FIXME Logging is still stdout/stderr based, and we close those */
2925
		JANUS_PRINT("\nNOTE: This is still WIP, and no logging/output is available when running as\n");
2926
		JANUS_PRINT("      a daemon at the moment. Check the documentation for alternatives on\n");
2927
		JANUS_PRINT("      running Janus in background.\n");
2960
		daemonize = TRUE;
2961
	} else {
2962
		/* Check if the configuration file is saying anything about this */
2963
		janus_config_item *item = janus_config_get_item_drilldown(config, "general", "daemonize");
2964
		if(item && item->value && janus_is_true(item->value))
2965
			daemonize = TRUE;
2966
	}
2967
	/* If we're going to daemonize, make sure logging to stdout is disabled and a log file has been specified */
2968
	if(daemonize && use_stdout) {
2969
		use_stdout = FALSE;
2970
	}
2971
	if(daemonize && logfile == NULL) {
2972
		g_print("Running Janus as a daemon but no log file provided, giving up...\n");
2973
		exit(1);
2974
	}
2975
	/* Daemonize now, if we need to */
2976
	if(daemonize) {
2977
		g_print("Running Janus as a daemon\n");
2928 2978

  
2929 2979
		/* Fork off the parent process */
2930 2980
		pid_t pid = fork();
2931 2981
		if(pid < 0) {
2932
			JANUS_PRINT("Fork error!\n");
2982
			g_print("Fork error!\n");
2933 2983
			exit(1);
2934 2984
		}
2935 2985
		if(pid > 0) {
......
2941 2991
		/* Create a new SID for the child process */
2942 2992
		pid_t sid = setsid();
2943 2993
		if(sid < 0) {
2944
			JANUS_PRINT("Error setting SID!\n");
2994
			g_print("Error setting SID!\n");
2945 2995
			exit(1);
2946 2996
		}
2947 2997
		/* Change the current working directory */
2948 2998
		if((chdir("/")) < 0) {
2949
			JANUS_PRINT("Error changing the current working directory!\n");
2999
			g_print("Error changing the current working directory!\n");
2950 3000
			exit(1);
2951 3001
		}
2952 3002

  
......
2956 3006
		close(STDERR_FILENO);
2957 3007
	}
2958 3008

  
3009
	/* Initialize logger */
3010
	if(janus_log_init(use_stdout, logfile) < 0)
3011
		exit(1);
3012

  
3013
	JANUS_PRINT("---------------------------------------------------\n");
3014
	JANUS_PRINT("  Starting Meetecho Janus (WebRTC Gateway) v%s\n", JANUS_VERSION_STRING);
3015
	JANUS_PRINT("---------------------------------------------------\n\n");
3016

  
3017
	/* Handle SIGINT (CTRL-C), SIGTERM (from service managers) */
3018
	signal(SIGINT, janus_handle_signal);
3019
	signal(SIGTERM, janus_handle_signal);
3020

  
3021
	/* Setup Glib */
3022
#if !GLIB_CHECK_VERSION(2, 36, 0)
3023
	g_type_init();
3024
#endif
3025

  
2959 3026
	/* Logging level: default is info and no timestamps */
2960 3027
	janus_log_level = LOG_INFO;
2961 3028
	janus_log_timestamps = FALSE;
......
2968 3035
		janus_log_level = args_info.debug_level_arg;
2969 3036
	}
2970 3037

  
2971
	/* Any configuration to open? */
2972
	if(args_info.config_given) {
2973
		config_file = g_strdup(args_info.config_arg);
2974
		if(config_file == NULL) {
2975
			JANUS_PRINT("Memory error!\n");
2976
			exit(1);
2977
		}
2978
	}
2979
	if(args_info.configs_folder_given) {
2980
		configs_folder = g_strdup(args_info.configs_folder_arg);
2981
		if(configs_folder == NULL) {
2982
			JANUS_PRINT("Memory error!\n");
2983
			exit(1);
2984
		}
2985
	} else {
2986
		configs_folder = g_strdup (CONFDIR);
2987
	}
2988
	if(config_file == NULL) {
2989
		char file[255];
2990
		g_snprintf(file, 255, "%s/janus.cfg", configs_folder);
2991
		config_file = g_strdup(file);
2992
		if(config_file == NULL) {
2993
			JANUS_PRINT("Memory error!\n");
2994
			exit(1);
2995
		}
2996
	}
2997
	JANUS_PRINT("Reading configuration from %s\n", config_file);
2998
	if((config = janus_config_parse(config_file)) == NULL) {
2999
		if(args_info.config_given) {
3000
			/* We only give up if the configuration file was explicitly provided */
3001
			exit(1);
3002
		}
3003
		JANUS_PRINT("Error reading/parsing the configuration file, going on with the defaults and the command line arguments\n");
3004
		config = janus_config_create("janus.cfg");
3005
		if(config == NULL) {
3006
			/* If we can't even create an empty configuration, something's definitely wrong */
3007
			exit(1);
3008
		}
3009
	}
3038
	/* Proceed with the rest of the configuration */
3010 3039
	janus_config_print(config);
3011 3040
	if(args_info.debug_level_given) {
3012 3041
		char debug[5];
......
3710 3739
	}
3711 3740

  
3712 3741
	JANUS_PRINT("Bye!\n");
3742

  
3713 3743
	janus_log_destroy();
3744

  
3714 3745
	exit(0);
3715 3746
}
janus.ggo
1 1
option "daemon" b "Launch Janus in background as a daemon" flag off
2
option "disable-stdout" N "Disable stdout based logging" flag off
3
option "log-file" L "Log to the specified file (default=stdout only)" string typestr="path" optional
2 4
option "interface" i "Interface to use (will be the public IP)" string typestr="ipaddress" optional
3 5
option "plugins-folder" P "Plugins folder (default=./plugins)" string typestr="path" optional
4 6
option "config" C "Configuration file to use" string typestr="filename" optional
log.c
31 31
#define BUFFER_ALLOCSZ(r)	(r + sizeof(janus_log_buffer))
32 32

  
33 33
static gboolean janus_log_console = TRUE;
34
static char *janus_log_filepath = NULL;
34 35
static FILE *janus_log_file = NULL;
35 36

  
36 37
static gint initialized = 0;
......
47 48
static janus_log_buffer *bufferpool = NULL;
48 49

  
49 50

  
51
gboolean janus_log_is_stdout_enabled(void) {
52
	return janus_log_console;
53
}
54

  
55
gboolean janus_log_is_logfile_enabled(void) {
56
	return janus_log_file != NULL;
57
}
58

  
59
char *janus_log_get_logfile_path(void) {
60
	return janus_log_filepath;
61
}
62

  
63

  
50 64
static void janus_log_freebuffers(janus_log_buffer **list) {
51 65
	janus_log_buffer *b, *head = *list;
52 66

  
......
150 164
	if(janus_log_file)
151 165
		fclose(janus_log_file);
152 166
	janus_log_file = NULL;
167
	if(janus_log_filepath)
168
		g_free(janus_log_filepath);
169
	janus_log_filepath = NULL;
153 170

  
154 171
	return NULL;
155 172
}
......
200 217
			g_print("Error opening log file %s: %s\n", logfile, strerror(errno));
201 218
			return -1;
202 219
		}
220
		janus_log_filepath = g_strdup(logfile);
221
	}
222
	if(!janus_log_console && logfile == NULL) {
223
		g_print("WARNING: logging completely disabled!\n");
224
		g_print("         (no stdout and no logfile, this may not be what you want...)\n");
203 225
	}
204 226
	printthread = g_thread_new(THREAD_NAME, &janus_log_thread, NULL);
205 227
	return 0;
log.h
22 22
* @param[in] args Parameters to insert into formatted string
23 23
* \note This output is buffered and may not appear immediately on stdout. */
24 24
void janus_vprintf(const char *format, ...) G_GNUC_PRINTF(1, 2);
25

  
25 26
/*! \brief Log initialization
26 27
* \note This should be called before attempting to use the logger. A buffer
27 28
* pool and processing thread are created.
......
32 33
/*! \brief Log destruction */
33 34
void janus_log_destroy(void);
34 35

  
36
/*! \brief Method to check whether stdout logging is enabled
37
 * @returns TRUE if stdout logging is enabled, FALSE otherwise */
38
gboolean janus_log_is_stdout_enabled(void);
39
/*! \brief Method to check whether file-based logging is enabled
40
 * @returns TRUE if file-based logging is enabled, FALSE otherwise */
41
gboolean janus_log_is_logfile_enabled(void);
42
/*! \brief Method to get the path to the log file
43
 * @returns The full path to the log file, or NULL otherwise */
44
char *janus_log_get_logfile_path(void);
45

  
35 46
#endif
mainpage.dox
1951 1951
 * Since version \c 0.1.0, you can run Janus as a daemon application. To
1952 1952
 * do so, just pass either \c -b or <code>\--daemon</code> as a command line
1953 1953
 * argument, and Janus will be daemonized. Just beware, though, that
1954
 * since all logging in Janus is currently stdout/stderr based, running
1955
 * Janus as a daemon will make you loose all output and logs. Work is
1956
 * currently ongoing to change the way logging is implemented.
1954
 * since this results in stdout/stdin/stderr being closed, you MUST
1955
 * specify a log file for Janus to use, either via command line (\c -L
1956
 * or <code>\--log-file) or in \c janus.cfg.
1957 1957
 *
1958 1958
 * \section bg Running in background
1959 1959
 * Another simple way to run Janus in the background is to just append the

Also available in: Unified diff