You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2021/11/05 20:22:52 UTC

[commons-daemon] 02/02: Refactor common call sites into a utility method.

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-daemon.git

commit 604ac139fa90ad83d6e14f2bac513784fe505f60
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Nov 5 16:22:48 2021 -0400

    Refactor common call sites into a utility method.
---
 src/native/windows/include/log.h |  6 ++++++
 src/native/windows/src/log.c     | 37 +++++++++++++++++++++----------------
 2 files changed, 27 insertions(+), 16 deletions(-)

diff --git a/src/native/windows/include/log.h b/src/native/windows/include/log.h
index 05fac9f..9188e1e 100644
--- a/src/native/windows/include/log.h
+++ b/src/native/windows/include/log.h
@@ -112,6 +112,12 @@ apxDisplayError(
     ...
 );
 
+DWORD
+apxGetMessage(
+    DWORD dwMessageId,
+    LPSTR lpBuffer,
+    DWORD nSize);
+
 __APXEND_DECLS
 
 #endif /* _LOG_H_INCLUDED_ */
diff --git a/src/native/windows/src/log.c b/src/native/windows/src/log.c
index 821541f..e1ef8fb 100644
--- a/src/native/windows/src/log.c
+++ b/src/native/windows/src/log.c
@@ -285,6 +285,25 @@ void apxLogLevelSetW(HANDLE  hFile,
     }
 }
 
+DWORD
+apxGetMessage(
+    DWORD dwMessageId,
+    LPSTR lpBuffer,
+    DWORD nSize)
+{
+    if (nSize == 0 || lpBuffer == 0) {
+        return 0;
+    }
+    return FormatMessageA(
+        FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
+        NULL,
+        dwMessageId,
+        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
+        lpBuffer,
+        nSize,
+        NULL);
+}
+
 int
 apxLogWrite(
     HANDLE  hFile,
@@ -339,14 +358,7 @@ apxLogWrite(
             }
         }
         else
-            FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM |
-                           FORMAT_MESSAGE_IGNORE_INSERTS,
-                           NULL,
-                           dwMessageId,
-                           MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
-                           szBp,
-                           1000,
-                           NULL);
+            apxGetMessage(dwMessageId, szBp, 1000);
     }
     else {
         va_start(args, szFormat);
@@ -458,14 +470,7 @@ apxDisplayError(
         f = "";
     sysbuf[0] = '\0';
     if (dwMessageId != ERROR_SUCCESS) {
-        len = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM |
-                             FORMAT_MESSAGE_IGNORE_INSERTS,
-                             NULL,
-                             dwMessageId,
-                             MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
-                             sysbuf,
-                             SIZ_DESLEN,
-                             NULL);
+        len = apxGetMessage(dwMessageId, sysbuf, SIZ_DESLEN);
         sysbuf[len] = '\0';
         if (len > 0) {
             if (sysbuf[len - 1] == '\n')