Correct conditional compilation & move logger source files to correct place
[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 <sys/time.h>
26 #include "mono-logger.h"
27
28 static FILE *logFile = NULL;
29 static void *logUserData = NULL;
30 static char *logFileName = L".//mono.log";
31
32 /**
33  * mapSyslogLevel:
34  *      
35  *      @level - GLogLevelFlags value
36  *      @returns The equivalent character identifier
37  */
38 static __inline__ char 
39 mapLogFileLevel(GLogLevelFlags level) 
40 {
41         if (level & G_LOG_LEVEL_ERROR)
42                 return ('E');
43         if (level & G_LOG_LEVEL_CRIT)
44                 return ('C');
45         if (level & G_LOG_LEVEL_WARNING)
46                 return ('W');
47         if (level & G_LOG_LEVEL_MESSAGE)
48                 return ('N');
49         if (level & G_LOG_LEVEL_INFO)
50                 return ('I');
51         if (level & G_LOG_LEVEL_DEBUG)
52                 return ('D');
53         return ('I');
54 }
55
56 /**
57  * mono_log_open_logfile
58  *      
59  *      Open the logfile. If the path is not specified default to stdout. If the
60  *      open fails issue a warning and use stdout as the log file destination.
61  *
62  *      @ident - Identifier: ignored
63  *      @userData - Not used
64  */
65 void
66 mono_log_open_syslog(const char *ident, void *userData)
67 {
68         logFile = fopen(logFileName, "w");
69         if (logFile == NULL) {
70                 g_warning("opening of log file %s failed with %s",
71                           strerror(errno));
72         }
73         logUserData = userData;
74 }
75
76 /**
77  * mono_log_write_logfile
78  *      
79  *      Write data to the log file.
80  *
81  *      @domain - Identifier string
82  *      @level - Logging level flags
83  *      @format - Printf format string
84  *      @vargs - Variable argument list
85  */
86 void
87 mono_log_write_syslog(const char *domain, GLogLevelFlags level, mono_bool hdr, const char *format, va_list args)
88 {
89         time_t t;
90         struct tm tod;
91         char logTime[80],
92               logMessage[512];
93         pid_t pid;
94         int iLog = 0;
95         size_t nLog;
96
97         if (logFile == NULL)
98                 mono_log_open_logfile(NULL, NULL);
99
100         time(&t);
101         localtime_r(&t, &tod);
102         pid = getpid();
103         strftime(logTime, sizeof(logTime), "%F %T", &tod);
104         iLog = snprintf(logMessage, sizeof(logMessage), "%s level[%c] mono[%d]: ",
105                         logTime,mapLogFileLevel(level),pid);
106         nLog = sizeof(logMessage) - iLog - 2;
107         iLog = vsnprintf(logMessage=iLog, nLog, format, args);
108         logMessage[iLog++] = '\n';
109         logMessage[iLog++] = 0;
110         fputs(logMessage, logFile);
111         fflush(logFile);
112
113         if (level == G_LOG_FLAG_FATAL)
114                 abort();
115 }
116
117 /**
118  * mono_log_close_logfile
119  *
120  *      Close the log file
121  */
122 void
123 mono_log_close_syslog()
124 {
125         if (logFile) {
126                 fclose(logFile);
127                 logFile = NULL;
128         }
129 }
130 #endif