From c3f98b388a9979d820eb6d8104f8c17514c87f50 Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Sun, 9 Nov 2014 17:04:25 -0800 Subject: [PATCH] GUAC-911: Only log up to specified log level. Add comments to log.h. --- src/guacd/daemon.c | 3 ++- src/guacd/log.c | 10 +++++++++- src/guacd/log.h | 37 ++++++++++++++++++++++++++++++++++++- 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/src/guacd/daemon.c b/src/guacd/daemon.c index 58fa5efa..45ca1f82 100644 --- a/src/guacd/daemon.c +++ b/src/guacd/daemon.c @@ -392,7 +392,8 @@ int main(int argc, char* argv[]) { /* Set up logging prefix */ strncpy(log_prefix, basename(argv[0]), sizeof(log_prefix)); - /* Open log as early as we can */ + /* Init logging as early as possible */ + log_level = config->max_log_level; openlog("guacd", LOG_PID, LOG_DAEMON); /* Log start */ diff --git a/src/guacd/log.c b/src/guacd/log.c index adaf7401..c6252ac3 100644 --- a/src/guacd/log.c +++ b/src/guacd/log.c @@ -21,6 +21,7 @@ */ #include "config.h" +#include "log.h" #include #include @@ -33,14 +34,21 @@ /* Log prefix, defaulting to "guacd" */ char log_prefix[64] = "guacd"; +int log_level = GUAC_LOG_INFO; + void vguacd_log(guac_client_log_level level, const char* format, va_list args) { const char* priority_name; int priority; - /* Copy log message into buffer */ char message[2048]; + + /* Don't bother if the log level is too high */ + if (level > log_level) + return; + + /* Copy log message into buffer */ vsnprintf(message, sizeof(message), format, args); /* Convert log level to syslog priority */ diff --git a/src/guacd/log.h b/src/guacd/log.h index 922f102f..644f6a32 100644 --- a/src/guacd/log.h +++ b/src/guacd/log.h @@ -28,14 +28,49 @@ #include +/** + * The maximum level at which to log messages. All other messages will be + * dropped. + */ +extern int log_level; + +/** + * The string to prepend to all log messages. + */ extern char log_prefix[64]; +/** + * Writes a message to guacd's logs. This function takes a format and va_list, + * similar to vprintf. + */ void vguacd_log(guac_client_log_level level, const char* format, va_list args); + +/** + * Writes a message to guacd's logs. This function accepts parameters + * identically to printf. + */ void guacd_log(guac_client_log_level level, const char* format, ...); -void guacd_client_log(guac_client* client, guac_client_log_level level, const char* format, va_list args); +/** + * Writes a message using the logging facilities of the given client, + * automatically including any information present in guac_error. This function + * accepts parameters identically to printf. + */ +void guacd_client_log(guac_client* client, guac_client_log_level level, + const char* format, va_list args); +/** + * Prints an error message to guacd's logs, automatically including any + * information present in guac_error. This function accepts parameters + * identically to printf. + */ void guacd_log_guac_error(const char* message); + +/** + * Prints an error message using the logging facilities of the given client, + * automatically including any information present in guac_error. This function + * accepts parameters identically to printf. + */ void guacd_client_log_guac_error(guac_client* client, const char* message); #endif