all repos — mgba @ 000f232c582b0981b66d48d7af799e97faffad8e

mGBA Game Boy Advance Emulator

src/core/log.c (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#include <mgba/core/log.h>
 7
 8#include <mgba/core/thread.h>
 9
10#define MAX_CATEGORY 64
11
12static struct mLogger* _defaultLogger = NULL;
13
14struct mLogger* mLogGetContext(void) {
15	struct mLogger* logger = NULL;
16#ifndef DISABLE_THREADING
17	logger = mCoreThreadLogger();
18#endif
19	if (logger) {
20		return logger;
21	}
22	return _defaultLogger;
23}
24
25void mLogSetDefaultLogger(struct mLogger* logger) {
26	_defaultLogger = logger;
27}
28
29static int _category = 0;
30static const char* _categoryNames[MAX_CATEGORY];
31static const char* _categoryIds[MAX_CATEGORY];
32
33int mLogGenerateCategory(const char* name, const char* id) {
34	if (_category < MAX_CATEGORY) {
35		_categoryNames[_category] = name;
36		_categoryIds[_category] = id;
37	}
38	++_category;
39	return _category;
40}
41
42const char* mLogCategoryName(int category) {
43	if (category < MAX_CATEGORY) {
44		return _categoryNames[category];
45	}
46	return NULL;
47}
48
49const char* mLogCategoryId(int category) {
50	if (category < MAX_CATEGORY) {
51		return _categoryIds[category];
52	}
53	return NULL;
54}
55
56int mLogCategoryById(const char* id) {
57	int i;
58	for (i = 0; i < _category; ++i) {
59		if (strcmp(_categoryIds[i], id) == 0) {
60			return i;
61		}
62	}
63	return -1;
64}
65
66void mLog(int category, enum mLogLevel level, const char* format, ...) {
67	struct mLogger* context = mLogGetContext();
68	va_list args;
69	va_start(args, format);
70	if (context) {
71		context->log(context, category, level, format, args);
72	} else {
73		printf("%s: ", mLogCategoryName(category));
74		vprintf(format, args);
75		printf("\n");
76	}
77	va_end(args);
78}
79
80mLOG_DEFINE_CATEGORY(STATUS, "Status", "core.status")