(!1688) os: log: add syslog support
Add support for logging to syslog. Signed-off-by: Enrico Weigelt, metux IT consult <info@metux.net>
This commit is contained in:
		
							parent
							
								
									1fbf6d9b21
								
							
						
					
					
						commit
						a406c71576
					
				| 
						 | 
				
			
			@ -24,6 +24,7 @@ is" without express or implied warranty.
 | 
			
		|||
#include "mi/mi_priv.h"
 | 
			
		||||
#include "miext/extinit_priv.h"
 | 
			
		||||
#include "os/ddx_priv.h"
 | 
			
		||||
#include "os/log_priv.h"
 | 
			
		||||
#include "os/osdep.h"
 | 
			
		||||
 | 
			
		||||
#include "screenint.h"
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										38
									
								
								os/log.c
								
								
								
								
							
							
						
						
									
										38
									
								
								os/log.c
								
								
								
								
							| 
						 | 
				
			
			@ -88,6 +88,10 @@ OR PERFORMANCE OF THIS SOFTWARE.
 | 
			
		|||
#include <X11/Xfuncproto.h>
 | 
			
		||||
#include <X11/Xos.h>
 | 
			
		||||
 | 
			
		||||
#ifdef CONFIG_SYSLOG
 | 
			
		||||
#include <syslog.h>
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#include "dix/dix_priv.h"
 | 
			
		||||
#include "dix/input_priv.h"
 | 
			
		||||
#include "os/audit.h"
 | 
			
		||||
| 
						 | 
				
			
			@ -110,11 +114,16 @@ OR PERFORMANCE OF THIS SOFTWARE.
 | 
			
		|||
/* Default logging parameters. */
 | 
			
		||||
#define DEFAULT_LOG_VERBOSITY		0
 | 
			
		||||
#define DEFAULT_LOG_FILE_VERBOSITY	3
 | 
			
		||||
#define DEFAULT_SYSLOG_VERBOSITY	0
 | 
			
		||||
 | 
			
		||||
static int logFileFd = -1;
 | 
			
		||||
Bool xorgLogSync = FALSE;
 | 
			
		||||
int xorgLogVerbosity = DEFAULT_LOG_VERBOSITY;
 | 
			
		||||
int xorgLogFileVerbosity = DEFAULT_LOG_FILE_VERBOSITY;
 | 
			
		||||
#ifdef CONFIG_SYSLOG
 | 
			
		||||
int xorgSyslogVerbosity = DEFAULT_SYSLOG_VERBOSITY;
 | 
			
		||||
const char *xorgSyslogIdent = "X";
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
/* Buffer to information logged before the log file is opened. */
 | 
			
		||||
static char *saveBuffer = NULL;
 | 
			
		||||
| 
						 | 
				
			
			@ -206,6 +215,18 @@ static inline void doLogSync(void) {
 | 
			
		|||
#endif
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void initSyslog(void) {
 | 
			
		||||
#ifdef CONFIG_SYSLOG
 | 
			
		||||
    char buffer[4096];
 | 
			
		||||
    strcpy(buffer, xorgSyslogIdent);
 | 
			
		||||
 | 
			
		||||
    snprintf(buffer, sizeof(buffer), "%s :%s", xorgSyslogIdent, (display ? display : "<>"));
 | 
			
		||||
 | 
			
		||||
    /* initialize syslog */
 | 
			
		||||
    openlog(buffer, LOG_PID, LOG_LOCAL1);
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * LogInit is called to start logging to a file.  It is also called (with
 | 
			
		||||
 * NULL arguments) when logging to a file is not wanted.  It must always be
 | 
			
		||||
| 
						 | 
				
			
			@ -263,6 +284,7 @@ LogInit(const char *fname, const char *backup)
 | 
			
		|||
    }
 | 
			
		||||
    needBuffer = FALSE;
 | 
			
		||||
 | 
			
		||||
    initSyslog();
 | 
			
		||||
    return logFileName;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -294,6 +316,7 @@ LogSetDisplay(void)
 | 
			
		|||
        free(saved_log_fname);
 | 
			
		||||
        free(saved_log_backup);
 | 
			
		||||
    }
 | 
			
		||||
    initSyslog();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
| 
						 | 
				
			
			@ -528,6 +551,19 @@ vpnprintf(char *string, int size_in, const char *f, va_list args)
 | 
			
		|||
    return s_idx;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
