all repos — mgba @ 752b4faaff506092ed6017f14d6adb6463337243

mGBA Game Boy Advance Emulator

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	void (*log)(struct mLogger*, int category, enum mLogLevel level, const char* format, va_list args);
23};
24
25struct mLogger* mLogGetContext(void);
26int mLogGenerateCategory(const char*);
27const char* mLogCategoryName(int);
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		printf("%s: ", mLogCategoryName(category()));
38		vprintf(format, args);
39		printf("\n");
40	}
41	va_end(args);
42}
43
44#define mLOG(CATEGORY, LEVEL, ...) _mLog(_mLOG_CAT_ ## CATEGORY, mLOG_ ## LEVEL, __VA_ARGS__)
45
46#define mLOG_DECLARE_CATEGORY(CATEGORY) int _mLOG_CAT_ ## CATEGORY (void);
47#define mLOG_DEFINE_CATEGORY(CATEGORY, NAME) \
48	int _mLOG_CAT_ ## CATEGORY (void) { \
49		static int category = 0; \
50		if (!category) { \
51			category = mLogGenerateCategory(NAME); \
52		} \
53		return category; \
54	}
55
56mLOG_DECLARE_CATEGORY(STATUS)
57
58#endif