Debian Codegraph @

common/logger.c

raw symbols SCIP
1 /*
2 * Crossfire -- cooperative multi-player graphical RPG and adventure game
3 *
4 * Copyright (c) 1999-2014 Mark Wedel and the Crossfire Development Team
5 * Copyright (c) 1992 Frank Tore Johansen
6 *
7 * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
8 * welcome to redistribute it under certain conditions. For details, please
9 * see COPYING and LICENSE.
10 *
11 * The authors can be reached via e-mail at <[email protected]>.
12 */
13
14 /**
15 * @file logger.c
16 * This handles logging, to file or strerr/stdout.
17 */
18
19 #include "global.h"
20
21 #include <stdarg.h>
22 #include <stdlib.h>
23
24 #include "sproto.h"
25
26 int reopen_logfile = 0; /* May be set in SIGHUP handler */
27
28 /**
29 * Human-readable name of log levels.
30 */
31 static const char *const loglevel_names[] = {
32 "[EE] ",
33 "[II] ",
34 "[DD] ",
35 "[MM] ",
36 };
37
38 /**
39 * Logs a message to stderr, or to file.
40 * Or discards the message if it is of no importance, and none have
41 * asked to hear messages of that logLevel.
42 *
43 * See include/logger.h for possible logLevels. Messages with llevInfo
44 * and llevError are always printed, regardless of debug mode.
45 *
46 * @param logLevel
47 * level of the message
48 * @param format
49 * message to log. Works like printf() and such
50 */
51 void LOG(LogLevel logLevel, const char *format, ...) {
52 char buf[20480]; /* This needs to be really really big - larger
53 * than any other buffer, since that buffer may
54 * need to be put in this one.
55 */
56
57 char time_buf[2048];
58
59 va_list ap;
60 va_start(ap, format);
61
62 buf[0] = '\0';
63 if (logLevel <= settings.debug) {
64 time_buf[0] = '\0';
65 if (settings.log_timestamp == TRUE) {
66 struct tm *time_tmp;
67 time_t now = time((time_t *)NULL);
68
69 time_tmp = localtime(&now);
70 if (time_tmp != NULL) {
71 if (strftime(time_buf, sizeof(time_buf), settings.log_timestamp_format, time_tmp) == 0) {
72 time_buf[0] = '\0';
73 }
74 }
75 }
76
77 vsnprintf(buf, sizeof(buf), format, ap);
78 #ifdef WIN32 /* ---win32 change log handling for win32 */
79 if (time_buf[0] != 0) {
80 fputs(time_buf, logfile);
81 fputs(" ", logfile);
82 }
83 fputs(loglevel_names[logLevel], logfile); /* wrote to file or stdout */
84 fputs(buf, logfile); /* wrote to file or stdout */
85 #ifdef DEBUG /* if we have a debug version, we want see ALL output */
86 fflush(logfile); /* so flush this! */
87 #endif
88 if (logfile != stderr) { /* if was it a logfile wrote it to screen too */
89 if (time_buf[0] != 0) {
90 fputs(time_buf, stderr);
91 fputs(" ", stderr);
92 }
93 fputs(loglevel_names[logLevel], stderr);
94 fputs(buf, stderr);
95 }
96 #else /* not WIN32 */
97 if (reopen_logfile) {
98 reopen_logfile = 0;
99 if (fclose(logfile) != 0) {
100 /* stderr has been closed if -detach was used, but it's better
101 * to try to report about this anyway. */
102 perror("tried to close log file after SIGHUP in logger.c:LOG()");
103 }
104 if ((logfile = fopen(settings.logfilename, "a")) == NULL) {
105 /* There's likely to be something very wrong with the OS anyway
106 * if reopening fails. */
107 perror("tried to open log file after SIGHUP in logger.c:LOG()");
108 emergency_save(0);
109 clean_tmp_files();
110 exit(1);
111 }
112 setvbuf(logfile, NULL, _IOLBF, 0);
113 LOG(llevInfo, "logfile reopened\n");
114 }
115
116 if (time_buf[0] != 0) {
117 fputs(time_buf, logfile);
118 fputs(" ", logfile);
119 }
120 fputs(loglevel_names[logLevel], logfile);
121 fputs(buf, logfile);
122 #endif
123 }
124 if (!exiting
125 && !trying_emergency_save
126 && logLevel == llevError
127 && ++nroferrors > MAX_ERRORS) {
128 exiting = 1;
129 if (!trying_emergency_save)
130 emergency_save(0);
131 }
132 va_end(ap);
133 }