[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: [Cluster-devel] logsys in cluster3



Here's a patch that adds a simple, function-based api to logsys, allowing
non-plugins to use it much more cleanly.  It seems to mostly work with the
attached test program, which illustrates how I expect to use it in various
cluster3 programs.  (What doesn't work yet is the "test 1" output
following the logsys_init() call.)


Index: logsys.c
===================================================================
--- logsys.c	(revision 1568)
+++ logsys.c	(working copy)
@@ -632,3 +632,32 @@
 {
 	worker_thread_group_wait (&log_thread_group);
 }
+
+int logsys_init(char *name, int mode, int facility, int priority, char *file)
+{
+	char *errstr;
+	logsys_config_mode_set(mode);
+	logsys_config_facility_set(name, facility);
+	logsys_config_file_set(&errstr, file);
+	_logsys_config_priority_set(0, priority);
+	if ((mode & LOG_MODE_BUFFER_BEFORE_CONFIG) == 0) {
+		_logsys_wthread_create();
+	}
+	return 0;
+}
+
+int logsys_conf(char *name, int mode, int facility, int priority, char *file)
+{
+	char *errstr;
+	logsys_config_mode_set(mode);
+	logsys_config_facility_set(name, facility);
+	logsys_config_file_set(&errstr, file);
+	_logsys_config_priority_set(0, priority);
+	return 0;
+}
+
+int logsys_exit(void)
+{
+	return 0;
+}
+
Index: logsys.h
===================================================================
--- logsys.h	(revision 1568)
+++ logsys.h	(working copy)
@@ -170,8 +170,9 @@
 	}								\
 }
 
+static unsigned int logsys_subsys_id __attribute__((unused));		\
+
 #define LOGSYS_DECLARE_NOSUBSYS(priority)				\
-static unsigned int logsys_subsys_id __attribute__((unused));		\
 __attribute__ ((constructor)) static void logsys_nosubsys_init (void)	\
 {									\
 	_logsys_nosubsys_set();						\
@@ -180,7 +181,6 @@
 }
 
 #define LOGSYS_DECLARE_SUBSYS(subsys,priority)				\
-static unsigned int logsys_subsys_id __attribute__((unused));		\
 __attribute__ ((constructor)) static void logsys_subsys_init (void)	\
 {									\
 	logsys_subsys_id =						\
@@ -293,4 +293,10 @@
 	_logsys_config_priority_set (logsys_subsys_id, priority);       \
 } while(0)
 
+/* simple, function-based api */
+
+int logsys_init(char *name, int mode, int facility, int priority, char *file);
+int logsys_conf(char *name, int mode, int facility, int priority, char *file);
+int logsys_exit(void);
+
 #endif /* LOGSYS_H_DEFINED */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "logsys.h"

#define DEFAULT_MODE		LOG_MODE_OUTPUT_STDERR
#define DEFAULT_FACILITY	LOG_DAEMON
#define DEFAULT_PRIORITY	LOG_LEVEL_INFO
#define DEFAULT_FILE		NULL

char *prog_name;
int prog_debug;

/* Read cluster.conf settings and convert them into logsys values.
   If no cluster.conf setting exists, the default that was used in
   logsys_init() is used. */

int read_ccs_logging(int *mode, int *facility, int *priority, char *file,
                     int *debug)
{
	*mode = DEFAULT_MODE;
	*facility = DEFAULT_FACILITY;
	*priority = DEFAULT_PRIORITY;

	if (DEFAULT_FILE)
		strcpy(file, DEFAULT_FILE);

	/* Read settings from ccs to override the defaults above.
	   (with some appropriate helper functions around ccs_get, the
	   following can be pretty compact)

           mode from
           "/cluster/logging/@to_stderr"
           "/cluster/logging/@to_syslog"
           "/cluster/logging/@to_file"

           facility from
           "/cluster/logging/@syslog_facility"

           priority from
           "/cluster/logging/logger_subsys[ subsys=\"prog_name\"]/@syslog_level"

           file from
           "/cluster/logging/@filename"

           debug from
           "/cluster/logging/logger_subsys[ subsys=\"prog_name\"]/@debug"
        */

	*mode = LOG_MODE_OUTPUT_FILE;
	*priority = LOG_LEVEL_DEBUG;
	strcpy(file, "/tmp/testprog.log");

	return 0;
}

/* this function will also be called when we get a cman config-update event */

void setup_logging(int *prog_debug)
{
	int mode, facility, priority;
	char file[1024];

	/* The debug setting is special, it's used by the program
	   and not used to configure logsys. */

	read_ccs_logging(&mode, &facility, &priority, file, prog_debug);
	logsys_conf(prog_name, mode, facility, priority, file);
}

int main(int argc, char **argv)
{
	prog_name = argv[0];

	logsys_init(prog_name, DEFAULT_MODE, DEFAULT_FACILITY,
		    DEFAULT_PRIORITY, DEFAULT_FILE);

	log_printf(LOG_LEVEL_ERROR, "error test 1\n");
	log_printf(LOG_LEVEL_DEBUG, "debug test 1\n");

	setup_logging(&prog_debug);

	log_printf(LOG_LEVEL_ERROR, "error test 2\n");
	log_printf(LOG_LEVEL_DEBUG, "debug test 2\n");
}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]