Merge pull request #3707 from lateralusX/jlorenss/win-api-family-support-libmonoutils
[mono.git] / mono / utils / mono-log-windows.c
1 /*
2  * mono-log-windows.c: Simplistic simulation of a syslog logger for Windows
3  *
4  * This module contains the Windows syslog logger interface
5  *
6  * Author:
7  *    Neale Ferguson <neale@sinenomine.net>
8  *
9  */
10 #include <config.h>
11
12 #ifdef HAVE_UNISTD_H
13 #include <unistd.h>
14 #endif
15
16 #ifdef HOST_WIN32
17
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <ctype.h>
21 #include <string.h>
22 #include <glib.h>
23 #include <errno.h>
24 #include <time.h>
25 #include <process.h>
26 #include "mono-logger-internals.h"
27 #include "mono-proclib.h"
28
29 static FILE *logFile = NULL;
30 static void *logUserData = NULL;
31 static wchar_t *logFileName = L".//mono.log";
32
33 /**
34  * mapSyslogLevel:
35  *      
36  *      @level - GLogLevelFlags value
37  *      @returns The equivalent character identifier
38  */
39 static inline char 
40 mapLogFileLevel(GLogLevelFlags level) 
41 {
42         if (level & G_LOG_LEVEL_ERROR)
43                 return ('E');
44         if (level & G_LOG_LEVEL_CRITICAL)
45                 return ('C');
46         if (level & G_LOG_LEVEL_WARNING)
47                 return ('W');
48         if (level & G_LOG_LEVEL_MESSAGE)
49                 return ('N');
50         if (level & G_LOG_LEVEL_INFO)
51                 return ('I');
52         if (level & G_LOG_LEVEL_DEBUG)
53                 return ('D');
54         return ('I');
55 }
56
57 /**
58  * mono_log_open_syslog
59  *      
60  *      Open the syslog file. If the open fails issue a warning and 
61  *      use stdout as the log file destination.
62  *
63  *      @ident - Identifier: ignored
64  *      @userData - Not used
65  */
66 void
67 mono_log_open_syslog(const char *ident, void *userData)
68 {
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;
74         }
75         logUserData = userData;
76 }
77
78 /**
79  * mono_log_write_syslog
80  *      
81  *      Write data to the syslog file.
82  *
83  *      @domain - Identifier string
84  *      @level - Logging level flags
85  *      @format - Printf format string
86  *      @vargs - Variable argument list
87  */
88 void
89 mono_log_write_syslog(const char *domain, GLogLevelFlags level, mono_bool hdr, const char *message)
90 {
91         time_t t;
92         int pid;
93         char logTime [80];
94
95         if (logFile == NULL)
96                 logFile = stdout;
97
98         struct tm *tod;
99         time(&t);
100         tod = localtime(&t);
101         pid = mono_process_current_pid ();
102         strftime(logTime, sizeof(logTime), "%F %T", tod);
103
104         fprintf (logFile, "%s level[%c] mono[%d]: %s\n", logTime, mapLogFileLevel (level), pid, message);
105
106         fflush(logFile);
107
108         if (level & G_LOG_LEVEL_ERROR)
109                 abort();
110 }
111
112 /**
113  * mono_log_close_syslog
114  *
115  *      Close the syslog file
116  */
117 void
118 mono_log_close_syslog()
119 {
120         if (logFile) {
121                 fclose(logFile);
122                 logFile = NULL;
123         }
124 }
125 #endif