src/platform/python/mgba/log.py (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/.
6from ._pylib import ffi, lib # pylint: disable=no-name-in-module
7from . import create_callback
8
9create_callback("mLoggerPy", "log", "_pyLog")
10
11
12def install_default(logger):
13 Logger.install_default(logger)
14
15
16class Logger(object):
17 FATAL = lib.mLOG_FATAL
18 DEBUG = lib.mLOG_DEBUG
19 INFO = lib.mLOG_INFO
20 WARN = lib.mLOG_WARN
21 ERROR = lib.mLOG_ERROR
22 STUB = lib.mLOG_STUB
23 GAME_ERROR = lib.mLOG_GAME_ERROR
24
25 _DEFAULT_LOGGER = None
26
27 def __init__(self):
28 self._handle = ffi.new_handle(self)
29 self._native = ffi.gc(lib.mLoggerPythonCreate(self._handle), lib.free)
30
31 @staticmethod
32 def category_name(category):
33 return ffi.string(lib.mLogCategoryName(category)).decode('UTF-8')
34
35 @classmethod
36 def install_default(cls, logger):
37 cls._DEFAULT_LOGGER = logger
38 lib.mLogSetDefaultLogger(logger._native)
39
40 def log(self, category, level, message):
41 print("{}: {}".format(self.category_name(category), message))
42
43
44class NullLogger(Logger):
45 def log(self, category, level, message):
46 pass