[Groonga-commit] groonga/groonga at ac965f0 [master] windows: use Groonga's default encoding for log message

Back to archive index

Kouhei Sutou null+****@clear*****
Sun Jul 26 18:49:17 JST 2015


Kouhei Sutou	2015-07-26 18:49:17 +0900 (Sun, 26 Jul 2015)

  New Revision: ac965f0387ab8496a1354a773a3c3de03eb3c246
  https://github.com/groonga/groonga/commit/ac965f0387ab8496a1354a773a3c3de03eb3c246

  Message:
    windows: use Groonga's default encoding for log message

  Modified files:
    lib/error.c
    lib/grn_util.h
    lib/util.c
    lib/windows_event_logger.c

  Modified: lib/error.c (+35 -17)
===================================================================
--- lib/error.c    2015-07-26 18:25:51 +0900 (d3ae7ac)
+++ lib/error.c    2015-07-26 18:49:17 +0900 (095331d)
@@ -16,6 +16,7 @@
 */
 
 #include "grn_error.h"
+#include "grn_util.h"
 
 #ifdef HAVE_ERRNO_H
 #include <errno.h>
@@ -129,28 +130,45 @@ grn_current_error_message(void)
 {
 # define ERROR_MESSAGE_BUFFER_SIZE 4096
   int error_code = GetLastError();
+  static WCHAR utf16_message[ERROR_MESSAGE_BUFFER_SIZE];
+  DWORD written_utf16_chars;
   static char message[ERROR_MESSAGE_BUFFER_SIZE];
-  DWORD written_bytes;
-
-  written_bytes = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
-                                FORMAT_MESSAGE_IGNORE_INSERTS,
-                                NULL,
-                                error_code,
-                                LANG_ID_USER_DEFAULT(),
-                                message,
-                                ERROR_MESSAGE_BUFFER_SIZE,
-                                NULL);
-  if (written_bytes >= 2) {
-    if (message[written_bytes - 1] == '\n') {
-      message[written_bytes - 1] = '\0';
-      written_bytes--;
+
+  written_utf16_chars = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM |
+                                       FORMAT_MESSAGE_IGNORE_INSERTS,
+                                       NULL,
+                                       error_code,
+                                       LANG_ID_USER_DEFAULT(),
+                                       utf16_message,
+                                       ERROR_MESSAGE_BUFFER_SIZE,
+                                       NULL);
+  if (written_utf16_chars >= 2) {
+    if (utf16_message[written_utf16_chars - 1] == L'\n') {
+      utf16_message[written_utf16_chars - 1] = L'\0';
+      written_utf16_chars--;
     }
-    if (message[written_bytes - 1] == '\r') {
-      message[written_bytes - 1] = '\0';
-      written_bytes--;
+    if (utf16_message[written_utf16_chars - 1] == L'\r') {
+      utf16_message[written_utf16_chars - 1] = L'\0';
+      written_utf16_chars--;
     }
   }
 
+  {
+    UINT code_page;
+    DWORD convert_flags = 0;
+    int written_bytes;
+
+    code_page = grn_windows_encoding_to_code_page(grn_get_default_encoding());
+    written_bytes = WideCharToMultiByte(code_page,
+                                        convert_flags,
+                                        utf16_message,
+                                        written_utf16_chars,
+                                        message,
+                                        ERROR_MESSAGE_BUFFER_SIZE,
+                                        NULL,
+                                        NULL);
+  }
+
   return message;
 
 # undef ERROR_MESSAGE_BUFFER_SIZE

  Modified: lib/grn_util.h (+4 -0)
===================================================================
--- lib/grn_util.h    2015-07-26 18:25:51 +0900 (f94140d)
+++ lib/grn_util.h    2015-07-26 18:49:17 +0900 (258a429)
@@ -31,6 +31,10 @@ GRN_API char *grn_path_separator_to_system(char *dest, char *groonga_path);
 
 int grn_mkstemp(char *path_template);
 
+#ifdef WIN32
+GRN_API UINT grn_windows_encoding_to_code_page(grn_encoding encoding);
+#endif /* WIN32 */
+
 #ifdef __cplusplus
 }
 #endif

  Modified: lib/util.c (+31 -0)
===================================================================
--- lib/util.c    2015-07-26 18:25:51 +0900 (17172d6)
+++ lib/util.c    2015-07-26 18:49:17 +0900 (1008fa9)
@@ -1385,3 +1385,34 @@ grn_mkstemp(char *path_template)
 # endif /* HAVE_MKSTEMP */
 }
 #endif /* WIN32 */
+
+#ifdef WIN32
+UINT
+grn_windows_encoding_to_code_page(grn_encoding encoding)
+{
+  UINT code_page;
+
+  switch (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;
+  }
+
+  return code_page;
+}
+#endif /* WIN32 */

  Modified: lib/windows_event_logger.c (+2 -20)
===================================================================
--- lib/windows_event_logger.c    2015-07-26 18:25:51 +0900 (d4dc3d8)
+++ lib/windows_event_logger.c    2015-07-26 18:49:17 +0900 (f80fc1b)
@@ -18,6 +18,7 @@
 
 #include "grn_logger.h"
 #include "grn_ctx.h"
+#include "grn_util.h"
 
 #include <string.h>
 
@@ -106,26 +107,7 @@ windows_event_logger_log(grn_ctx *ctx, grn_log_level level,
                       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;
-    }
+    code_page = grn_windows_encoding_to_code_page(ctx->encoding);
 
     n_converted_chars = MultiByteToWideChar(code_page,
                                             convert_flags,
-------------- next part --------------
HTML����������������������������...
Download 



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