Hi guys,

changes compared to take2:

- move SUBSYS_MAX and SUBSYS_MAXLEN in logsys.h. The former is required
to assert within DECLARE_* and the latter is good to have visibile as
it's part of the public API limit for subsystem name.

- make DECLARE_* macros assert on error.

- fix error handling in _logsys_rec_init by returning instead of
asserting and remove duplicated assertion (there are a few more of
those, but as noted in take2, now we can handle everything internally).

- fix LOG_MODE_THREADED to not fire wthread when working in non-threaded
mode.

This version has been tested with a full corosync run and it seems to
handle properly.

As soon as this one is ACK, I'll post the rest of corosync port to the
new API.

Fabio
--- include/corosync/engine/logsys.h	(revision 2058)
+++ include/corosync/engine/logsys.h	(working copy)
@@ -46,9 +46,9 @@
 #define LOG_MODE_OUTPUT_FILE		(1<<0)
 #define LOG_MODE_OUTPUT_STDERR		(1<<1)
 #define LOG_MODE_OUTPUT_SYSLOG		(1<<3)
-#define LOG_MODE_NOSUBSYS		(1<<4)
-#define LOG_MODE_FORK			(1<<5)
-#define LOG_MODE_THREADED		(1<<6)
+/* FORK and THREADED are ignored for SUBSYSTEMS */
+#define LOG_MODE_FORK			(1<<4)
+#define LOG_MODE_THREADED		(1<<5)
 
 /*
  * Log priorities, compliant with syslog and SA Forum Log spec.
@@ -65,7 +65,10 @@
 
 /*
  * The tag masks are all mutually exclusive
- */	
+ *
+ * LOG is mandatory, but enforced, for subsystems.
+ * Be careful if/when changing tags at runtime.
+ */
 #define LOGSYS_TAG_LOG			(0xff<<28)
 #define LOGSYS_TAG_ENTER		(1<<27)
 #define LOGSYS_TAG_LEAVE		(1<<26)
@@ -79,36 +82,141 @@
 #define LOGSYS_TAG_TRACE8		(1<<18)
 
 /*
- * External API
+ * Internal APIs that must be globally exported
+ * (External API below)
  */
-extern void logsys_config_mode_set (
-	unsigned int mode);
 
-extern unsigned int logsys_config_mode_get (void);
+/* logsys_logger bits */
+#define SUBSYS_MAX	63 /* maximum number of subsystems */
+#define SUBSYS_MAXLEN	64 /* max len of a subsystem name */
 
-extern int logsys_config_file_set (
-	const char **error_string,
-	const char *file);
+extern int _logsys_system_setup(
+	const char *system,
+	unsigned int mode,
+	unsigned int debug,
+	const char *logfile,
+	int logfile_priority,
+	int syslog_facility,
+	int syslog_priority,
+	unsigned int tags);
 
-extern void logsys_config_facility_set (
-	const char *name,
-	unsigned int facility);
+extern int _logsys_config_subsys_get (
+	const char *subsys);
 
+extern unsigned int _logsys_subsys_create (const char *subsys);
+
+extern int _logsys_rec_init (unsigned int size);
+
+extern void _logsys_log_printf (
+	int subsysid,
+	const char *function_name,
+	const char *file_name,
+	int file_line,
+	unsigned int level,
+	const char *format,
+	...) __attribute__((format(printf, 6, 7)));
+
+extern void _logsys_log_rec (
+	int subsysid,
+	const char *function_name,
+	const char *file_name,
+	int file_line,
+	unsigned int rec_ident,
+	...);
+
+extern int _logsys_wthread_create (void);
+
+static unsigned int logsys_subsys_id __attribute__((unused)) = -1;
+
+/*
+ * External API - init
+ * See below:
+ *
+ * LOGSYS_DECLARE_SYSTEM
+ * LOGSYS_DECLARE_SUBSYS
+ *
+ */
+extern void logsys_fork_completed (void);
+
+extern void logsys_atexit (void);
+
+/*
+ * External API - misc
+ */
+extern int logsys_log_rec_store (const char *filename);
+
+/*
+ * External API - configuration
+ */
+
+/*
+ * configuration bits that can only be done for the whole system
+ */
 extern int logsys_format_set (
 	const char *format);
 
 extern char *logsys_format_get (void);
 
