mono_native_tls_get_value that does not change LastError. (#15568)
[mono-project.git] / mono / utils / mono-log-windows.c
blob5d0f0c12f2b2dc208746970e4e90830b4a8b9175
1 /**
2 * \file
3 * Simplistic simulation of a syslog logger for Windows
5 * This module contains the Windows syslog logger interface
7 * Author:
8 * Neale Ferguson <neale@sinenomine.net>
11 #include <config.h>
13 #ifdef HAVE_UNISTD_H
14 #include <unistd.h>
15 #endif
17 #ifdef HOST_WIN32
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <ctype.h>
22 #include <string.h>
23 #include <glib.h>
24 #include <errno.h>
25 #include <time.h>
26 #include <process.h>
27 #include "mono-logger-internals.h"
28 #include "mono-proclib.h"
29 #include "mono-time.h"
31 static FILE *logFile = NULL;
32 static void *logUserData = NULL;
33 static const wchar_t *logFileName = L".//mono.log"; // FIXME double slash
35 /**
36 * mapSyslogLevel:
38 * @level - GLogLevelFlags value
39 * @returns The equivalent character identifier
41 static inline char
42 mapLogFileLevel(GLogLevelFlags level)
44 if (level & G_LOG_LEVEL_ERROR)
45 return ('E');
46 if (level & G_LOG_LEVEL_CRITICAL)
47 return ('C');
48 if (level & G_LOG_LEVEL_WARNING)
49 return ('W');
50 if (level & G_LOG_LEVEL_MESSAGE)
51 return ('N');
52 if (level & G_LOG_LEVEL_INFO)
53 return ('I');
54 if (level & G_LOG_LEVEL_DEBUG)
55 return ('D');
56 return ('I');
59 /**
60 * mono_log_open_syslog:
61 * \param ident Identifier: ignored
62 * \param userData Not used
63 * Open the syslog file. If the open fails issue a warning and
64 * use stdout as the log file destination.
66 void
67 mono_log_open_syslog(const char *ident, void *userData)
69 logFile = _wfopen(logFileName, L"w");
70 if (logFile == NULL) {
71 g_warning("opening of log file %s failed with %s",
72 strerror(errno));
73 logFile = stdout;
75 logUserData = userData;
78 /**
79 * mono_log_write_syslog
80 * \param domain Identifier string
81 * \param level Logging level flags
82 * \param format \c printf format string
83 * \param vargs Variable argument list
84 * Write data to the syslog file.
86 void
87 mono_log_write_syslog(const char *domain, GLogLevelFlags level, mono_bool hdr, const char *message)
89 time_t t;
90 int pid;
91 char logTime [80];
93 if (logFile == NULL)
94 logFile = stdout;
96 struct tm *tod;
97 time(&t);
98 tod = localtime(&t);
99 pid = mono_process_current_pid ();
100 strftime(logTime, sizeof(logTime), MONO_STRFTIME_F " " MONO_STRFTIME_T, tod);
102 fprintf (logFile, "%s level[%c] mono[%d]: %s\n", logTime, mapLogFileLevel (level), pid, message);
104 fflush(logFile);
106 if (level & G_LOG_LEVEL_ERROR)
107 g_assert_abort ();
111 * mono_log_close_syslog
113 * Close the syslog file
115 void
116 mono_log_close_syslog()
118 if (logFile) {
119 fclose(logFile);
120 logFile = NULL;
123 #endif