Merge pull request #3528 from BrzVlad/fix-sgen-check-before-collections
[mono.git] / mono / utils / mono-log-common.c
index 10f1f2f70ea8e4d38b800934972dcb2b47cd8859..cf73d9acbb84cbcafc5328dc5fb6b3b6f9975e50 100644 (file)
 #include <glib.h>
 #include <errno.h>
 #include <time.h>
+#ifndef HOST_WIN32
 #include <sys/time.h>
-#include "mono-logger.h"
+#else
+#include <process.h>
+#endif
+#include "mono-logger-internals.h"
 
 static FILE *logFile = NULL;
 static void *logUserData = NULL;
@@ -32,7 +36,7 @@ static void *logUserData = NULL;
  *     @level - GLogLevelFlags value
  *     @returns The equivalent character identifier
  */
-static __inline__ char 
+static inline char 
 mapLogFileLevel(GLogLevelFlags level) 
 {
        if (level & G_LOG_LEVEL_ERROR)
@@ -65,10 +69,19 @@ mono_log_open_logfile(const char *path, void *userData)
        if (path == NULL) {
                logFile = stdout;
        } else {
+#ifndef HOST_WIN32
                logFile = fopen(path, "w");
+#else
+               gunichar2 *wPath = g_utf8_to_utf16(path, -1, 0, 0, 0);
+               if (wPath != NULL) {
+                       logFile = _wfopen((wchar_t *) wPath, L"w");
+                       g_free (wPath);
+               }
+#endif
                if (logFile == NULL) {
                        g_warning("opening of log file %s failed with %s - defaulting to stdout", 
                                  path, strerror(errno));
+                       logFile = stdout;
                }
        }
        logUserData = userData;
@@ -85,23 +98,38 @@ mono_log_open_logfile(const char *path, void *userData)
  *     @vargs - Variable argument list
  */
 void
-mono_log_write_logfile(const char *domain, GLogLevelFlags level, const char *format, va_list args)
+mono_log_write_logfile (const char *log_domain, GLogLevelFlags level, mono_bool hdr, const char *message)
 {
        time_t t;
-       struct tm tod;
-       char logTime[80];
-       pid_t pid;
 
        if (logFile == NULL)
                logFile = stdout;
 
-       time(&t);
-       localtime_r(&t, &tod);
-       pid = getpid();
-       strftime(logTime, sizeof(logTime), "%F %T", &tod);
-       fprintf(logFile, "%s level[%c] mono[%d]: ",logTime,mapLogFileLevel(level),pid);
-       vfprintf(logFile, format, args);
-       fputc('\n', logFile);
+       if (hdr) {
+               pid_t pid;
+               char logTime [80];
+
+#ifndef HOST_WIN32
+               struct tm tod;
+               time(&t);
+               localtime_r(&t, &tod);
+               pid = getpid();
+               strftime(logTime, sizeof(logTime), "%Y-%m-%d %H:%M:%S", &tod);
+#else
+               struct tm *tod;
+               time(&t);
+               tod = localtime(&t);
+               pid = _getpid();
+               strftime(logTime, sizeof(logTime), "%F %T", tod);
+#endif
+               fprintf (logFile, "%s level[%c] mono[%d]: %s\n", logTime, mapLogFileLevel (level), pid, message);
+       } else {
+               fprintf (logFile, "%s%s%s\n",
+                       log_domain != NULL ? log_domain : "",
+                       log_domain != NULL ? ": " : "",
+                       message);
+       }
+
        fflush(logFile);
 
        if (level == G_LOG_FLAG_FATAL)