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 "log.h"
7
8#include "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];
31
32int mLogGenerateCategory(const char* name) {
33 ++_category;
34 if (_category < MAX_CATEGORY) {
35 _categoryNames[_category] = name;
36 }
37 return _category;
38}
39
40const char* mLogCategoryName(int category) {
41 if (category < MAX_CATEGORY) {
42 return _categoryNames[category];
43 }
44 return 0;
45}
46
47void mLog(int category, enum mLogLevel level, const char* format, ...) {
48 struct mLogger* context = mLogGetContext();
49 va_list args;
50 va_start(args, format);
51 if (context) {
52 context->log(context, category, level, format, args);
53 } else {
54 printf("%s: ", mLogCategoryName(category));
55 vprintf(format, args);
56 printf("\n");
57 }
58 va_end(args);
59}
60
61mLOG_DEFINE_CATEGORY(STATUS, "Status")