GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/logger.h Lines: 0 2 0.0 %
Date: 2017-11-29 Branches: 0 0 0.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2004-2009  The Mana World Development Team
4
 *  Copyright (C) 2009-2010  The Mana Developers
5
 *  Copyright (C) 2011-2017  The ManaPlus Developers
6
 *
7
 *  This file is part of The ManaPlus Client.
8
 *
9
 *  This program is free software; you can redistribute it and/or modify
10
 *  it under the terms of the GNU General Public License as published by
11
 *  the Free Software Foundation; either version 2 of the License, or
12
 *  any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
#ifndef LOGGER_H
24
#define LOGGER_H
25
26
#include "localconsts.h"
27
28
#include "utils/vector.h"
29
30
PRAGMA48(GCC diagnostic push)
31
PRAGMA48(GCC diagnostic ignored "-Wshadow")
32
#include <SDL_mutex.h>
33
PRAGMA48(GCC diagnostic pop)
34
35
#include <fstream>
36
37
#ifdef ENABLEDEBUGLOG
38
#define DEBUGLOG(str) \
39
    if (logger && !mIgnore) \
40
        logger->dlog(str)
41
#define DEBUGLOG2(str, pos, comment) \
42
    if (logger && !mIgnore) \
43
        logger->dlog2(str, pos, comment)
44
#define DEBUGLOGSTR(str) \
45
    if (logger) \
46
        logger->dlog(str)
47
#define IGNOREDEBUGLOG mIgnore = Net::isIgnorePacket(mId)
48
#else  // ENABLEDEBUGLOG
49
#define DEBUGLOG(str) {}
50
#define DEBUGLOG2(str, comment) {}
51
#define DEBUGLOGSTR(str)
52
#define IGNOREDEBUGLOG {}
53
#endif  // ENABLEDEBUGLOG
54
55
#define UNIMPLEMENTEDPACKET \
56
    logger->unimplemented(msg.getId())
57
58
#define UNIMPLEMENTEDPACKETFIELD(field) \
59
    logger->unimplemented(msg.getId(), field)
60
61
#define WRONGPACKETSIZE \
62
    logger->unimplemented(CAST_U32(mId), mLength, mPos)
63
64
/**
65
 * The Log Class : Useful to write debug or info messages
66
 */
67
class Logger final
68
{
69
    public:
70
        /**
71
         * Constructor.
72
         */
73
        Logger();
74
75
        A_DELETE_COPY(Logger)
76
77
        /**
78
         * Destructor, closes log file.
79
         */
80
        ~Logger();
81
82
        /**
83
         * Sets the file to log to and opens it
84
         */
85
        void setLogFile(const std::string &logFilename);
86
87
        /**
88
         * Sets whether the log should be written to standard output.
89
         */
90
        void setLogToStandardOut(const bool value)
91
        { mLogToStandardOut = value; }
92
93
        /**
94
         * Enters a message in the log. The message will be timestamped.
95
         */
96
        void log(const char *const log_text, ...) A_NONNULL(2)
97
#ifdef __GNUC__
98
#ifdef __OpenBSD__
99
100
            __attribute__((__format__(printf, 2, 3)))
101
#else  // __OpenBSD__
102
103
#ifdef BAD_CILKPLUS
104
            __attribute__((__format__(gnu_printf, 1, 2)))
105
#else  // BAD_CILKPLUS
106
107
            __attribute__((__format__(gnu_printf, 2, 3)))
108
#endif  // BAD_CILKPLUS
109
110
#endif  // __OpenBSD__
111
#endif  // __GNUC__
112
            ;
113
114
        /**
115
         * Enters a message in the log. The message will be timestamped.
116
         */
117
        void assertLog(const char *const log_text, ...) A_NONNULL(2)
118
#ifdef __GNUC__
119
#ifdef __OpenBSD__
120
121
            __attribute__((__format__(printf, 2, 3)))
122
#else  // __OpenBSD__
123
124
#ifdef BAD_CILKPLUS
125
            __attribute__((__format__(gnu_printf, 1, 2)))
126
#else  // BAD_CILKPLUS
127
128
            __attribute__((__format__(gnu_printf, 2, 3)))
129
#endif  // BAD_CILKPLUS
130
131
#endif  // __OpenBSD__
132
#endif  // __GNUC__
133
            ;
134
135
        /**
136
         * Enters a message in the log (thread safe).
137
         */
138
        void log_r(const char *const log_text, ...) A_NONNULL(2)
139
#ifdef __GNUC__
140
#ifdef __OpenBSD__
141
            __attribute__((__format__(printf, 2, 3)))
142
#else  // __OpenBSD__
143
144
#ifdef BAD_CILKPLUS
145
            __attribute__((__format__(gnu_printf, 1, 2)))
146
#else  // BAD_CILKPLUS
147
148
            __attribute__((__format__(gnu_printf, 2, 3)))
149
#endif  // BAD_CILKPLUS
150
151
#endif  // __OpenBSD__
152
#endif  // __GNUC__
153
            ;
154
155
        /**
156
         * Enters a message in the log. The message will be timestamped.
157
         */
158
        void log1(const char *const log_text) A_NONNULL(2);
159
160
        /**
161
         * Enters a message in the log. The message will be timestamped.
162
         */
163
        void log(const std::string &str);
164
165
        void flush();
166
167
#ifdef ENABLEDEBUGLOG
168
        /**
169
         * Enters debug message in the log. The message will be timestamped.
170
         */
171
        void dlog(const std::string &str);
172
173
        void dlog2(const std::string &str,
174
                   const int pos,
175
                   const char* const comment);
176
#endif  // ENABLEDEBUGLOG
177
178
        void setDebugLog(const bool n)
179
        { mDebugLog = n; }
180
181
        void setReportUnimplemented(const bool n)
182
        { mReportUnimplemented = n; }
183
184
        /**
185
         * Log an error and quit. The error will pop-up on Windows and Mac, and
186
         * will be printed to standard error everywhere else.
187
         */
188
        void error(const std::string &error_text)
189
#ifndef BAD_CILKPLUS
190
        __attribute__ ((noreturn))
191
#endif  // BAD_CILKPLUS
192
        ;
193
194
        /**
195
         * Log an error and quit. The error will pop-up on Windows and Mac, and
196
         * will be printed to standard error everywhere else.
197
         */
198
        void safeError(const std::string &error_text)
199
#ifndef BAD_CILKPLUS
200
        __attribute__ ((noreturn))
201
#endif  // BAD_CILKPLUS
202
        ;
203
204
        void unimplemented(const int id);
205
206
        void unimplemented(const int id,
207
                           const int id2);
208
209
        void unimplemented(const uint32_t id,
210
                           const uint32_t id2,
211
                           const uint32_t id3) const;
212
213
    private:
214
        std::ofstream mLogFile;
215
        STD_VECTOR<std::string> mDelayedLog;
216
        SDL_mutex *mMutex;
217
        volatile bool mThreadLocked;
218
        bool mLogToStandardOut;
219
        bool mDebugLog;
220
        bool mReportUnimplemented;
221
};
222
223
extern Logger *logger;
224
225
#endif  // LOGGER_H