making (hopefully) the logging system thread-safe

This commit is contained in:
ilsimo 2006-06-04 12:10:02 +00:00
parent e9400d44e9
commit 9e2235ceaa
2 changed files with 35 additions and 42 deletions

View File

@ -15,12 +15,6 @@
xrdp: A Remote Desktop Protocol server.
Copyright (C) Jay Sorg 2005-2006
session manager
linux only
log.c: logging code
*/
#include "sys/types.h"
@ -37,12 +31,17 @@
static struct log_config *l_cfg;
/* threading additions */
#ifdef LOG_ENABLE_THREAD
#include "nptl/pthread.h"
static pthread_mutex_t log_lock;
static pthread_mutexattr_t log_lock_attr;
#endif
/**
*
* Opens log file
*
* @brief Opens log file
* @param fname log file name
*
* @return see open(2) return values
*
*/
@ -53,10 +52,8 @@ static int log_file_open(const char* fname)
/**
*
* Converts xrdp log level to syslog logging level
*
* @brief Converts xrdp log level to syslog logging level
* @param xrdp logging level
*
* @return syslog equivalent logging level
*
*/
@ -79,11 +76,10 @@ static int log_xrdp2syslog(const int lvl)
}
/**
*
* Converts xrdp log level to syslog logging level
*
* @param xrdp logging level
*
*ring
* @brief Converts xrdp log level to syslog logging level
* @param lvl logging level
* @param str pointer to a st
* @return syslog equivalent logging level
*
*/
@ -167,8 +163,15 @@ log_message(const unsigned int lvl, const char* msg, ...)
{
/* log to console */
g_printf((char*) buff);
/* log to application logfile */
#ifdef LOG_ENABLE_THREAD
pthread_mutex_lock(&log_lock);
#endif
return g_file_write(l_cfg->fd, (char*) buff, g_strlen((char*) buff));
#ifdef LOG_ENABLE_THREAD
pthread_mutex_unlock(&log_lock);
#endif
}
return 0;
}
@ -225,6 +228,11 @@ log_start(const char* progname, const char* logfile, const unsigned int loglvl,
/* if syslog is enabled, open it */
if (l_cfg->enable_syslog) openlog(l_cfg->program_name, LOG_CONS | LOG_PID, LOG_DAEMON);
#ifdef LOG_ENABLE_THREAD
pthread_mutexattr_init(&log_lock_attr);
pthread_mutex_init(&log_lock, &log_lock_attr);
#endif
return LOG_STARTUP_OK;
}
@ -295,4 +303,3 @@ log_text2level(char* buf)
return LOG_LEVEL_DEBUG;
}

View File

@ -15,12 +15,6 @@
xrdp: A Remote Desktop Protocol server.
Copyright (C) Jay Sorg 2005-2006
session manager
linux only
log.h: logging code declarations
*/
#ifndef LOG_H
@ -29,7 +23,7 @@
#include "arch.h"
/* logging buffer size */
#define LOG_BUFFER_SIZE 8192
#define LOG_BUFFER_SIZE 1024
/* logging levels */
#define LOG_LEVEL_ALWAYS 0
@ -46,6 +40,9 @@
#define LOG_ERROR_NO_CFG 4
#define LOG_ERROR_FILE_NOT_OPEN 5
/* enable threading */
/*#define LOG_ENABLE_THREAD*/
#ifdef DEBUG
#define LOG_DBG(s,args...) log_message(LOG_LEVEL_DEBUG,s,args);
#else
@ -64,12 +61,9 @@ struct log_config
/**
*
* Logs a message. Optionally logs the same message on syslog
*
* @brief Logs a message. Optionally logs the same message on syslog
* @param lvl The level of the logged message
*
* @param msg The message to be logged
*
* @return
*
*/
@ -78,18 +72,12 @@ log_message(const unsigned int lvl, const char* msg, ...);
/**
*
* Starts the logging subsystem
*
* @brief Starts the logging subsystem
* @param progname string to prepend to syslog messages
*
* @param logfile log file path
*
* @param loglvl level of messages to log
*
* @param syslog if set to 0, disables the use of syslog
*
* @param syslvl level of messages to log to syslog
*
* @return
*
*/
@ -99,7 +87,7 @@ log_start(const char* progname, const char* logfile, const unsigned int loglvl,
/**
*
* Shuts down the logging subsystem
* @brief Shuts down the logging subsystem
*
*/
void DEFAULT_CC
@ -107,11 +95,9 @@ log_end();
/**
*
* Converts a string to a log level
*
* @s The string to convert
*
* @return The corresponding level od LOG_LEVEL_DEBUG if error
* @brief Converts a string to a log level
* @param s The string to convert
* @return The corresponding level or LOG_LEVEL_DEBUG if error
*
*/
int DEFAULT_CC