all repos — mgba @ 533e96392bde7b4852c37aaaad8e53ce1bf38b0f

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