Hi guys,
this is a major rework, and hopefully the last one, for logsys.
The patch is big, so I strongly recommend to read the new code rather
than looking at the diff itself.
Those are the major changes:
logsys.h:
- add lots of comments and make it more tidy.
- drop of the whole NOSUBSYS concept. Absorbed into code.
- reduce the Internal API exports.
- drop logsys_init and logsys_conf.
- drop logsys_atsegv as it was a 100% dup of logsys_atexit.
- add all possible runtime configuration options.
- from logsys.h: LOGSYS_DECLARE_SUBSYS needs a feature review (see
below).
logsys.c:
- increase SUBSYS_MAX to 63. Allocate one extra for SYSTEM (or default
settings).
- make it so that SYSTEM holds the application defaults and SUBSYSTEM
can override SYSTEM settings (see also below).
- unify the concept of SYSTEM and SUBSYSTEM within the same data
structure.
- make output to targets flexible by subsystem/system priority.
- fix a bug (also present in current implementation) where loglevel was
being augmented by one for no reasons (decreasing the output).
- add a bunch of static helper functions to reduce code duplications a
lot.
- add _logsys_system_setup helper to init system and defaults. This
reduces the amount of code to be built in the construct header.
- SUBSYSTEM creation automatically inherit default settings from SYSTEM.
- cleanup usage of subsys and subsysid to be consistent in functions
interfaces.
- give a pretty good boost to error return codes.
So generally an example code with the new interface will look like:
#include <corosync/engine/logsys.h>
LOGSYS_DECLARE_SYSTEM("FOO",
LOG_MODE_OUTPUT_FILE | LOG_MODE_OUTPUT_SYSLOG |
LOG_MODE_OUTPUT_STDERR | LOG_MODE_THREADED |
LOG_MODE_FORK,
0, /* debug */
"/root/test.log", /* logfile */
LOG_LEVEL_INFO, /* logfile_priority */
LOG_DAEMON, /* syslog facility */
LOG_LEVEL_INFO, /* syslog level */
0, /* tags */
NULL, /* use default format */
1000000); /* flight recorder size */
and this will set and init the logging for the whole application.
Optionally for each file you can create a subsystem:
LOGSYS_DECLARE_SUBSYS("main")
if not specified the file.c will automatically use the main settings.
The new API allows configuration changes to all entries both for the
SYSTEM and SUBSYSTEM settings..
So it means that you can do at runtime:
logsys_config_file_set("FOO", &err, "/root/newfile.log");
or
logsys_config_file_set(NULL, &err, "/root/newfile.log");
(the two calls above are equivalent. you can either specify the SYSTEM
name or pass NULL and default to SYSTEM).
and everything outside a subsystem will start use the new logfile.
the SUBSYSTEM "main" is not affected by the change.
This is now true for all options in a consistent way.
----
This new implementation allows a great deal of flexibility and maintain
all the core features of logsys v2 such as threading and flight
recorder.
At this point in time I have only a bunch of test files to use against
the new implementation.
Before porting corosync/openais, I'd like to finalize the interface in
case something is not right.
Specifically I am not able to make up my mind about
LOGSYS_DECLARE_SUBSYS. Should it provide a similar interface as
DECLARE_SYSTEM to allow immediate overrides? or should we keep it simple
and just let the user do runtime config changes to the SUBSYSTEM when
required? Both and pro and cons..
I have also consider this option (pseudo code warning):
#define LOGSYS_DECLARE_SUBSYS(subsys,config_override_function)
[SNIP]
if config_override_function {
config_override_function(subsys);
}
Cheers
Fabio
Index: include/corosync/engine/logsys.h
===================================================================
--- include/corosync/engine/logsys.h (revision 1991)
+++ 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,137 @@
#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);
+extern void _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 int logsys_config_file_set (
- const char **error_string,
- const char *file);
+extern int _logsys_config_subsys_get (
+ const char *subsys);
-extern void logsys_config_facility_set (
- const char *name,
- unsigned int facility);
+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 void logsys_format_set (
- char *format);
+ 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 +231,35 @@
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 (101))) 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 (101))) \
+static void logsys_system_init (void) \
{ \
- const char *error_string; \
- \
- logsys_config_mode_set (mode); \
- logsys_config_file_set (&error_string, (file)); \
- logsys_config_facility_set (name, (facility)); \
+ _logsys_system_setup (name,mode,debug,file,file_priority, \
+ syslog_facility,syslog_priority,tags); \
logsys_format_set (format); \
_logsys_rec_init (rec_size); \
_logsys_wthread_create(); \
}
-#define LOGSYS_DECLARE_NOSUBSYS(priority) \
-__attribute__ ((constructor (102))) static void logsys_nosubsys_init (void) \
+#define LOGSYS_DECLARE_SUBSYS(subsys) \
+__attribute__ ((constructor (102))) \
+static void logsys_subsys_init (void) \
{ \
- unsigned int pri, tags; \
- \
logsys_subsys_id = \
- logsys_config_subsys_get("MAIN", &tags, &pri); \
+ _logsys_config_subsys_get((subsys)); \
\
- if (logsys_subsys_id == -1) { \
- _logsys_nosubsys_set(); \
- logsys_subsys_id = \
- _logsys_subsys_create ("MAIN", (priority)); \
- } \
-}
-
-#define LOGSYS_DECLARE_SUBSYS(subsys,priority) \
-__attribute__ ((constructor (102))) 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)); \
}
#define log_rec(rec_ident, args...) \
@@ -277,26 +324,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 */
Index: exec/logsys.c
===================================================================
--- exec/logsys.c (revision 1992)
+++ exec/logsys.c (working copy)
@@ -96,35 +96,29 @@
int flt_data_size;
-#define SUBSYS_MAX 32
+#define SUBSYS_MAX 63
#define COMBINE_BUFFER_SIZE 2048
+/* need unlogical order to preserve 64bit alignment */
struct logsys_logger {
- char subsys[64];
- unsigned int priority;
- unsigned int tags;
- unsigned int mode;
+ char subsys[64]; /* 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 */
};
/*
- * 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 +155,6 @@
#define FDTAIL_INDEX (flt_data_size + 1)
-static void logsys_atexit (void);
-
/*
* Helpers for _logsys_log_rec functionality
*/
@@ -204,7 +196,6 @@
}
#endif
-
/*
* Before any write operation, a reclaim on the buffer area must be executed
*/
@@ -305,7 +296,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 +307,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 +372,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,7 +446,7 @@
(char *)arguments[1],
(char *)arguments[2],
file_line,
- level,
+ (level-1),
(char *)arguments[3]);
}
@@ -582,32 +586,187 @@
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;
+ }
+
+ return;
+}
+
+/*
+ * 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];
+
+ 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);
+ 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;
+}
+
+/*
* Internal API - exported
*/
-void _logsys_nosubsys_set (void)
+
+void _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;
+
+ i = SUBSYS_MAX;
+
+ pthread_mutex_lock (&logsys_config_mutex);
+
+ snprintf(logsys_loggers[i].subsys,
+ sizeof(logsys_loggers[i].subsys),
+ "%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;
+
+ pthread_mutex_unlock (&logsys_config_mutex);
}
-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) {
+ memcpy(&logsys_loggers[i],
+ &logsys_loggers[SUBSYS_MAX],
+ sizeof(logsys_loggers[SUBSYS_MAX]));
+ strncpy (logsys_loggers[i].subsys, subsys,
+ sizeof(logsys_loggers[i].subsys));
+ logsys_loggers[i].tags = LOGSYS_TAG_LOG;
+ 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) {
wthread_create();
atexit (logsys_atexit);
}
@@ -651,7 +810,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 +849,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 +962,7 @@
}
void _logsys_log_printf (
- int subsys,
+ int subsysid,
const char *function_name,
const char *file_name,
int file_line,
@@ -815,12 +974,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 +989,7 @@
/*
* Create a log record
*/
- _logsys_log_rec (subsys,
+ _logsys_log_rec (subsysid,
function_name,
file_name,
file_line,
@@ -840,12 +997,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,67 +1013,104 @@
}
}
+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;
}
-void logsys_format_set (char *format)
+void logsys_format_set (const char *format)
{
pthread_mutex_lock (&logsys_config_mutex);
@@ -939,16 +1133,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;
@@ -1021,63 +1276,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;
@@ -1101,7 +1299,7 @@
return (0);
}
-static void logsys_atexit (void)
+void logsys_atexit (void)
{
if (wthread_active) {
wthread_should_exit = 1;
@@ -1109,58 +1307,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);
- logsys_format_set (format);
- _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
[email protected]
https://lists.linux-foundation.org/mailman/listinfo/openais