[Groonga-commit] groonga/groonga at 7660cbc [master] Add Windows event log support

Back to archive index

Kouhei Sutou null+****@clear*****
Sun Jul 26 15:19:59 JST 2015


Kouhei Sutou	2015-07-26 15:19:59 +0900 (Sun, 26 Jul 2015)

  New Revision: 7660cbc842ab3c33ecdc1ba7ca729612cbb75105
  https://github.com/groonga/groonga/commit/7660cbc842ab3c33ecdc1ba7ca729612cbb75105

  Message:
    Add Windows event log support
    
    It's not tested yet.

  Added files:
    lib/windows_event_logger.c
  Copied files:
    include/groonga/windows_event_logger.h
      (from include/groonga.h)
  Modified files:
    include/groonga.h
    include/groonga/Makefile.am
    lib/sources.am

  Modified: include/groonga.h (+1 -0)
===================================================================
--- include/groonga.h    2015-07-26 11:54:09 +0900 (ebd8094)
+++ include/groonga.h    2015-07-26 15:19:59 +0900 (f2250c8)
@@ -27,5 +27,6 @@
 #include "groonga/util.h"
 #include "groonga/request_canceler.h"
 #include "groonga/thread.h"
+#include "groonga/windows_event_logger.h"
 
 #endif /* GROONGA_H */

  Modified: include/groonga/Makefile.am (+2 -1)
===================================================================
--- include/groonga/Makefile.am    2015-07-26 11:54:09 +0900 (b31c216)
+++ include/groonga/Makefile.am    2015-07-26 15:19:59 +0900 (af953e9)
@@ -16,4 +16,5 @@ groonga_include_HEADERS =			\
 	token_filter.h				\
 	nfkc.h					\
 	normalizer.h				\
-	util.h
+	util.h					\
+	windows_event_logger.h

  Copied: include/groonga/windows_event_logger.h (+19 -15) 63%
