summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorRadek Novacek <rnovacek@redhat.com>2013-09-12 16:02:28 +0200
committerRadek Novacek <rnovacek@redhat.com>2013-09-19 09:19:35 +0200
commitba68ac8759f664b9079ca9d5b6772285f6433be6 (patch)
tree61bd6fd11741abeccdb84687d4822ea0e1cb982c /src
parent0f356df6cc42573ed7bf91ffb4ba8da4f516f5b4 (diff)
downloadopenlmi-providers-ba68ac8759f664b9079ca9d5b6772285f6433be6.tar.gz
openlmi-providers-ba68ac8759f664b9079ca9d5b6772285f6433be6.tar.xz
openlmi-providers-ba68ac8759f664b9079ca9d5b6772285f6433be6.zip
Improve logging in C providers
Use CMLogMessage instead of CMTraceMessage for each message with level > ERROR. The logging level is now read from the config file. The Stderr option for logging to stderr in now also honored.
Diffstat (limited to 'src')
-rw-r--r--src/openlmi.c77
-rw-r--r--src/openlmi.h23
2 files changed, 89 insertions, 11 deletions
diff --git a/src/openlmi.c b/src/openlmi.c
index ac539bd..25cf57c 100644
--- a/src/openlmi.c
+++ b/src/openlmi.c
@@ -22,7 +22,6 @@
#include <stdlib.h>
#include <stdarg.h>
-#include <stdbool.h>
#include <unistd.h>
#include <sys/utsname.h>
#include <string.h>
@@ -32,7 +31,6 @@
#include <glib.h>
static char *_fqdn = NULL;
-static int _log_level = _LMI_DEBUG_DEBUG;
static const CMPIBroker *_cb = NULL;
static char *_provider = NULL;
@@ -46,6 +44,12 @@ static pthread_mutex_t _init_mutex = PTHREAD_MUTEX_INITIALIZER;
// Cache for SystemCreationClassName
static char *_system_creation_class_name = NULL;
+// Cache for log level
+static int _log_level = _LMI_DEBUG_NONE;
+
+// Cache for log stderr
+static bool _log_stderr = false;
+
static ConfigEntry _toplevel_config_defaults[] = {
{ "CIM", "Namespace", "root/cimv2" },
{ "CIM", "SystemClassName", "Linux_ComputerSystem" },
@@ -137,6 +141,24 @@ char *lmi_read_config(const char *group, const char *key)
return g_key_file_get_string(_masterKeyFile, group, key, NULL);
}
+bool lmi_read_config_boolean(const char *group, const char *key)
+{
+ char *value = lmi_read_config(group, key);
+ if (value == NULL) {
+ return false;
+ }
+ char *true_values[] = { "1", "yes", "true", "on" };
+ size_t len = sizeof(true_values) / sizeof(true_values[0]);
+ for (size_t i = 0; i < len; ++i) {
+ if (strcasecmp(true_values[i], value) == 0) {
+ free(value);
+ return true;
+ }
+ }
+ free(value);
+ return false;
+}
+
GKeyFile *parse_config(const char *provider_name, const ConfigEntry *provider_config_defaults)
{
GError *error = NULL;
@@ -241,6 +263,8 @@ void lmi_init(const char *provider, const CMPIBroker *cb, const ConfigEntry *pro
}
// Read config only on first call of this function
_masterKeyFile = parse_config(provider, provider_config_defaults);
+ // We might read different log config, reread it in next _log_debug call
+ _log_level = _LMI_DEBUG_NONE;
pthread_mutex_unlock(&_init_mutex);
}
@@ -262,6 +286,24 @@ void lmi_set_log_level(int level)
void _lmi_debug(int level, const char *file, int line, const char *format, ...)
{
const char *lvl[] = { "NONE", "ERROR", "WARNING", "INFO", "DEBUG" };
+ if (_log_level == _LMI_DEBUG_NONE) {
+ // Read log level from config or default
+ _log_level = _LMI_DEBUG_ERROR; // Default
+ char *level = lmi_read_config("Log", "Level");
+ if (level != NULL) {
+ size_t len = sizeof(lvl) / sizeof(lvl[0]);
+ for (size_t i = 0; i < len; ++i) {
+ if (strcasecmp(level, lvl[i]) == 0) {
+ _log_level = i;
+ break;
+ }
+ }
+ free(level);
+ }
+
+ // Read if log to stderr
+ _log_stderr = lmi_read_config_boolean("Log", "Stderr");
+ }
if (level > 4) {
level = 4;
}
@@ -269,6 +311,11 @@ void _lmi_debug(int level, const char *file, int line, const char *format, ...)
level = 1;
}
+ if (level > _log_level) {
+ // Do not log this message
+ return;
+ }
+
char *message, *text;
va_list args;
va_start(args, format);
@@ -281,16 +328,28 @@ void _lmi_debug(int level, const char *file, int line, const char *format, ...)
rc.rc = CMPI_RC_OK;
if (_cb != NULL) {
// try to use standard CMPI logging
- rc = _cb->eft->trace(_cb, CMPI_LEV_INFO, _provider, text, NULL);
- }
- if (_cb == NULL || rc.rc != CMPI_RC_OK) {
- // Fallback to stderr
- if (level > _log_level) {
- free(text);
- return;
+ // CMPI has different severity levels (1=info, 4=fatal)
+ int severity = CMPI_SEV_INFO;
+ switch (level) {
+ case _LMI_DEBUG_DEBUG:
+ severity = CMPI_DEV_DEBUG;
+ break;
+ case _LMI_DEBUG_INFO:
+ severity = CMPI_SEV_INFO;
+ break;
+ case _LMI_DEBUG_WARN:
+ severity = CMPI_SEV_WARNING;
+ break;
+ case _LMI_DEBUG_ERROR:
+ severity = CMPI_SEV_ERROR;
+ break;
}
+ rc = CMLogMessage(_cb, severity, _provider, text, NULL);
+ }
+ if (_log_stderr || _cb == NULL || rc.rc != CMPI_RC_OK) {
+ // Fallback to stderr
fprintf(stderr, "%s\n", text);
}
free(text);
diff --git a/src/openlmi.h b/src/openlmi.h
index 93778ce..7d64bd1 100644
--- a/src/openlmi.h
+++ b/src/openlmi.h
@@ -22,6 +22,7 @@
#define OPENLMI_H
#include <cmpidt.h>
+#include <stdbool.h>
typedef struct {
const char *group;
@@ -78,11 +79,26 @@ void lmi_init(const char *provider, const CMPIBroker *cb,
char *lmi_read_config(const char *group, const char *key);
/**
+ * Reads a boolean value out of configuration files or default configuration
+ * options.
+ *
+ * Values "1", "yes", "true", and "on" are converted to TRUE, others to FALSE
+ *
+ * @param group Configration group
+ * @param key Configration key
+ * @return Boolean value of the key, false if the key is not in the
+ * configuration files neither in default options.
+ */
+bool lmi_read_config_boolean(const char *group, const char *key);
+
+/**
* To use standard CIMOM logging facility, broker must be assigned. Without
* calling this function, logging will go to stderr.
*
- * \p log_id Identification of log messages
- * \p cb CMPIBroker
+ * @deprecated Use lmi_init instead
+ *
+ * @param log_id Identification of log messages
+ * @param cb CMPIBroker
*/
void lmi_init_logging(const char *log_id, const CMPIBroker *cb);
@@ -96,6 +112,9 @@ int lmi_log_level(void);
/**
* Set logging level
*
+ * @note This method shouldn't be used directly, user setting
+ * from the configuration file should be honored
+ *
* @param level new logging level
*/
void lmi_set_log_level(int level);