少年修仙传客户端基础资源
hch
2024-04-01 d01413b00ef631ac20347716b23818b0b811f65f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/**
 * \file
 * Platform-independent interface to the logger
 *
 * This module contains the POSIX syslog logger interface
 *
 * Author:
 *    Neale Ferguson <neale@sinenomine.net>
 *
 */
#include <config.h>
 
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
 
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <glib.h>
#include <errno.h>
#include <time.h>
#ifndef HOST_WIN32
#include <sys/time.h>
#else
#include <process.h>
#endif
#include "mono-logger-internals.h"
#include "mono-proclib.h"
 
static FILE *logFile = NULL;
static void *logUserData = NULL;
 
/**
 * mapSyslogLevel:
 *     
 *     @level - GLogLevelFlags value
 *     @returns The equivalent character identifier
 */
static inline char 
mapLogFileLevel(GLogLevelFlags level) 
{
    if (level & G_LOG_LEVEL_ERROR)
        return ('E');
    if (level & G_LOG_LEVEL_CRITICAL)
        return ('C');
    if (level & G_LOG_LEVEL_WARNING)
        return ('W');
    if (level & G_LOG_LEVEL_MESSAGE)
        return ('N');
    if (level & G_LOG_LEVEL_INFO)
        return ('I');
    if (level & G_LOG_LEVEL_DEBUG)
        return ('D');
    return ('I');
}
 
/**
 * mono_log_open_logfile:
 * \param path Path for log file
 * \param userData Not used
 * Open the logfile. If the path is not specified default to stdout. If the
 * open fails issue a warning and use stdout as the log file destination.
 */
void
mono_log_open_logfile(const char *path, void *userData)
{
    if (path == NULL) {
        logFile = stdout;
    } else {
#ifndef HOST_WIN32
        logFile = fopen(path, "w");
#else
        gunichar2 *wPath = g_utf8_to_utf16(path, -1, 0, 0, 0);
        if (wPath != NULL) {
            logFile = _wfopen((wchar_t *) wPath, L"w");
            g_free (wPath);
        }
#endif
        if (logFile == NULL) {
            g_warning("opening of log file %s failed with %s - defaulting to stdout", 
                  path, strerror(errno));
            logFile = stdout;
        }
    }
    logUserData = userData;
}
 
/**
 * mono_log_write_logfile:
 * \param domain Identifier string
 * \param level Logging level flags
 * \param format \c printf format string
 * \param vargs Variable argument list
 * Write data to the log file.
 */
void
mono_log_write_logfile (const char *log_domain, GLogLevelFlags level, mono_bool hdr, const char *message)
{
    time_t t;
 
    if (logFile == NULL)
        logFile = stdout;
 
    if (hdr) {
        pid_t pid;
        char logTime [80];
 
#ifdef HAVE_LOCALTIME_R
        struct tm tod;
        time(&t);
        localtime_r(&t, &tod);
        strftime(logTime, sizeof(logTime), "%Y-%m-%d %H:%M:%S", &tod);
#else
        struct tm *tod;
        time(&t);
        tod = localtime(&t);
        strftime(logTime, sizeof(logTime), "%F %T", tod);
#endif
 
        pid = mono_process_current_pid ();
 
        fprintf (logFile, "%s level[%c] mono[%d]: %s\n", logTime, mapLogFileLevel (level), pid, message);
    } else {
        fprintf (logFile, "%s%s%s\n",
            log_domain != NULL ? log_domain : "",
            log_domain != NULL ? ": " : "",
            message);
    }
 
    fflush(logFile);
 
    if (level & G_LOG_LEVEL_ERROR)
        abort();
}
 
/**
 * mono_log_close_logfile:
 * Close the log file
 */
void
mono_log_close_logfile()
{
    if (logFile) {
        if (logFile != stdout)
            fclose(logFile);
        logFile = NULL;
    }
}