Merge pull request #3707 from lateralusX/jlorenss/win-api-family-support-libmonoutils
[mono.git] / mono / utils / mono-log-windows.c
index d89e134b7894b6d5b5fb030b1c51a8229367dff6..ab5db470e1941f75bd2e712d119f979ffa6ff602 100644 (file)
 #include <glib.h>
 #include <errno.h>
 #include <time.h>
-#include "mono-logger.h"
+#include <process.h>
+#include "mono-logger-internals.h"
+#include "mono-proclib.h"
 
 static FILE *logFile = NULL;
 static void *logUserData = NULL;
-static char *logFileName = L".//mono.log";
+static wchar_t *logFileName = L".//mono.log";
 
 /**
  * mapSyslogLevel:
@@ -39,7 +41,7 @@ mapLogFileLevel(GLogLevelFlags level)
 {
        if (level & G_LOG_LEVEL_ERROR)
                return ('E');
-       if (level & G_LOG_LEVEL_CRIT)
+       if (level & G_LOG_LEVEL_CRITICAL)
                return ('C');
        if (level & G_LOG_LEVEL_WARNING)
                return ('W');
@@ -53,10 +55,10 @@ mapLogFileLevel(GLogLevelFlags level)
 }
 
 /**
- * mono_log_open_logfile
+ * mono_log_open_syslog
  *     
- *     Open the logfile. If the path is not specified default to stdout. If the
- *     open fails issue a warning and use stdout as the log file destination.
+ *     Open the syslog file. If the open fails issue a warning and 
+ *     use stdout as the log file destination.
  *
  *     @ident - Identifier: ignored
  *     @userData - Not used
@@ -64,18 +66,19 @@ mapLogFileLevel(GLogLevelFlags level)
 void
 mono_log_open_syslog(const char *ident, void *userData)
 {
-       logFile = fopen(logFileName, "w");
+       logFile = _wfopen(logFileName, L"w");
        if (logFile == NULL) {
                g_warning("opening of log file %s failed with %s",
                          strerror(errno));
+               logFile = stdout;
        }
        logUserData = userData;
 }
 
 /**
- * mono_log_write_logfile
+ * mono_log_write_syslog
  *     
- *     Write data to the log file.
+ *     Write data to the syslog file.
  *
  *     @domain - Identifier string
  *     @level - Logging level flags
@@ -83,40 +86,33 @@ mono_log_open_syslog(const char *ident, void *userData)
  *     @vargs - Variable argument list
  */
 void
-mono_log_write_syslog(const char *domain, GLogLevelFlags level, mono_bool hdr, const char *format, va_list args)
+mono_log_write_syslog(const char *domain, GLogLevelFlags level, mono_bool hdr, const char *message)
 {
        time_t t;
-       struct tm tod;
-       char logTime[80],
-             logMessage[512];
-       pid_t pid;
-       int iLog = 0;
-       size_t nLog;
+       int pid;
+       char logTime [80];
 
        if (logFile == NULL)
-               mono_log_open_logfile(NULL, NULL);
+               logFile = stdout;
 
+       struct tm *tod;
        time(&t);
-       localtime(&t, &tod);
-       pid = _getpid();
-       strftime(logTime, sizeof(logTime), "%F %T", &tod);
-       iLog = snprintf(logMessage, sizeof(logMessage), "%s level[%c] mono[%d]: ",
-                       logTime,mapLogFileLevel(level),pid);
-       nLog = sizeof(logMessage) - iLog - 2;
-       iLog = vsnprintf(logMessage=iLog, nLog, format, args);
-       logMessage[iLog++] = '\n';
-       logMessage[iLog++] = 0;
-       fputs(logMessage, logFile);
+       tod = localtime(&t);
+       pid = mono_process_current_pid ();
+       strftime(logTime, sizeof(logTime), "%F %T", tod);
+
+       fprintf (logFile, "%s level[%c] mono[%d]: %s\n", logTime, mapLogFileLevel (level), pid, message);
+
        fflush(logFile);
 
-       if (level == G_LOG_FLAG_FATAL)
+       if (level & G_LOG_LEVEL_ERROR)
                abort();
 }
 
 /**
- * mono_log_close_logfile
+ * mono_log_close_syslog
  *
- *     Close the log file
+ *     Close the syslog file
  */
 void
 mono_log_close_syslog()