From ed347428244715fe35552fbf6364f3deb93f633d Mon Sep 17 00:00:00 2001
From: derselbst <tom.mbrt@googlemail.com>
Date: Tue, 22 Dec 2020 10:54:31 +0100
Subject: [PATCH] Add log messages to fluid_fopen()

---
 src/utils/fluid_sys.c | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)

diff --git a/src/utils/fluid_sys.c b/src/utils/fluid_sys.c
index 5c559286..fb970d69 100644
--- a/src/utils/fluid_sys.c
+++ b/src/utils/fluid_sys.c
@@ -225,43 +225,46 @@ void* fluid_alloc(size_t len)
 FILE *fluid_fopen(const char *filename, const char *mode)
 {
 #if defined(WIN32)
-    wchar_t *wpath, *wmode;
-    FILE *file;
+    wchar_t *wpath = NULL, *wmode = NULL;
+    FILE *file = NULL;
     int length;
     if ((length = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, filename, -1, NULL, 0)) == 0)
     {
+        FLUID_LOG(FLUID_ERR, "Unable to perform MultiByteToWideChar() conversion for filename '%s'. Error was: '%s'", filename, fluid_get_windows_error());
         errno = EINVAL;
-        return NULL;
+        goto error_recovery;
     }
     
     wpath = FLUID_MALLOC(length * sizeof(wchar_t));
     if (wpath == NULL)
     {
+        FLUID_LOG(FLUID_PANIC, "Out of memory.");
         errno = EINVAL;
-        return NULL;
+        goto error_recovery;
     }
 
     MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, filename, -1, wpath, length);
 
     if ((length = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, mode, -1, NULL, 0)) == 0)
     {
-        FLUID_FREE(wpath);
+        FLUID_LOG(FLUID_ERR, "Unable to perform MultiByteToWideChar() conversion for mode '%s'. Error was: '%s'", mode, fluid_get_windows_error());
         errno = EINVAL;
-        return NULL;
+        goto error_recovery;
     }
 
     wmode = FLUID_MALLOC(length * sizeof(wchar_t));
     if (wmode == NULL)
     {
-        FLUID_FREE(wpath);
+        FLUID_LOG(FLUID_PANIC, "Out of memory.");
         errno = EINVAL;
-        return NULL;
+        goto error_recovery;
     }
 
     MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, mode, -1, wmode, length);
 
     file = _wfopen(wpath, wmode);
 
+error_recovery:
     FLUID_FREE(wpath);
     FLUID_FREE(wmode);