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
11enum mLogLevel {
12 mLOG_FATAL = 0x01,
13 mLOG_ERROR = 0x02,
14 mLOG_WARN = 0x04,
15 mLOG_INFO = 0x08,
16 mLOG_DEBUG = 0x10,
17 mLOG_STUB = 0x20,
18 mLOG_GAME_ERROR = 0x40
19};
20
21struct mLogger {
22 ATTRIBUTE_FORMAT(printf, 4, 5)
23 void (*log)(struct mLogger*, int category, enum mLogLevel level, const char* format, ...);
24};
25
26struct mLogger* mLogGetContext(void);
27int mLogGenerateCategory(void);
28
29ATTRIBUTE_FORMAT(printf, 3, 4)
30static inline void _mLog(int (*category)(void), enum mLogLevel level, const char* format, ...) {
31 struct mLogger* context = mLogGetContext();
32 va_list args;
33 va_start(args, format);
34 if (context) {
35 context->log(context, category(), level, format, args);
36 } else {
37 vprintf(format, args);
38 printf("\n");
39 }
40 va_end(args);
41}
42
43#define mLOG(CATEGORY, LEVEL, ...) _mLog(_mLOG_CAT_ ## CATEGORY, mLOG_ ## LEVEL, __VA_ARGS__)
44
45#define mLOG_DECLARE_CATEGORY(CATEGORY) int _mLOG_CAT_ ## CATEGORY (void);
46#define mLOG_DEFINE_CATEGORY(CATEGORY) \
47 int _mLOG_CAT_ ## CATEGORY (void) { \
48 static int category = 0; \
49 if (!category) { \
50 category = mLogGenerateCategory(); \
51 } \
52 return category; \
53 }
54
55#endif