LogSyslogWrite(int verb, const char *buf, size_t len, Bool end_line) {
 | 
			
		||||
#ifdef CONFIG_SYSLOG
 | 
			
		||||
    if (inSignalContext) // syslog() ins't signal-safe yet :(
 | 
			
		||||
        return;          // shall we try syslog(2) syscall instead ?
 | 
			
		||||
 | 
			
		||||
    if (verb >= 0 && xorgSyslogVerbosity < verb)
 | 
			
		||||
        return;
 | 
			
		||||
 | 
			
		||||
    syslog(LOG_PID, "%.*s", (int)len, buf);
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* This function does the actual log message writes. It must be signal safe.
 | 
			
		||||
 * When attempting to call non-signal-safe functions, guard them with a check
 | 
			
		||||
 * of the inSignalContext global variable. */
 | 
			
		||||
| 
						 | 
				
			
			@ -537,6 +573,8 @@ LogSWrite(int verb, const char *buf, size_t len, Bool end_line)
 | 
			
		|||
    static Bool newline = TRUE;
 | 
			
		||||
    int ret;
 | 
			
		||||
 | 
			
		||||
    LogSyslogWrite(verb, buf, len, end_line);
 | 
			
		||||
 | 
			
		||||
    if (verb < 0 || xorgLogVerbosity >= verb)
 | 
			
		||||
        ret = write(2, buf, len);
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -72,4 +72,21 @@ extern int xorgLogFileVerbosity;
 | 
			
		|||
 */
 | 
			
		||||
extern Bool xorgLogSync;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @brief syslog verbosity
 | 
			
		||||
 *
 | 
			
		||||
 * The verbosity level of logging to syslog. All messages with
 | 
			
		||||
 * verbosity level below this one will be sent to local syslog daemon.
 | 
			
		||||
 */
 | 
			
		||||
extern int xorgSyslogVerbosity;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @brief syslog identifier
 | 
			
		||||
 *
 | 
			
		||||
 * The identifier prefix used for syslog logging.
 | 
			
		||||
 * Per default will be filled with basename(argv[0]). DDX'es can override
 | 
			
		||||
 * this before calling LogInit()
 | 
			
		||||
 */
 | 
			
		||||
extern const char *xorgSyslogIdent;
 | 
			
		||||
 | 
			
		||||
#endif /* __XORG_OS_LOGGING_H */
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -23,6 +23,8 @@ srcs_os = [
 | 
			
		|||
    'log.c',
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
conf_data.set('CONFIG_SYSLOG', cc.has_header('syslog.h') ? '1' : false)
 | 
			
		||||
 | 
			
		||||
# Wrapper code for missing C library functions. Note that conf_data contains either '1' or false.
 | 
			
		||||
srcs_libc = []
 | 
			
		||||
if conf_data.get('HAVE_REALLOCARRAY').to_int() == 0
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										11
									
								
								os/utils.c
								
								
								
								
							
							
						
						
									
										11
									
								
								os/utils.c
								
								
								
								
							| 
						 | 
				
			
			@ -67,6 +67,8 @@ OR PERFORMANCE OF THIS SOFTWARE.
 | 
			
		|||
#define TRANS_REOPEN
 | 
			
		||||
#include <X11/Xtrans/Xtrans.h>
 | 
			
		||||
 | 
			
		||||
#include <libgen.h>
 | 
			
		||||
 | 
			
		||||
#include "input.h"
 | 
			
		||||
#include "dixfont.h"
 | 
			
		||||
#include <X11/fonts/libxfont2.h>
 | 
			
		||||
| 
						 | 
				
			
			@ -420,6 +422,12 @@ ProcessCommandLine(int argc, char *argv[])
 | 
			
		|||
    }
 | 
			
		||||
    SeatId = getenv("XDG_SEAT");
 | 
			
		||||
 | 
			
		||||
#ifdef CONFIG_SYSLOG
 | 
			
		||||
    xorgSyslogIdent = getenv("SYSLOG_IDENT");
 | 
			
		||||
    if (!xorgSyslogIdent)
 | 
			
		||||
        xorgSyslogIdent = strdup(basename(argv[0]));
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
    for (i = 1; i < argc; i++) {
 | 
			
		||||
        /* call ddx first, so it can peek/override if it wants */
 | 
			
		||||
        if ((skip = ddxProcessArgument(argc, argv, i))) {
 | 
			
		||||
| 
						 | 
				
			
			@ -756,6 +764,9 @@ ProcessCommandLine(int argc, char *argv[])
 | 
			
		|||
            else
 | 
			
		||||
                UseMsg();
 | 
			
		||||
        }
 | 
			
		||||
#ifdef CONFIG_SYSLOG
 | 
			
		||||
        else if (ProcessCmdLineMultiInt(argc, argv, &i, "-syslogverbose", &xorgSyslogVerbosity));
 | 
			
		||||
#endif
 | 
			
		||||
        else {
 | 
			
		||||
            ErrorF("Unrecognized option: %s\n", argv[i]);
 | 
			
		||||
            UseMsg();
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue