[vlc-commits] [Git][videolan/vlc][master] 3 commits: messages: simplify Win32DebugOutputMsg()

Steve Lhomme (@robUx4) gitlab at videolan.org
Wed Jul 20 05:53:32 UTC 2022



Steve Lhomme pushed to branch master at VideoLAN / VLC


Commits:
e9464a1e by Steve Lhomme at 2022-07-20T05:41:15+00:00
messages: simplify Win32DebugOutputMsg()

No need to call vsnprintf() twice and add the ending \n in an extra part of the
temporary buffer. We have all that we need with a single vasprintf() call.

No need to make a va_copy() inside Win32DebugOutputMsg(), there is one on the
caller side and now we only use the va_list once.

- - - - -
572c454f by Steve Lhomme at 2022-07-20T05:41:15+00:00
messages: move Win32DebugOutputMsg to avoid forward declaration

No functional changes.

- - - - -
8e2c76e5 by Steve Lhomme at 2022-07-20T05:41:15+00:00
messages: only send messages to the Windows debugger if they will be logged

If logger is NULL then the intention was to not log it.

In vlc_vaLogCallback() we receive all the logs aimed at an internal logger.

No need to make a va_copy() when calling Win32DebugOutputMsg(), it does one
internally.

- - - - -


1 changed file:

- src/misc/messages.c


Changes:

=====================================
src/misc/messages.c
=====================================
@@ -43,6 +43,35 @@
 #include "rcu.h"
 #include "../libvlc.h"
 
+#ifdef _WIN32
+static const char msg_type[4][9] = { "", " error", " warning", " debug" };
+
+static void Win32DebugOutputMsg (int type, const vlc_log_t *p_item,
+                                 const char *format, va_list dol)
+{
+    char *msg = NULL;
+    int msg_len = vasprintf(&msg, format, dol);
+
+    if (unlikely(msg_len == -1))
+        return;
+
+    const char *ending = msg_len > 1 && msg[msg_len-1] == '\n' ? "" : "\n";
+
+    char* psz_msg = NULL;
+    if (asprintf(&psz_msg, "%s %s%s: %s%s", p_item->psz_module,
+                p_item->psz_object_type, msg_type[type], msg, ending) > 0) {
+        wchar_t* wmsg = ToWide(psz_msg);
+        if (likely(wmsg != NULL))
+        {
+            OutputDebugStringW(wmsg);
+            free(wmsg);
+        }
+        free(psz_msg);
+    }
+    free(msg);
+}
+#endif
+
 static void vlc_LogSpam(vlc_object_t *obj)
 {
     /* Announce who we are */
@@ -61,6 +90,14 @@ static void vlc_vaLogCallback(vlc_logger_t *logger, int type,
                               va_list ap)
 {
     if (logger != NULL) {
+#ifdef _WIN32
+        va_list dol;
+
+        va_copy (dol, ap);
+        Win32DebugOutputMsg (type, item, format, dol);
+        va_end (dol);
+#endif
+
         int canc = vlc_savecancel();
 
         logger->ops->log(logger, type, item, format, ap);
@@ -78,11 +115,6 @@ static void vlc_LogCallback(vlc_logger_t *logger, int type,
     va_end(ap);
 }
 
-#ifdef _WIN32
-static void Win32DebugOutputMsg (int , const vlc_log_t *,
-                                 const char *, va_list);
-#endif
-
 void vlc_vaLog(struct vlc_logger *const *loggerp, int type,
                const char *typename, const char *module,
                const char *file, unsigned line, const char *func,
@@ -116,14 +148,6 @@ void vlc_vaLog(struct vlc_logger *const *loggerp, int type,
     msg.func = func;
     msg.tid = vlc_thread_id();
 
-#ifdef _WIN32
-    va_list ap;
-
-    va_copy (ap, args);
-    Win32DebugOutputMsg (type, &msg, format, ap);
-    va_end (ap);
-#endif
-
     /* Pass message to the callback */
     if (logger != NULL)
         vlc_vaLogCallback(logger, type, &msg, format, args);
@@ -141,48 +165,6 @@ void vlc_Log(struct vlc_logger *const *logger, int type,
     va_end(ap);
 }
 
-#ifdef _WIN32
-static const char msg_type[4][9] = { "", " error", " warning", " debug" };
-
-static void Win32DebugOutputMsg (int type, const vlc_log_t *p_item,
-                                 const char *format, va_list dol)
-{
-    VLC_UNUSED(p_item);
-
-    va_list dol2;
-    va_copy (dol2, dol);
-    int msg_len = vsnprintf(NULL, 0, format, dol2);
-    va_end (dol2);
-
-    if (msg_len <= 0)
-        return;
-
-    char *msg = malloc(msg_len + 1 + 1);
-    if (!msg)
-        return;
-
-    msg_len = vsnprintf(msg, msg_len+1, format, dol);
-    if (msg_len > 0){
-        if (msg[msg_len-1] != '\n') {
-            msg[msg_len] = '\n';
-            msg[msg_len + 1] = '\0';
-        }
-        char* psz_msg = NULL;
-        if (asprintf(&psz_msg, "%s %s%s: %s", p_item->psz_module,
-                    p_item->psz_object_type, msg_type[type], msg) > 0) {
-            wchar_t* wmsg = ToWide(psz_msg);
-            if (likely(wmsg != NULL))
-            {
-                OutputDebugStringW(wmsg);
-                free(wmsg);
-            }
-            free(psz_msg);
-        }
-    }
-    free(msg);
-}
-#endif
-
 /**
  * Early (latched) message log.
  *



View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/53c0bfa28f720499f1f05936b235ba64a5ef54ea...8e2c76e5f523106375ca8612ade8a51bb7a6c926

-- 
View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/53c0bfa28f720499f1f05936b235ba64a5ef54ea...8e2c76e5f523106375ca8612ade8a51bb7a6c926
You're receiving this email because of your account on code.videolan.org.


VideoLAN code repository instance


More information about the vlc-commits mailing list