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 mLOG_ALL = 0x7F
21};
22
23struct mLogger {
24 void (*log)(struct mLogger*, int category, enum mLogLevel level, const char* format, va_list args);
25};
26
27struct mLogger* mLogGetContext(void);
28void mLogSetDefaultLogger(struct mLogger*);
29int mLogGenerateCategory(const char*);
30const char* mLogCategoryName(int);
31
32ATTRIBUTE_FORMAT(printf, 3, 4)
33static inline void mLog(int category, enum mLogLevel level, const char* format, ...) {
34 struct mLogger* context = mLogGetContext();
35 va_list args;
36 va_start(args, format);
37 if (context) {
38 context->log(context, category, level, format, args);
39 } else {
40 printf("%s: ", mLogCategoryName(category));
41 vprintf(format, args);
42 printf("\n");
43 }
44 va_end(args);
45}
46
47#define mLOG(CATEGORY, LEVEL, ...) mLog(_mLOG_CAT_ ## CATEGORY (), mLOG_ ## LEVEL, __VA_ARGS__)
48
49#define mLOG_DECLARE_CATEGORY(CATEGORY) int _mLOG_CAT_ ## CATEGORY (void);
50#define mLOG_DEFINE_CATEGORY(CATEGORY, NAME) \
51 int _mLOG_CAT_ ## CATEGORY (void) { \
52 static int category = 0; \
53 if (!category) { \
54 category = mLogGenerateCategory(NAME); \
55 } \
56 return category; \
57 }
58
59mLOG_DECLARE_CATEGORY(STATUS)
60
61#endif