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