[PATCH] Add first simple event logging functions
Wald Commits
scm-commit at wald.intevation.org
Thu Jun 19 11:53:41 CEST 2014
# HG changeset patch
# User Andre Heinecke <andre.heinecke at intevation.de>
# Date 1403171587 -7200
# Node ID 2a4f7364ab8150b88e5e4f43604418e4f4f72f48
# Parent de1e3a47ed210621a1c5959979102dcd0d779e37
Add first simple event logging functions
diff -r de1e3a47ed21 -r 2a4f7364ab81 common/logging.c
--- a/common/logging.c Wed Jun 18 16:35:03 2014 +0200
+++ b/common/logging.c Thu Jun 19 11:53:07 2014 +0200
@@ -9,8 +9,80 @@
#include "strhelp.h"
#include <stdio.h>
+#include <stdarg.h>
+#include <stdbool.h>
+
+#include <strhelp.h>
#ifdef WIN32
+# include <windows.h>
+#else
+# include <syslog.h>
+#endif
+
+
+#ifdef WIN32
+static void
+win_log(const char *format, va_list ap, bool error)
+{
+ HANDLE log_src = NULL;
+ wchar_t *wmsg = NULL;
+ BOOL failure = TRUE;
+ WORD type = 0,
+ category = 0;
+ char buffer[MAX_LOG+1];
+
+ vsnprintf (buffer, MAX_LOG, format, ap);
+ buffer[MAX_LOG] = '\0';
+
+ log_src = RegisterEventSourceA (NULL, LOG_NAME);
+
+ if (log_src == NULL)
+ {
+ PRINTLASTERROR ("Failed to open log source.");
+ return;
+ }
+
+ if (error)
+ {
+ type = EVENTLOG_ERROR_TYPE;
+ }
+ else
+ {
+ type = EVENTLOG_INFORMATION_TYPE;
+ }
+
+ wmsg = utf8_to_wchar (buffer, strlen(buffer));
+ if (wmsg == NULL)
+ {
+ ERRORPRINTF ("Failed to convert log message to utf-16");
+ goto done;
+ }
+
+ failure = ReportEventW (log_src,
+ type,
+ category,
+ 0,
+ NULL,
+ 1,
+ 0,
+ (const WCHAR **) &wmsg,
+ NULL);
+ if (failure)
+ {
+ PRINTLASTERROR ("Failed to report event.");
+ }
+
+done:
+ xfree (wmsg);
+
+ if (!DeregisterEventSource (log_src))
+ {
+ PRINTLASTERROR ("Failed to close log source.");
+ }
+ return;
+}
+
char *
getLastErrorMsg()
{
@@ -44,4 +116,40 @@
return retval;
}
+#else /* WIN32 */
+
+
+static void
+linux_log (const char *format, va_list ap, bool error)
+{
+ openlog (LOG_NAME, LOG_CONS | LOG_PID | LOG_NDELAY, LOG_USER);
+ vsyslog ( error ? LOG_ERR : LOG_INFO, format, ap);
+}
+
+#endif /* WIN32 */
+
+void
+syslog_info_printf(const char *format, ...)
+{
+ va_list args;
+ va_start (args, format);
+#ifdef WIN32
+ win_log (format, args, false);
+#else
+ linux_log (format, args, false);
#endif
+ va_end (args);
+}
+
+void
+syslog_error_printf(const char *format, ...)
+{
+ va_list args;
+ va_start (args, format);
+#ifdef WIN32
+ win_log (format, args, true);
+#else
+ linux_log (format, args, true);
+#endif
+ va_end (args);
+}
diff -r de1e3a47ed21 -r 2a4f7364ab81 common/logging.h
--- a/common/logging.h Wed Jun 18 16:35:03 2014 +0200
+++ b/common/logging.h Thu Jun 19 11:53:07 2014 +0200
@@ -19,6 +19,12 @@
#include <stdio.h>
+/** @def Maximum length of log messages */
+#define MAX_LOG 511
+
+/** @def The name used for logging */
+#define LOG_NAME "TrustBridge"
+
#ifdef WIN32
#include <windows.h>
@@ -108,6 +114,26 @@
ERRORPRINTF ("Failed to get error information\n");
+/**
+ * @brief log an informational message into the syslog / event log
+ *
+ * The message length is limited to MAX_LOG characters. Log messages
+ * are expected to be in UTF-8 encoding.
+ *
+ * Function paramters are the same as for the printf familiy.
+ */
+void syslog_info_printf(const char *format, ...);
+
+/**
+ * @brief log an error message into the syslog / event log
+ *
+ * The message length is limited to MAX_LOG characters. Log messages
+ * are expected to be in UTF-8 encoding.
+ *
+ * Function paramters are the same as for the printf familiy.
+ */
+void syslog_error_printf(const char *format, ...);
+
#ifdef __cplusplus
}
#endif
More information about the Trustbridge-commits
mailing list