Hi,

this duplicate code was giving me a fit every single time I was looking
at it.

Make it common, static and shared between the only two callers.

At the first look this might look like a performance hit because we move
the log_level check after the va operations but truth is that the check
should not be there at all. It will have to be removed once log_rec is
able to perform as we expect to record everything. At this point in time
the check is only a workaround to make everything work.

Fabio

PS also remove the Makefile.am check since now the 2 callers are very
small and close to each other.
Index: exec/logsys.c
===================================================================
--- exec/logsys.c	(revision 2184)
+++ exec/logsys.c	(working copy)
@@ -945,6 +945,60 @@
 		LOGSYS_MAX_SUBSYS_NAMELEN);
 }
 
+static void __logsys_log_printf(
+	int subsysid,
+	const char *function_name,
+	const char *file_name,
+	int file_line,
+	unsigned int level,
+	unsigned int tag,
+	char *logsys_print_buffer,
+	unsigned int len)
+{
+	if (subsysid <= -1) {
+		subsysid = LOGSYS_MAX_SUBSYS_COUNT;
+	}
+
+	if ((level > logsys_loggers[subsysid].syslog_priority) &&
+	    (level > logsys_loggers[subsysid].logfile_priority) &&
+	    (logsys_loggers[subsysid].debug == 0)) {
+		return;
+	}
+
+	if (logsys_print_buffer[len - 1] == '\n') {
+		logsys_print_buffer[len - 1] = '\0';
+		len -= 1;
+	}
+
+	/*
+	 * Create a log record
+	 */
+	_logsys_log_rec (subsysid,
+		function_name,
+		file_name,
+		file_line,
+		level,
+		tag |= LOGSYS_TAG_LOG,
+		logsys_print_buffer, len + 1,
+		LOGSYS_REC_END);
+
+	if ((logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].mode & LOGSYS_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[subsysid].subsys,
+			file_name, function_name, file_line, level, tag,
+			logsys_print_buffer);
+	} else {
+		/*
+		 * Signal worker thread to display logging output
+		 */
+		wthread_signal ();
+	}
+
+}
+
 /*
  * Internal API - exported
  */
@@ -1262,48 +1316,11 @@
 	char logsys_print_buffer[COMBINE_BUFFER_SIZE];
 	unsigned int len;
 
-	if (subsysid <= -1) {
-		subsysid = LOGSYS_MAX_SUBSYS_COUNT;
-	}
-
-	if ((level > logsys_loggers[subsysid].syslog_priority) &&
-	    (level > logsys_loggers[subsysid].logfile_priority) &&
-	    (logsys_loggers[subsysid].debug == 0)) {
-		return;
-	}
-
 	len = vsprintf (logsys_print_buffer, format, ap);
-	if (logsys_print_buffer[len - 1] == '\n') {
-		logsys_print_buffer[len - 1] = '\0';
-		len -= 1;
-	}
 
-	/*
-	 * Create a log record
-	 */
-	_logsys_log_rec (subsysid,
-		function_name,
-		file_name,
-		file_line,
-		level,
-		tag |= LOGSYS_TAG_LOG,
-		logsys_print_buffer, len + 1,
-		LOGSYS_REC_END);
-
-	if ((logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].mode & LOGSYS_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[subsysid].subsys,
-			file_name, function_name, file_line, level, tag,
-			logsys_print_buffer);
-	} else {
-		/*
-		 * Signal worker thread to display logging output
-		 */
-		wthread_signal ();
-	}
+	__logsys_log_printf(subsysid, function_name, file_name,
+			file_line, level, tag,
+			logsys_print_buffer, len);
 }
 
 void _logsys_log_printf (
@@ -1320,50 +1337,13 @@
 	unsigned int len;
 	va_list ap;
 
-	if (subsysid <= -1) {
-		subsysid = LOGSYS_MAX_SUBSYS_COUNT;
-	}
-
-	if ((level > logsys_loggers[subsysid].syslog_priority) &&
-	    (level > logsys_loggers[subsysid].logfile_priority) &&
-	    (logsys_loggers[subsysid].debug == 0)) {
-		return;
-	}
-
 	va_start (ap, format);
 	len = vsprintf (logsys_print_buffer, format, ap);
 	va_end (ap);
-	if (logsys_print_buffer[len - 1] == '\n') {
-		logsys_print_buffer[len - 1] = '\0';
-		len -= 1;
-	}
 
-	/*
-	 * Create a log record
-	 */
-	_logsys_log_rec (subsysid,
-		function_name,
-		file_name,
-		file_line,
-		level,
-		tag |= LOGSYS_TAG_LOG,
-		logsys_print_buffer, len + 1,
-		LOGSYS_REC_END);
-
-	if ((logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].mode & LOGSYS_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[subsysid].subsys,
-			file_name, function_name, file_line, level, tag,
-			logsys_print_buffer);
-	} else {
-		/*
-		 * Signal worker thread to display logging output
-		 */
-		wthread_signal ();
-	}
+	__logsys_log_printf(subsysid, function_name, file_name,
+			file_line, level, tag,
+			logsys_print_buffer, len);
 }
 
 int _logsys_config_subsys_get (const char *subsys)
Index: exec/Makefile.am
===================================================================
--- exec/Makefile.am	(revision 2184)
+++ exec/Makefile.am	(working copy)
@@ -141,14 +141,3 @@
 
 clean-local:
 	rm -f corosync *.o *.lcrso gmon.out *.da *.bb *.bbg *.so*
-
-# Since we're requiring that _logsys_log_printf and _logsys_log_vprintf
-# have nearly identical code, here we require that they stay in sync.
-check_logsys_log_printf_functions:
-	$(AWK) '/^void _logsys_log_printf \(/, /^}/' $(srcdir)/logsys.c \
-	  | sed '/^[	 ]*va_/d;s/\.\.\.)$$/va_list ap)/' > $...@-1
-	$(AWK) '/^void _logsys_log_vprintf \(/, /^}/' $(srcdir)/logsys.c \
-	  | sed 's/log_vprintf/log_printf/' > $...@-2
-	diff $...@-1 $...@-2 && rm -f $...@-1 $...@-2
-
-check: check_logsys_log_printf_functions
_______________________________________________
Openais mailing list
[email protected]
https://lists.linux-foundation.org/mailman/listinfo/openais

Reply via email to