-extern unsigned int logsys_config_subsys_set (
+/*
+ * per system/subsystem settings.
+ *
+ * NOTE: once a subsystem is created and configured, changing
+ * the default does NOT affect the subsystems.
+ *
+ * Pass a NULL subsystem to change the defaults (or what was initialized
+ * via DECLARE_SYSTEM or equivalent).
+ */
+extern unsigned int logsys_config_syslog_facility_set (
 	const char *subsys,
-	unsigned int tags,
+	unsigned int facility);
+
+extern unsigned int logsys_config_syslog_priority_set (
+	const char *subsys,
 	unsigned int priority);
 
-extern int logsys_config_subsys_get (
+extern unsigned int logsys_config_mode_set (
 	const char *subsys,
-	unsigned int *tags,
-	unsigned int *priority);
+	unsigned int mode);
 
+extern unsigned int logsys_config_mode_get (
+	const char *subsys);
+
+extern unsigned int logsys_config_tags_set (
+	const char *subsys,
+	unsigned int tags);
+
+extern unsigned int logsys_config_tags_get (
+	const char *subsys);
+
+/*
+ * to close a logfile, just invoke this function with a NULL
+ * file or if you want to change logfile, the old one will
+ * be closed for you.
+ */
+extern int logsys_config_file_set (
+	const char *subsys,
+	const char **error_string,
+	const char *file);
+
+extern unsigned int logsys_config_logfile_priority_set (
+	const char *subsys,
+	unsigned int priority);
+
+/*
+ * enabling debug, disable message priority filtering.
+ * everything is sent everywhere. priority values
+ * for file and syslog are not overwritten.
+ */
+extern unsigned int logsys_config_debug_set (
+	const char *subsys,
+	unsigned int value);
+
+/*
+ * External API - helpers
+ *
+ * convert facility/priority/tag to/from name/values
+ */
+
 extern int logsys_facility_id_get (
 	const char *name);
 
@@ -127,92 +235,41 @@
 extern const char *logsys_tag_name_get (
 	unsigned int tag);
 
-extern void logsys_fork_completed (void);
-
-extern void logsys_flush (void);
-
-extern void logsys_atsegv (void);
-
-extern int logsys_log_rec_store (const char *filename);
-
 /*
- * Internal APIs that must be globally exported
- */
-extern unsigned int _logsys_subsys_create (
-	const char *ident,
-	unsigned int priority);
-
-extern void _logsys_nosubsys_set (void);
-
-extern int _logsys_rec_init (unsigned int size);
-
-extern void _logsys_log_printf (
-	int subsys,
-	const char *function_name,
-	const char *file_name,
-	int file_line,
-	unsigned int level,
-	const char *format,
-	...) __attribute__((format(printf, 6, 7)));
-
-extern void _logsys_log_rec (
-	int subsys,
-	const char *function_name,
-	const char *file_name,
-	int file_line,
-	unsigned int rec_ident,
-	...);
-
-extern int _logsys_wthread_create (void);
-
-static unsigned int logsys_subsys_id __attribute__((unused)) = -1;
-									
-/*
  * External definitions
  */
 extern void *logsys_rec_end;
 
 #define LOG_REC_END (&logsys_rec_end)
 
-#define LOGSYS_DECLARE_SYSTEM(name,mode,file,facility,format,rec_size)	\
-__attribute__ ((constructor)) static void logsys_system_init (void)	\
+#define LOGSYS_DECLARE_SYSTEM(name,mode,debug,file,file_priority,	\
+		syslog_facility,syslog_priority,tags,format,rec_size)	\
+__attribute__ ((constructor))						\
+static void logsys_system_init (void)					\
 {									\
-	const char *error_string;						\
+	int err;							\
 									\
-	logsys_config_mode_set (mode);					\
-	logsys_config_file_set (&error_string, (file));			\
-	logsys_config_facility_set (name, (facility));			\
-	logsys_format_set (format); /* FIXME: assert success? */	\
-	_logsys_rec_init (rec_size);					\
-	_logsys_wthread_create();					\
-}
-
-#define LOGSYS_DECLARE_NOSUBSYS(priority)				\
-__attribute__ ((constructor)) static void logsys_nosubsys_init (void)	\
-{									\
-	unsigned int pri, tags;						\
+	err = _logsys_system_setup (name,mode,debug,file,file_priority,	\
+				syslog_facility,syslog_priority,tags);	\
+	assert (err == 0);						\
 									\
-	logsys_subsys_id =						\
-		logsys_config_subsys_get("MAIN", &tags, &pri);		\
+	err = logsys_format_set (format);				\
+	assert (err == 0);						\
 									\
-	if (logsys_subsys_id == -1) {					\
-		_logsys_nosubsys_set();					\
-		logsys_subsys_id =					\
-			_logsys_subsys_create ("MAIN", (priority));	\
-	}								\
+	err = _logsys_rec_init (rec_size);				\
+	assert (err == 0);						\
+									\
+	err = _logsys_wthread_create();					\
+	assert (err == 0);						\
 }
 
-#define LOGSYS_DECLARE_SUBSYS(subsys,priority)				\
-__attribute__ ((constructor)) static void logsys_subsys_init (void)	\
+#define LOGSYS_DECLARE_SUBSYS(subsys)					\
+__attribute__ ((constructor))						\
+static void logsys_subsys_init (void)					\
 {									\
-	unsigned int pri, tags;						\
-									\
 	logsys_subsys_id =						\
-		logsys_config_subsys_get((subsys), &tags, &pri);	\
-									\
-	if (logsys_subsys_id == -1)					\
-		logsys_subsys_id =					\
-			_logsys_subsys_create ((subsys), (priority));	\
+		_logsys_subsys_create ((subsys));			\
+	assert (logsys_subsys_id < SUBSYS_MAX);				\
 }
 
 #define log_rec(rec_ident, args...)					\
@@ -277,26 +334,4 @@
 		__FILE__,  __LINE__, LOGSYS_TAG_TRACE8, format, ##args);\
 } while(0)
 
-/*
- * For one-time programmatic initialization and configuration of logsys
- * instead of using the DECLARE macros.  These APIs do not allow subsystems
- */
-int logsys_init (
-	const char *name,
-	int mode,
-	int facility,
-	int priority,
-	const char *file,
-	char *format,
-	int rec_size);
-
-int logsys_conf (
-	char *name,
-	int mode,
-	int facility,
-	int priority,
-	char *file);
-
-void logsys_exit (void);
-
 #endif /* LOGSYS_H_DEFINED */
--- exec/logsys.c	(revision 2058)
+++ exec/logsys.c	(working copy)
@@ -96,35 +96,35 @@
 
 int flt_data_size;
 
-#define SUBSYS_MAX 32
-
 #define COMBINE_BUFFER_SIZE 2048
 
+/* values for logsys_logger init_status */
+#define LOGSYS_LOGGER_INIT_DONE		0
+#define LOGSYS_LOGGER_NEEDS_INIT	1
+
+static int logsys_system_needs_init = LOGSYS_LOGGER_NEEDS_INIT;
+
+/* need unlogical order to preserve 64bit alignment */
 struct logsys_logger {
-	char subsys[64];
-	unsigned int priority;
-	unsigned int tags;
-	unsigned int mode;
+	char subsys[SUBSYS_MAXLEN];	/* subsystem name */
+	char *logfile;			/* log to file */
+	FILE *logfile_fp;		/* track file descriptor */
+	unsigned int mode;		/* subsystem mode */
+	unsigned int debug;		/* debug on|off */
+	unsigned int tags;		/* trace tags */
+	int syslog_facility;		/* facility */
+	int syslog_priority;		/* priority */
+	int logfile_priority;		/* priority to file */
+	int init_status;		/* internal field to handle init queues
+					   for subsystems */
 };
 
 /*
- * Configuration parameters for logging system
+ * operating global variables
  */
-static const char *logsys_name = NULL;
 
-static unsigned int logsys_mode = LOG_MODE_NOSUBSYS;
+static struct logsys_logger logsys_loggers[SUBSYS_MAX + 1];
 
-static const char *logsys_file = NULL;
-
-static FILE *logsys_file_fp = NULL;
-
-static int logsys_facility = LOG_DAEMON;
-
-/*
- * operating global variables
- */
-static struct logsys_logger logsys_loggers[SUBSYS_MAX];
-
 static int wthread_active = 0;
 
 static int wthread_should_exit = 0;
@@ -161,8 +161,6 @@
 
 #define FDTAIL_INDEX 	(flt_data_size + 1)
 
-static void logsys_atexit (void);
-
 /*
  * Helpers for _logsys_log_rec functionality
  */
@@ -204,7 +202,6 @@
 }
 #endif
 
-
 /*
  * Before any write operation, a reclaim on the buffer area must be executed
  */
@@ -305,7 +302,7 @@
 	const char *file_name,
 	const char *function_name,
 	int file_line,