===================================================================
--- include/groonga.h    2015-07-26 11:54:09 +0900 (ebd8094)
+++ include/groonga/windows_event_logger.h    2015-07-26 15:19:59 +0900 (ec53125)
@@ -1,5 +1,5 @@
 /*
-  Copyright(C) 2014-2015 Brazil
+  Copyright(C) 2015 Brazil
 
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
@@ -15,17 +15,21 @@
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
-#ifndef GROONGA_H
-#define GROONGA_H
-
-#include "groonga/portability.h"
-#include "groonga/groonga.h"
-#include "groonga/obj.h"
-#include "groonga/ii.h"
-#include "groonga/expr.h"
-#include "groonga/output.h"
-#include "groonga/util.h"
-#include "groonga/request_canceler.h"
-#include "groonga/thread.h"
-
-#endif /* GROONGA_H */
+
+#ifndef GROONGA_WINDOWS_EVENT_LOGGER_H
+#define GROONGA_WINDOWS_EVENT_LOGGER_H
+
+#ifdef  __cplusplus
+extern "C" {
+#endif
+
+GRN_API const char *grn_windows_event_logger_get_source_name(void);
+GRN_API void grn_windows_event_logger_set_source_name(const char *name);
+
+GRN_API grn_rc grn_windows_event_logger_set(grn_ctx *ctx);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* GROONGA_WINDOWS_EVENT_LOGGER_H */

  Modified: lib/sources.am (+2 -1)
===================================================================
--- lib/sources.am    2015-07-26 11:54:09 +0900 (d4180a4)
+++ lib/sources.am    2015-07-26 15:19:59 +0900 (9d43e7a)
@@ -71,4 +71,5 @@ libgroonga_la_SOURCES =				\
 	grn_tokenizers.h			\
 	token_filter.c				\
 	util.c					\
-	grn_util.h
+	grn_util.h				\
+	windows_event_logger.c

  Added: lib/windows_event_logger.c (+247 -0) 100644
===================================================================
--- /dev/null
+++ lib/windows_event_logger.c    2015-07-26 15:19:59 +0900 (fff4f91)
@@ -0,0 +1,247 @@
+/* -*- c-basic-offset: 2 -*- */
+/*
+  Copyright(C) 2015 Brazil
+
+  This library is free software; you can redistribute it and/or
+  modify it under the terms of the GNU Lesser General Public
+  License version 2.1 as published by the Free Software Foundation.
+
+  This library is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public
+  License along with this library; if not, write to the Free Software
+  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+
+#include "grn_logger.h"
+#include "grn_ctx.h"
+
+#include <string.h>
+
+static char *windows_event_source_name = NULL;
+
+#ifdef WIN32
+typedef struct _grn_windows_event_logger_data {
+  HANDLE event_log;
+} grn_windows_event_logger_data;
+#endif /* WIN32 */
+
+const char *
+grn_windows_event_looger_get_source_name(void)
+{
+  return windows_event_source_name;
+}
+
+void
+grn_windows_event_looger_set_source_name(const char *name)
+{
+  if (windows_event_source_name) {
+    free(windows_event_source_name);
+  }
+
+  if (name) {
+    windows_event_source_name = grn_strdup_raw(name);
+  } else {
+    windows_event_source_name = NULL;
+  }
+}
+
+#ifdef WIN32
+static void
+windows_event_logger_log(grn_ctx *ctx, grn_log_level level,
+                         const char *timestamp, const char *title,
+                         const char *message, const char *location,
+                         void *user_data)
+{
+  grn_windows_event_logger_data *data = user_data;
+  WORD type;
+  WORD category = 0;
+  DWORD event_id = 0;
+  PSID user_sid = NULL;
+  WORD n_strings = 1;
+  DWORD data_size = 0;
+  void *raw_data = NULL;
+
+  switch (level) {
+  case GRN_LOG_NONE :
+    type = EVENTLOG_INFORMATION_TYPE;
+    break;
+  case GRN_LOG_EMERG :
+  case GRN_LOG_ALERT :
+  case GRN_LOG_CRIT :
+  case GRN_LOG_ERROR :
+    type = EVENTLOG_ERROR_TYPE;
+    break;
+  case GRN_LOG_WARNING :
+    type = EVENTLOG_WARNING_TYPE;
+    break;
+  case GRN_LOG_NOTICE :
+  case GRN_LOG_INFO :
+  case GRN_LOG_DEBUG :
+  case GRN_LOG_DUMP :
+    type = EVENTLOG_INFORMATION_TYPE;
+    break;
+  default :
+    type = EVENTLOG_INFORMATION_TYPE;
+    break;
+  }
+
+  {
+    const char level_marks[] = " EACewnid-";
+    grn_obj formatted_buffer;
+    UINT code_page;
+    int n_converted_chars;
+#define CONVERTED_BUFFER_SIZE 512
+
+    GRN_TEXT_INIT(&formatted_buffer, 0);
+    if (location && location[0]) {
+      grn_text_printf(ctx, &formatted_buffer, "%s|%c|%s %s %s",
+                      timestamp, level_marks[level], title, message, location);
+    } else {
+      grn_text_printf(ctx, &formatted_buffer, "%s|%c|%s %s",
+                      timestamp, level_marks[level], title, message);
+    }
+
+    switch (ctx->encoding) {
+    case GRN_ENC_EUC_JP :
+      code_page = 20932;
+      break;
+    case GRN_ENC_UTF8 :
+      code_page = CP_UTF8;
+      break;
+    case GRN_ENC_SJIS :
+      code_page = 932;
+      break;
+    case GRN_ENC_LATIN1 :
+      code_page = 1252;
+      break;
+    case GRN_ENC_KOI8R :
+      code_page = 20866;
+      break;
+    default :
+      code_page = CP_ACP;
+      break;
+    }
+
+    n_converted_chars = MultiByteToWideChar(code_page,
+                                            MB_PRECOMPOSED,
+                                            GRN_TEXT_VALUE(&formatted_buffer),
+                                            GRN_TEXT_LEN(&formatted_buffer),
+                                            NULL,
+                                            0);
+    if (n_converted_chars < CONVERTED_BUFFER_SIZE) {
+      WCHAR converted_buffer[CONVERTED_BUFFER_SIZE];
+      const WCHAR *strings[1];
+      n_converted_chars = MultiByteToWideChar(code_page,
+                                              MB_PRECOMPOSED,
+                                              GRN_TEXT_VALUE(&formatted_buffer),
+                                              GRN_TEXT_LEN(&formatted_buffer),
+                                              converted_buffer,
+                                              CONVERTED_BUFFER_SIZE);
+      converted_buffer[n_converted_chars] = L'\0';
+      strings[0] = converted_buffer;
+      ReportEventW(data->event_log,
+                   type,
+                   category,
+                   event_id,
+                   user_sid,
+                   n_strings,
+                   data_size,
+                   strings,
+                   raw_data);
+    } else {
+      WCHAR *converted;
+      const WCHAR *strings[1];
+      converted = GRN_MALLOCN(WCHAR, n_converted_chars);
+      n_converted_chars = MultiByteToWideChar(code_page,
+                                              MB_PRECOMPOSED,
+                                              GRN_TEXT_VALUE(&formatted_buffer),
+                                              GRN_TEXT_LEN(&formatted_buffer),
+                                              converted,
+                                              n_converted_chars);
+      converted[n_converted_chars] = L'\0';
+      strings[0] = converted;
+      ReportEventW(data->event_log,
+                   type,
+                   category,
+                   event_id,
+                   user_sid,
+                   n_strings,
+                   data_size,
+                   strings,
+                   raw_data);
+      GRN_FREE(converted);
+    }
+    GRN_OBJ_FIN(ctx, &formatted_buffer);
+  }
+}
+
+static void
+windows_event_logger_reopen(grn_ctx *ctx, void *user_data)
+{
+}
+
+static void
+windows_event_logger_fin(grn_ctx *ctx, void *user_data)
+{
+  grn_windows_event_logger_data *data = user_data;
+
+  if (data->event_log) {
+    DeregisterEventSource(data->event_log);
+  }
+  GRN_FREE(data);
+}
+#endif /* WIN32 */
+
+grn_rc
+grn_windows_event_logger_set(grn_ctx *ctx)
+{
+#ifdef WIN32
+  grn_rc rc;
+  grn_logger windows_event_logger;
+  grn_windows_event_logger_data *data;
+  const char *source_name;
+  GRN_API_ENTER;
+
+  data = GRN_MALLOC(sizeof(grn_windows_event_logger_data));
+  if (!data) {
+    ERR(GRN_NO_MEMORY_AVAILABLE,
+        "failed to allocate user data for Windows event logger");
+    GRN_API_RETURN(ctx->rc);
+  }
+
+  if (windows_event_source_name) {
+    source_name = windows_event_source_name;
+  } else {
+    source_name = "Groonga";
+  }
+  data->event_log = RegisterEventSourceA(NULL, source_name);
+  if (!data->event_log) {
+    GRN_FREE(data);
+    SERR("RegisterEventSource");
+    GRN_LOG(ctx, GRN_LOG_ERROR,
+            "failed to register event source: <%s>",
+            source_name);
+    GRN_API_RETURN(ctx->rc);
+  }
+
+  windows_event_logger.max_level = GRN_LOG_DEFAULT_LEVEL;
+  windows_event_logger.flags     = GRN_LOG_TIME | GRN_LOG_MESSAGE;
+  windows_event_logger.user_data = data;
+  windows_event_logger.log       = windows_event_logger_log;
+  windows_event_logger.reopen    = windows_event_logger_reopen;
+  windows_event_logger.fin       = windows_event_logger_fin;
+
+  rc = grn_logger_set(ctx, &windows_event_logger);
+  if (rc != GRN_SUCCESS) {
+    windows_event_logger.fin(ctx, windows_event_logger.user_data);
+  }
+
+  GRN_API_RETURN(rc);
+#else /* WIN32 */
+  return GRN_FUNCTION_NOT_IMPLEMENTED;
+#endif /* WIN32 */
+}
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index