src/core/log.h (view raw)
1/* Copyright (c) 2013-2016 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#ifndef M_LOG_H
7#define M_LOG_H
8
9#include "util/common.h"
10
11CXX_GUARD_START
12
13enum mLogLevel {
14 mLOG_FATAL = 0x01,
15 mLOG_ERROR = 0x02,
16 mLOG_WARN = 0x04,
17 mLOG_INFO = 0x08,
18 mLOG_DEBUG = 0x10,
19 mLOG_STUB = 0x20,
20 mLOG_GAME_ERROR = 0x40,
21
22 mLOG_ALL = 0x7F
23};
24
25struct mLogger {
26 void (*log)(struct mLogger*, int category, enum mLogLevel level, const char* format, va_list args);
27};
28
29struct mLogger* mLogGetContext(void);
30void mLogSetDefaultLogger(struct mLogger*);
31int mLogGenerateCategory(const char*);
32const char* mLogCategoryName(int);
33
34ATTRIBUTE_FORMAT(printf, 3, 4)
35static inline void mLog(int category, enum mLogLevel level, const char* format, ...) {
36 struct mLogger* context = mLogGetContext();
37 va_list args;
38 va_start(args, format);
39 if (context) {
40 context->log(context, category, level, format, args);
41 } else {
42 printf("%s: ", mLogCategoryName(category));
43 vprintf(format, args);
44 printf("\n");
45 }
46 va_end(args);
47}
48
49#define mLOG(CATEGORY, LEVEL, ...) mLog(_mLOG_CAT_ ## CATEGORY (), mLOG_ ## LEVEL, __VA_ARGS__)
50
51#define mLOG_DECLARE_CATEGORY(CATEGORY) int _mLOG_CAT_ ## CATEGORY (void);
52#define mLOG_DEFINE_CATEGORY(CATEGORY, NAME) \
53 int _mLOG_CAT_ ## CATEGORY (void) { \
54 static int category = 0; \
55 if (!category) { \
56 category = mLogGenerateCategory(NAME); \
57 } \
58 return category; \
59 }
60
61mLOG_DECLARE_CATEGORY(STATUS)
62
63CXX_GUARD_END
64
65#endif