-	unsigned int level,
+	int level,
 	char *buffer)
 {
 	char output_buffer[COMBINE_BUFFER_SIZE];
@@ -316,7 +313,13 @@
 	struct timeval tv;
 	int cutoff;
 	unsigned int len;
-	
+	int subsysid;
+
+	subsysid = _logsys_config_subsys_get(subsys);
+	if (subsysid == - 1) {
+		return;
+	}
+
 	while (format_buffer[format_buffer_idx]) {
 		cutoff = -1;
 		if (format_buffer[format_buffer_idx] == '%') {
@@ -375,34 +378,41 @@
 
 	/*
 	 * Output to syslog
-	 */	
-	if (logsys_mode & LOG_MODE_OUTPUT_SYSLOG) {
-		syslog (level, "%s", output_buffer);
+	 */
+	if (((logsys_loggers[subsysid].mode & LOG_MODE_OUTPUT_SYSLOG) &&
+	     (level <= logsys_loggers[subsysid].syslog_priority)) || 
+	     (logsys_loggers[subsysid].debug != 0)) {
+		syslog (level | logsys_loggers[subsysid].syslog_facility, "%s", output_buffer);
 	}
 
 	/*
 	 * Terminate string with \n \0
 	 */
-	if (logsys_mode & (LOG_MODE_OUTPUT_FILE|LOG_MODE_OUTPUT_STDERR)) {
+	if (logsys_loggers[subsysid].mode & (LOG_MODE_OUTPUT_FILE|LOG_MODE_OUTPUT_STDERR)) {
 		output_buffer[output_buffer_idx++] = '\n';
 		output_buffer[output_buffer_idx] = '\0';
 	}
 
 	/*
 	 * Output to configured file
-	 */	
-	if ((logsys_mode & LOG_MODE_OUTPUT_FILE) && logsys_file_fp) {
+	 */
+	if (((logsys_loggers[subsysid].mode & LOG_MODE_OUTPUT_FILE) &&
+	      logsys_loggers[subsysid].logfile_fp &&
+	     (level <= logsys_loggers[subsysid].logfile_priority)) ||
+	     (logsys_loggers[subsysid].debug != 0)) {
 		/*
 		 * Output to a file
 		 */
-		(void)fwrite (output_buffer, strlen (output_buffer), 1, logsys_file_fp);
-		fflush (logsys_file_fp);
+		(void)fwrite (output_buffer, strlen (output_buffer), 1, logsys_loggers[subsysid].logfile_fp);
+		fflush (logsys_loggers[subsysid].logfile_fp);
 	}
 
 	/*
 	 * Output to stderr
-	 */	
-	if (logsys_mode & LOG_MODE_OUTPUT_STDERR) {
+	 */
+	if (((logsys_loggers[subsysid].mode & LOG_MODE_OUTPUT_STDERR) &&
+	     (level <= logsys_loggers[subsysid].logfile_priority)) ||
+	     (logsys_loggers[subsysid].debug != 0)) {
 		(void)write (STDERR_FILENO, output_buffer, strlen (output_buffer));
 	}
 }
@@ -442,10 +452,10 @@
 		(char *)arguments[1],
 		(char *)arguments[2],
 		file_line,
-		level,
+		(level-1),
 		(char *)arguments[3]);
 }
-	
+
 static int record_read (char *buf, int rec_idx, int *log_msg) {
         unsigned int rec_size;
         unsigned int rec_ident;
@@ -582,32 +592,238 @@
 	wthread_wait_locked ();
 }
 
+static int _logsys_config_subsys_get_unlocked (const char *subsys)
+{
+	unsigned int i;
+
+	if (!subsys) {
+		return SUBSYS_MAX;
+	}
+
+ 	for (i = 0; i <= SUBSYS_MAX; i++) {
+		if (strcmp (logsys_loggers[i].subsys, subsys) == 0) {
+			pthread_mutex_unlock (&logsys_config_mutex);
+			return i;
+		}
+	}
+
+	return (-1);
+}
+
+static void syslog_facility_reconf (void)
+{
+	closelog();
+	openlog(logsys_loggers[SUBSYS_MAX].subsys,
+		LOG_CONS|LOG_PID,
+		logsys_loggers[SUBSYS_MAX].syslog_facility);
+}
+
 /*
+ * this is always invoked within the mutex, so it's safe to parse the
+ * whole thing as we need.
+ */
+static void logsys_close_logfile (
+	int subsysid)
+{
+	int i;
+
+	/* if there is no fp, just cleanup possible logfile strdups */
+	if (logsys_loggers[subsysid].logfile_fp == NULL) {
+		goto outfree;
+	}
+
+	/* if there is another subsystem or system using the same fp,
+	 * then we clean our own structs, but we can't close the file
+	 * as it is in use by somebody else.
+	 * Only the last users will be allowed to perform the fclose.
+	 */
+ 	for (i = 0; i <= SUBSYS_MAX; i++) {
+		if ((logsys_loggers[i].logfile_fp == logsys_loggers[subsysid].logfile_fp) &&
+		    (i != subsysid)) {
+			logsys_loggers[subsysid].logfile_fp = NULL;
+			goto outfree;
+		}
+	}
+
+	/* if we are here, we are the last users of that fp, so we can safely
+	 * close it.
+	 */
+	fclose (logsys_loggers[subsysid].logfile_fp);
+	logsys_loggers[subsysid].logfile_fp = NULL;
+
+	/* this is common to all exit paths */ 
+outfree:
+	if (logsys_loggers[subsysid].logfile != NULL) {
+		free (logsys_loggers[subsysid].logfile);
+		logsys_loggers[subsysid].logfile = NULL;
+	}
+}
+
+/*
+ * we need a version that can work when somebody else is already
+ * holding a config mutex lock or we will never get out of here
+ */
+static int logsys_config_file_set_unlocked (
+		int subsysid,
+		const char **error_string,
+		const char *file)
+{
+	static char error_string_response[512];
+	int i;
+
+	if (file == NULL) {
+		logsys_close_logfile(subsysid);
+		return (0);
+	}
+
+	if (logsys_loggers[subsysid].mode & LOG_MODE_OUTPUT_FILE) {
+		logsys_close_logfile(subsysid);
+		logsys_loggers[subsysid].logfile = strdup(file);
+		if (logsys_loggers[subsysid].logfile == NULL) {
+			snprintf (error_string_response,
+				sizeof(error_string_response),
+				"Unable to allocate memory for logfile '%s'\n",
+				file);
+			*error_string = error_string_response;
+			return (-1);
+		}
+ 		for (i = 0; i <= SUBSYS_MAX; i++) {
+			if ((logsys_loggers[i].logfile != NULL) &&
+			    (strcmp (logsys_loggers[i].logfile,
+				logsys_loggers[subsysid].logfile) == 0) &&
+			    (i != subsysid)) {
+				logsys_loggers[subsysid].logfile_fp =
+					logsys_loggers[i].logfile_fp;
+			}
+		}
+		if (logsys_loggers[subsysid].logfile_fp == NULL) {
+			logsys_loggers[subsysid].logfile_fp =
+				fopen (file, "a+");
+		}
+		if (logsys_loggers[subsysid].logfile_fp == NULL) {
+			snprintf (error_string_response,
+				sizeof(error_string_response),
+				"Can't open logfile '%s' for reason (%s).\n",
+					 file, strerror (errno));
+			*error_string = error_string_response;
+			return (-1);
+		}
+	} else
+		logsys_close_logfile(subsysid);
+
+	return (0);
+}
+
+static void logsys_subsys_init (
+		const char *subsys,
+		int subsysid)
+{
+	if (logsys_system_needs_init == LOGSYS_LOGGER_NEEDS_INIT) {
+		logsys_loggers[subsysid].init_status =
+			LOGSYS_LOGGER_NEEDS_INIT;
+	} else {
+		memcpy(&logsys_loggers[subsysid],
+		       &logsys_loggers[SUBSYS_MAX],
+		       sizeof(logsys_loggers[SUBSYS_MAX]));
+		logsys_loggers[subsysid].tags = LOGSYS_TAG_LOG;
+		logsys_loggers[subsysid].init_status = 
+			LOGSYS_LOGGER_INIT_DONE;
+	}
+	strncpy (logsys_loggers[subsysid].subsys, subsys,
+		SUBSYS_MAXLEN);
+}
+
+/*
  * Internal API - exported
  */
-void _logsys_nosubsys_set (void)
+
+int _logsys_system_setup(
+	const char *system,
+	unsigned int mode,
+	unsigned int debug,
+	const char *logfile,
+	int logfile_priority,
+	int syslog_facility,
+	int syslog_priority,
+	unsigned int tags)
 {
-	logsys_mode |= LOG_MODE_NOSUBSYS;
+	int i;
+	const char *errstr;
+	char tempsubsys[SUBSYS_MAXLEN];
+
+	i = SUBSYS_MAX;
+
+	pthread_mutex_lock (&logsys_config_mutex);
+
+	snprintf(logsys_loggers[i].subsys,
+		 SUBSYS_MAXLEN,
+		"%s", system);
+
+	logsys_loggers[i].mode = mode;
+
+	logsys_loggers[i].debug = debug;
+
+	if ((logfile) && strlen(logfile) > 0) {
+		logsys_config_file_set_unlocked (i, &errstr, logfile);
+	}
+	logsys_loggers[i].logfile_priority = logfile_priority;
+
+	logsys_loggers[i].syslog_facility = syslog_facility;
+	logsys_loggers[i].syslog_priority = syslog_priority;
+	syslog_facility_reconf();
+
+	logsys_loggers[i].tags = tags;
+
+	logsys_loggers[i].init_status = LOGSYS_LOGGER_INIT_DONE;
+
+	logsys_system_needs_init = LOGSYS_LOGGER_INIT_DONE;
+
+	for (i = 0; i < SUBSYS_MAX; i++) {
+		if ((strcmp (logsys_loggers[i].subsys, "") != 0) &&
+			(logsys_loggers[i].init_status ==
+			 LOGSYS_LOGGER_NEEDS_INIT)) {
+				strncpy (tempsubsys, logsys_loggers[i].subsys,
+					SUBSYS_MAXLEN);
+				logsys_subsys_init(tempsubsys, i);
+		}
+	}
+
+	pthread_mutex_unlock (&logsys_config_mutex);
+
+	return (0);
 }
 
-unsigned int _logsys_subsys_create (
-	const char *subsys,
-	unsigned int priority)
+unsigned int _logsys_subsys_create (const char *subsys)
 {
+	int i;
+
 	assert (subsys != NULL);
 
-	return logsys_config_subsys_set (
-		subsys,
-		LOGSYS_TAG_LOG,
-		priority);
+	pthread_mutex_lock (&logsys_config_mutex);
+
+	i = _logsys_config_subsys_get_unlocked (subsys);
+	if ((i > -1) && (i < SUBSYS_MAX)) {
+		pthread_mutex_unlock (&logsys_config_mutex);
+		return i;
+	}
+
+	for (i = 0; i < SUBSYS_MAX; i++) {
+		if (strcmp (logsys_loggers[i].subsys, "") == 0) {
+			logsys_subsys_init(subsys, i);			
+			break;
+		}
+	}
+
+	assert(i < SUBSYS_MAX);
+
+	pthread_mutex_unlock (&logsys_config_mutex);
+	return i;
 }
 
 int _logsys_wthread_create (void)
 {
-	if ((logsys_mode & LOG_MODE_FORK) == 0) {
-		if (logsys_name != NULL) {
-			openlog (logsys_name, LOG_CONS|LOG_PID, logsys_facility);
-		}
+	if (((logsys_loggers[SUBSYS_MAX].mode & LOG_MODE_FORK) == 0) && 
+		((logsys_loggers[SUBSYS_MAX].mode & LOG_MODE_THREADED) != 0)) {
 		wthread_create();
 		atexit (logsys_atexit);
 	}
@@ -621,9 +837,10 @@
 	 * Last record ends at zero
 	 */
 	flt_data = malloc ((size + 2) * sizeof (unsigned int));
-	assert (flt_data != NULL);
+	if (flt_data == NULL) {
+		return (-1);
+	}
 	flt_data_size = size;
-	assert (flt_data != NULL);
 	flt_data[FDHEAD_INDEX] = 0;
 	flt_data[FDTAIL_INDEX] = 0;
 
@@ -651,7 +868,7 @@
  * ... repeats length & arg
  */
 void _logsys_log_rec (
-	int subsys,
+	int subsysid,
 	const char *function_name,
 	const char *file_name,
 	int file_line,
@@ -690,8 +907,8 @@
 	/*
 	 * Encode logsys subsystem identity, filename, and function
 	 */
-	buf_args[0] = logsys_loggers[subsys].subsys;
-	buf_len[0] = strlen (logsys_loggers[subsys].subsys) + 1;
+	buf_args[0] = logsys_loggers[subsysid].subsys;
+	buf_len[0] = strlen (logsys_loggers[subsysid].subsys) + 1;
 	buf_args[1] = file_name;
 	buf_len[1] = strlen (file_name) + 1;
 	buf_args[2] = function_name;
@@ -803,7 +1020,7 @@
 }
 
 void _logsys_log_printf (
-        int subsys,
+        int subsysid,
         const char *function_name,
         const char *file_name,
         int file_line,
@@ -815,12 +1032,10 @@
 	unsigned int len;
 	va_list ap;
 
-	if (logsys_mode & LOG_MODE_NOSUBSYS) {
-		subsys = 0;
+	if (subsysid == -1) {
+		subsysid = SUBSYS_MAX;
 	}
-	if (level > logsys_loggers[subsys].priority) {
-		return;
-	}
+
 	va_start (ap, format);
 	len = vsprintf (logsys_print_buffer, format, ap);
 	va_end (ap);
@@ -832,7 +1047,7 @@
 	/*
 	 * Create a log record
 	 */
-	_logsys_log_rec (subsys,
+	_logsys_log_rec (subsysid,
 		function_name,
 		file_name,
 		file_line,
@@ -840,12 +1055,12 @@
 		logsys_print_buffer, len + 1,
 		LOG_REC_END);
 
-	if ((logsys_mode & LOG_MODE_THREADED) == 0) {
+	if ((logsys_loggers[SUBSYS_MAX].mode & LOG_MODE_THREADED) == 0) {
 		/*
 		 * Output (and block) if the log mode is not threaded otherwise
 		 * expect the worker thread to output the log data once signaled
 		 */
-		log_printf_to_logs (logsys_loggers[subsys].subsys,
+		log_printf_to_logs (logsys_loggers[subsysid].subsys,
 			file_name, function_name, file_line, level,
 			logsys_print_buffer);
 	} else {
@@ -856,68 +1071,107 @@
 	}
 }
 
+int _logsys_config_subsys_get (const char *subsys)
+{
+	unsigned int i;
+
+	pthread_mutex_lock (&logsys_config_mutex);
+
+	i = _logsys_config_subsys_get_unlocked (subsys);
+
+	pthread_mutex_unlock (&logsys_config_mutex);
+
+	return i;
+}
+
 /*
  * External Configuration and Initialization API
  */
 void logsys_fork_completed (void)
 {
-	logsys_mode &= ~LOG_MODE_FORK;
+	logsys_loggers[SUBSYS_MAX].mode &= ~LOG_MODE_FORK;
 	_logsys_wthread_create ();
 }
 
-void logsys_config_mode_set (unsigned int mode)
+unsigned int logsys_config_mode_set (const char *subsys, unsigned int mode)
 {
+	int i;
+
 	pthread_mutex_lock (&logsys_config_mutex);
-	logsys_mode = mode;
+	i = _logsys_config_subsys_get_unlocked (subsys);
+	if (i != -1) {
+		logsys_loggers[i].mode = mode;
+		i = 0;
+	}
 	pthread_mutex_unlock (&logsys_config_mutex);
+
+	return i;
 }
 
-unsigned int logsys_config_mode_get (void)
+unsigned int logsys_config_mode_get (const char *subsys)
 {
-	return logsys_mode;
+	int i;
+
+	i = _logsys_config_subsys_get (subsys);
+	if (i == -1) {
+		return -1;
+	}
+
+	return logsys_loggers[i].mode;
 }
 
-static void logsys_close_logfile (void)
+unsigned int logsys_config_tags_set (const char *subsys, unsigned int tags)
 {
-	if (logsys_file_fp != NULL) {
-		fclose (logsys_file_fp);
-		logsys_file_fp = NULL;
+	int i;
+
+	pthread_mutex_lock (&logsys_config_mutex);
+	i = _logsys_config_subsys_get_unlocked (subsys);
+	if (i != -1) {
+		logsys_loggers[i].tags = tags;
+		i = 0;
 	}
+	pthread_mutex_unlock (&logsys_config_mutex);
+
+	return i;
 }
 
-int logsys_config_file_set (const char **error_string, const char *file)
+unsigned int logsys_config_tags_get (const char *subsys)
 {
-	static char error_string_response[512];
+	int i;
 
-	if (file == NULL) {
-		logsys_close_logfile();
-		return (0);
+	i = _logsys_config_subsys_get (subsys);
+	if (i == -1) {
+		return -1;
 	}
 
+	return logsys_loggers[i].tags;
+}
+
+int logsys_config_file_set (
+		const char *subsys,
+		const char **error_string,
+		const char *file)
+{
+	int i;
+	int res;
+
 	pthread_mutex_lock (&logsys_config_mutex);
 
-	if (logsys_mode & LOG_MODE_OUTPUT_FILE) {
-		logsys_file = file;
-		logsys_close_logfile();
-		logsys_file_fp = fopen (file, "a+");
-		if (logsys_file_fp == 0) {
-			snprintf (error_string_response,
-				sizeof(error_string_response),
-				"Can't open logfile '%s' for reason (%s).\n",
-					 file, strerror (errno));
-			*error_string = error_string_response;
-			pthread_mutex_unlock (&logsys_config_mutex);
-			return (-1);
-		}
-	} else
-		logsys_close_logfile();
+	i = _logsys_config_subsys_get_unlocked (subsys);
+	if (i == -1) {
+		res = i;
+	} else {
+		res = logsys_config_file_set_unlocked(i, error_string, file);
+	}
 
 	pthread_mutex_unlock (&logsys_config_mutex);
-	return (0);
+	return res;
 }
 
 int logsys_format_set (const char *format)
 {
+	int ret = 0;
+
 	pthread_mutex_lock (&logsys_config_mutex);
 
 	if (format_buffer) {
@@ -927,15 +1181,15 @@
 
 	if (format) {
 		format_buffer = strdup(format);
+		if (format_buffer == NULL) {
+			ret = -1;
+		}
 	} else {
 		format_buffer = strdup("[%6s] %b");
 	}
-	if (format_buffer == NULL) {
-		return -1;
-	}
 
 	pthread_mutex_unlock (&logsys_config_mutex);
-	return 0;
+	return ret;
 }
 
 char *logsys_format_get (void)
@@ -943,16 +1197,77 @@
 	return format_buffer;
 }
 
-void logsys_config_facility_set (const char *name, unsigned int facility)
+unsigned int logsys_config_syslog_facility_set (
+	const char *subsys,
+	unsigned int facility)
 {
+	int i;
+
 	pthread_mutex_lock (&logsys_config_mutex);
+	i = _logsys_config_subsys_get_unlocked (subsys);
+	if (i != -1) {
+		logsys_loggers[i].syslog_facility = facility;
+		if (i == SUBSYS_MAX) {
+			syslog_facility_reconf();
+		}
+		i = 0;
+	}
+	pthread_mutex_unlock (&logsys_config_mutex);
 
-	logsys_name = name;
-	logsys_facility = facility;
+	return i;
+}
 
+unsigned int logsys_config_syslog_priority_set (
+	const char *subsys,
+	unsigned int priority)
+{
+	int i;
+
+	pthread_mutex_lock (&logsys_config_mutex);
+	i = _logsys_config_subsys_get_unlocked (subsys);
+	if (i != -1) {
+		logsys_loggers[i].syslog_priority = priority;
+		i = 0;
+	}
 	pthread_mutex_unlock (&logsys_config_mutex);
+
+	return i;
 }
 
+unsigned int logsys_config_logfile_priority_set (
+	const char *subsys,
+	unsigned int priority)
+{
+	int i;
+
+	pthread_mutex_lock (&logsys_config_mutex);
+	i = _logsys_config_subsys_get_unlocked (subsys);
+	if (i != -1) {
+		logsys_loggers[i].logfile_priority = priority;
+		i = 0;
+	}
+	pthread_mutex_unlock (&logsys_config_mutex);
+
+	return i;
+}
+
+unsigned int logsys_config_debug_set (
+	const char *subsys,
+	unsigned int debug)
+{
+	int i;
+
+	pthread_mutex_lock (&logsys_config_mutex);
+	i = _logsys_config_subsys_get_unlocked (subsys);
+	if (i != -1) {
+		logsys_loggers[i].debug = debug;
+		i = 0;
+	}
+	pthread_mutex_unlock (&logsys_config_mutex);
+
+	return i;
+}
+
 int logsys_facility_id_get (const char *name)
 {
 	unsigned int i;
@@ -1025,63 +1340,6 @@
 	return (NULL);
 }
 
-unsigned int logsys_config_subsys_set (
-	const char *subsys,
-	unsigned int tags,
-	unsigned int priority)
-{
-	int i;
-
-	pthread_mutex_lock (&logsys_config_mutex);
- 	for (i = 0; i < SUBSYS_MAX; i++) {
-		if (strcmp (logsys_loggers[i].subsys, subsys) == 0) {
-			logsys_loggers[i].tags = tags;
-			logsys_loggers[i].priority = priority;
-
-			break;
-		}
-	}
-
-	if (i == SUBSYS_MAX) {
-		for (i = 0; i < SUBSYS_MAX; i++) {
-			if (strcmp (logsys_loggers[i].subsys, "") == 0) {
-				strncpy (logsys_loggers[i].subsys, subsys,
-					sizeof(logsys_loggers[i].subsys));
-				logsys_loggers[i].tags = tags;
-				logsys_loggers[i].priority = priority;
-				break;
-			}
-		}
-	}
-	assert(i < SUBSYS_MAX);
-
-	pthread_mutex_unlock (&logsys_config_mutex);
-	return i;
-}
-
-int logsys_config_subsys_get (
-	const char *subsys,
-	unsigned int *tags,
-	unsigned int *priority)
-{
-	unsigned int i;
-
-	pthread_mutex_lock (&logsys_config_mutex);
-
- 	for (i = 0; i < SUBSYS_MAX; i++) {
-		if (strcmp (logsys_loggers[i].subsys, subsys) == 0) {
-			*tags = logsys_loggers[i].tags;
-			*priority = logsys_loggers[i].priority;
-			pthread_mutex_unlock (&logsys_config_mutex);
-			return i;
-		}
-	}
-
-	pthread_mutex_unlock (&logsys_config_mutex);
-
-	return (-1);
-}
-
 int logsys_log_rec_store (const char *filename)
 {
 	int fd;
@@ -1105,7 +1363,7 @@
 	return (0);
 }
 
-static void logsys_atexit (void)
+void logsys_atexit (void)
 {
 	if (wthread_active) {
 		wthread_should_exit = 1;
@@ -1113,59 +1371,3 @@
 		pthread_join (logsys_thread_id, NULL);
 	}
 }
-
-void logsys_atsegv (void)
-{
-	if (wthread_active) {
-		wthread_should_exit = 1;
-		wthread_signal ();
-		pthread_join (logsys_thread_id, NULL);
-	}
-}
-
-int logsys_init (
-	const char *name,
-	int mode,
-	int facility,
-	int priority,
-	const char *file,
-	char *format,
-	int rec_size)
-{
-	const char *errstr;
-
-	_logsys_nosubsys_set ();
-	_logsys_subsys_create (name, priority);
-	strncpy (logsys_loggers[0].subsys, name,
-		 sizeof (logsys_loggers[0].subsys));
-	logsys_config_mode_set (mode);
-	logsys_config_facility_set (name, facility);
-	logsys_config_file_set (&errstr, file);
-	if (logsys_format_set (format))
-		return -1;
-	_logsys_rec_init (rec_size);
-	_logsys_wthread_create ();
-	return (0);
-}
-
-int logsys_conf (
-	char *name,
-	int mode,
-	int facility,
-	int priority,
-	char *file)
-{
-	const char *errstr;
-
-	_logsys_rec_init (100000);
-	strncpy (logsys_loggers[0].subsys, name,
-		sizeof (logsys_loggers[0].subsys));
-	logsys_config_mode_set (mode);
-	logsys_config_facility_set (name, facility);
-	logsys_config_file_set (&errstr, file);
-	return (0);
-}
-
-void logsys_exit (void)
-{
-}
_______________________________________________
Openais mailing list
Openais@lists.linux-foundation.org
https://lists.linux-foundation.org/mailman/listinfo/openais

Reply via email to