Update Haiku support (#15674)
[mono-project.git] / mono / utils / mono-log-common.c
blob467b3a66652d6e8a0309da3e943c4faa52eed143
1 /**
2 * \file
3 * Platform-independent interface to the logger
5 * This module contains the POSIX 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 #include <stdlib.h>
18 #include <stdio.h>
19 #include <ctype.h>
20 #include <string.h>
21 #include <glib.h>
22 #include <errno.h>
23 #include <time.h>
24 #ifndef HOST_WIN32
25 #include <sys/time.h>
26 #else
27 #include <process.h>
28 #endif
29 #include "mono-logger-internals.h"
30 #include "mono-proclib.h"
31 #include "mono-time.h"
33 static FILE *logFile = NULL;
34 static void *logUserData = NULL;
36 /**
37 * mapSyslogLevel:
39 * @level - GLogLevelFlags value
40 * @returns The equivalent character identifier
42 static inline char
43 mapLogFileLevel(GLogLevelFlags level)
45 if (level & G_LOG_LEVEL_ERROR)
46 return ('E');
47 if (level & G_LOG_LEVEL_CRITICAL)
48 return ('C');
49 if (level & G_LOG_LEVEL_WARNING)
50 return ('W');
51 if (level & G_LOG_LEVEL_MESSAGE)
52 return ('N');
53 if (level & G_LOG_LEVEL_INFO)
54 return ('I');
55 if (level & G_LOG_LEVEL_DEBUG)
56 return ('D');
57 return ('I');
60 /**
61 * mono_log_open_logfile:
62 * \param path Path for log file
63 * \param userData Not used
64 * Open the logfile. If the path is not specified default to stdout. If the
65 * open fails issue a warning and use stdout as the log file destination.
67 void
68 mono_log_open_logfile(const char *path, void *userData)
70 if (path == NULL) {
71 logFile = stdout;
72 } else {
73 #ifndef HOST_WIN32
74 logFile = fopen(path, "w");
75 #else
76 gunichar2 *wPath = g_utf8_to_utf16(path, -1, 0, 0, 0);
77 if (wPath != NULL) {
78 logFile = _wfopen((wchar_t *) wPath, L"w");
79 g_free (wPath);
81 #endif
82 if (logFile == NULL) {
83 g_warning("opening of log file %s failed with %s - defaulting to stdout",
84 path, strerror(errno));
85 logFile = stdout;
88 logUserData = userData;
91 /**
92 * mono_log_write_logfile:
93 * \param domain Identifier string
94 * \param level Logging level flags
95 * \param format \c printf format string
96 * \param vargs Variable argument list
97 * Write data to the log file.
99 void
100 mono_log_write_logfile (const char *log_domain, GLogLevelFlags level, mono_bool hdr, const char *message)
102 time_t t;
104 if (logFile == NULL)
105 logFile = stdout;
107 if (hdr) {
108 pid_t pid;
109 char logTime [80];
111 #ifdef HAVE_LOCALTIME_R
112 struct tm tod;
113 time(&t);
114 localtime_r(&t, &tod);
115 strftime(logTime, sizeof(logTime), MONO_STRFTIME_F " " MONO_STRFTIME_T, &tod);
116 #else
117 struct tm *tod;
118 time(&t);
119 tod = localtime(&t);
120 strftime(logTime, sizeof(logTime), MONO_STRFTIME_F " " MONO_STRFTIME_T, tod);
121 #endif
123 pid = mono_process_current_pid ();
125 fprintf (logFile, "%s level[%c] mono[%d]: %s\n", logTime, mapLogFileLevel (level), pid, message);
126 } else {
127 fprintf (logFile, "%s%s%s\n",
128 log_domain != NULL ? log_domain : "",
129 log_domain != NULL ? ": " : "",
130 message);
133 fflush(logFile);
135 if (level & G_LOG_LEVEL_ERROR)
136 g_assert_abort ();
140 * mono_log_close_logfile:
141 * Close the log file
143 void
144 mono_log_close_logfile()
146 if (logFile) {
147 if (logFile != stdout)
148 fclose(logFile);
149 logFile = NULL;