Skip to content

Commit

Permalink
Merge pull request #100 from maniek2332/ft/spdlog
Browse files Browse the repository at this point in the history
Updated logging functions
  • Loading branch information
labuzm committed Oct 31, 2020
2 parents f203917 + bcf2713 commit fe89047
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 86 deletions.
4 changes: 2 additions & 2 deletions demos/basic/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from kaa.engine import Engine, Scene, VirtualResolutionMode
from kaa.geometry import Segment, Circle, Polygon, Vector
from kaa.log import (
set_core_logging_level, CoreLogLevel, CoreLogCategory
set_core_logging_level, CoreLogLevel
)


Expand Down Expand Up @@ -124,7 +124,7 @@ def update(self, dt):

if __name__ == '__main__':
with Engine(virtual_resolution=Vector(5, 5)) as engine:
set_core_logging_level(CoreLogCategory.engine, CoreLogLevel.debug)
set_core_logging_level("engine", CoreLogLevel.debug)
scene = DemoScene(
sound_path=len(sys.argv) >= 2 and sys.argv[1],
music_path=len(sys.argv) >= 3 and sys.argv[2],
Expand Down
8 changes: 5 additions & 3 deletions demos/log/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from kaa.log import get_core_logging_level, set_core_logging_level, CoreLogLevel, CoreLogCategory
from kaa.log import (
get_core_logging_level, set_core_logging_level, CoreLogLevel,
)

from kaa.engine import Engine, Scene
from kaa.geometry import Vector
Expand All @@ -17,6 +19,6 @@ def update(self, dt):
engine.window.size = Vector(400, 200)
engine.window.center()

print(get_core_logging_level(CoreLogCategory.renderer))
print(get_core_logging_level("renderer"))

engine.run(scene)
engine.run(scene)
3 changes: 0 additions & 3 deletions demos/transformations/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
from kaa.timers import Timer
from kaa.input import Keycode, MouseButton
from kaa.colors import Color
from kaa.log import (
set_core_logging_level, CoreLogLevel, CoreLogCategory
)

class MyNode(Node):

Expand Down
8 changes: 4 additions & 4 deletions src/kaa/engine.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ from .kaacore.engine cimport (
CEngine, get_c_engine, is_c_engine_initialized, CVirtualResolutionMode
)
from .kaacore.display cimport CDisplay
from .kaacore.log cimport c_log_dynamic, CLogCategory, CLogLevel
from .kaacore.log cimport c_emit_log_dynamic, CLogLevel, _log_category_wrapper

from . import __version__

Expand Down Expand Up @@ -145,8 +145,8 @@ def Engine(Vector virtual_resolution, virtual_resolution_mode=None):
global _c_engine_instance
_c_engine_instance = unique_ptr[CEngine](c_engine_ptr)

c_log_dynamic(
CLogLevel.info, CLogCategory.engine, 'Engine initialized.'
c_emit_log_dynamic(
CLogLevel.info, _log_category_wrapper, 'Engine initialized.'
)
_print_hello_message()

Expand Down Expand Up @@ -178,4 +178,4 @@ _ \ / /
""".lstrip('\n').rstrip().format(version=__version__)

for line in kaa_ascii_logo.split('\n'):
c_log_dynamic(CLogLevel.info, CLogCategory.engine, line.encode())
c_emit_log_dynamic(CLogLevel.info, _log_category_wrapper, line.encode())
16 changes: 10 additions & 6 deletions src/kaa/extra/include/pythonic_callback.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ struct PythonicCallbackWrapper {
PythonicCallbackWrapper()
: py_callback(nullptr), is_weakref(false)
{
log<LogLevel::debug>("Creating empty PythonicCallbackWrapper.");
KAACORE_LOG_DEBUG("Creating empty PythonicCallbackWrapper.");
}

PythonicCallbackWrapper(PyObject* py_callback, bool is_weakref=false)
: py_callback(py_callback), is_weakref(is_weakref)
{
log<LogLevel::debug>("Creating PythonicCallbackWrapper: %p.", py_callback);
KAACORE_LOG_DEBUG("Creating PythonicCallbackWrapper: {}.",
fmt::ptr(py_callback));
PyGILState_STATE gstate = PyGILState_Ensure();
Py_INCREF(this->py_callback);
PyGILState_Release(gstate);
Expand All @@ -42,7 +43,8 @@ struct PythonicCallbackWrapper {
PyGILState_STATE gstate = PyGILState_Ensure();
this->py_callback = other.py_callback;
this->is_weakref = other.is_weakref;
log<LogLevel::debug>("Copying PythonicCallbackWrapper: %p.", this->py_callback);
KAACORE_LOG_DEBUG("Copying PythonicCallbackWrapper: {}.",
fmt::ptr(this->py_callback));
Py_INCREF(this->py_callback);
PyGILState_Release(gstate);
}
Expand All @@ -52,16 +54,18 @@ struct PythonicCallbackWrapper {
{
other.py_callback = nullptr;
other.is_weakref = false;
log<LogLevel::debug>("Moving PythonicCallbackWrapper: %p.", this->py_callback);
KAACORE_LOG_DEBUG("Moving PythonicCallbackWrapper: {}.",
fmt::ptr(this->py_callback));
}

~PythonicCallbackWrapper()
{
if (this->py_callback != nullptr) {
PyGILState_STATE gstate = PyGILState_Ensure();
Py_DECREF(this->py_callback);
log<LogLevel::debug>(
"Destroying PythonicCallbackWrapper: %p.", this->py_callback
KAACORE_LOG_DEBUG(
"Destroying PythonicCallbackWrapper: {}.",
fmt::ptr(this->py_callback)
);
PyGILState_Release(gstate);
}
Expand Down
34 changes: 15 additions & 19 deletions src/kaa/kaacore/log.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,23 @@ from .exceptions cimport raise_py_error


cdef extern from "kaacore/log.h" nogil:
cdef enum CLogLevel "kaacore::LogLevel":
verbose "kaacore::LogLevel::verbose",
debug "kaacore::LogLevel::debug",
info "kaacore::LogLevel::info",
warn "kaacore::LogLevel::warn",
error "kaacore::LogLevel::error",
critical "kaacore::LogLevel::critical",
size_t _log_category_app "kaacore::_log_category_app"
size_t _log_category_wrapper "kaacore::_log_category_wrapper"

cdef enum CLogCategory "kaacore::LogCategory":
engine "kaacore::LogCategory::engine",
renderer "kaacore::LogCategory::renderer",
input "kaacore::LogCategory::input",
audio "kaacore::LogCategory::audio",
nodes "kaacore::LogCategory::nodes",
physics "kaacore::LogCategory::physics",
misc "kaacore::LogCategory::misc",
application "kaacore::LogCategory::application",
cdef enum CLogLevel "spdlog::level::level_enum":
trace "spdlog::level::level_enum::trace",
debug "spdlog::level::level_enum::debug",
info "spdlog::level::level_enum::info",
warn "spdlog::level::level_enum::warn",
error "spdlog::level::level_enum::err",
critical "spdlog::level::level_enum::critical",
off "spdlog::level::level_enum::off",

void c_log_dynamic "log_dynamic"(const CLogLevel level, const CLogCategory category, const char* msg)
void c_emit_log_dynamic "emit_log_dynamic"(const CLogLevel level, const size_t logger_index, const char* msg)

CLogLevel c_get_logging_level "get_logging_level"(const CLogCategory category)
void c_set_logging_level "set_logging_level"(const CLogCategory category, const CLogLevel level)
CLogLevel c_get_logging_level "get_logging_level"(const char* category) \
except +raise_py_error
void c_set_logging_level "set_logging_level"(const char* category, const CLogLevel level) \
except +raise_py_error

void c_initialize_logging "initialize_logging"() except +raise_py_error
54 changes: 11 additions & 43 deletions src/kaa/log.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,24 @@ import logging
from enum import IntEnum

from .kaacore.log cimport (
c_log_dynamic, CLogLevel, CLogCategory, c_get_logging_level,
c_set_logging_level, c_initialize_logging
c_emit_log_dynamic, CLogLevel, c_get_logging_level,
c_set_logging_level, c_initialize_logging, _log_category_app
)


class CoreLogLevel(IntEnum):
verbose = <uint32_t>CLogLevel.verbose
trace = <uint32_t>CLogLevel.trace
debug = <uint32_t>CLogLevel.debug
info = <uint32_t>CLogLevel.info
warn = <uint32_t>CLogLevel.warn
error = <uint32_t>CLogLevel.error
critical = <uint32_t>CLogLevel.critical


class CoreLogCategory(IntEnum):
engine = <uint32_t>CLogCategory.engine
renderer = <uint32_t>CLogCategory.renderer
input = <uint32_t>CLogCategory.input
audio = <uint32_t>CLogCategory.audio
nodes = <uint32_t>CLogCategory.nodes
physics = <uint32_t>CLogCategory.physics
misc = <uint32_t>CLogCategory.misc
application = <uint32_t>CLogCategory.application
off = <uint32_t>CLogLevel.off


cdef CLogLevel _python_to_core_level(level):
if level < 10:
return CLogLevel.verbose
return CLogLevel.trace
elif 10 <= level < 20:
return CLogLevel.debug
elif 20 <= level < 30:
Expand All @@ -42,27 +32,7 @@ cdef CLogLevel _python_to_core_level(level):
return CLogLevel.critical


cdef int _core_to_python_level(CLogLevel level):
if level == CLogLevel.verbose:
return logging.NOTSET
elif level == CLogLevel.debug:
return logging.DEBUG
elif level == CLogLevel.info:
return logging.INFO
elif level == CLogLevel.warn:
return logging.WARN
elif level == CLogLevel.error:
return logging.ERROR
else:
return logging.CRITICAL


class CoreHandler(logging.Handler):
def __init__(self, core_category=CoreLogCategory.application):
assert isinstance(core_category, CoreLogCategory)
super().__init__()
self._core_category = core_category

def handle(self, record):
# simplified handle (no I/O locks)
cdef int rv = self.filter(record)
Expand All @@ -77,28 +47,26 @@ class CoreHandler(logging.Handler):
except Exception:
self.handleError(record)
else:
c_log_dynamic(
c_emit_log_dynamic(
_python_to_core_level(record.levelno),
<CLogCategory>(<uint32_t>self._core_category),
msg_enc
_log_category_app, msg_enc
)


def get_core_logging_level(core_category):
def get_core_logging_level(str core_category):
return CoreLogLevel(<uint32_t>c_get_logging_level(
<CLogCategory>(<uint32_t>core_category.value)
core_category.encode('ascii')
))


def set_core_logging_level(core_category, level):
def set_core_logging_level(str core_category, level):
cdef CLogLevel core_level
if isinstance(level, CoreLogLevel):
core_level = <CLogLevel>(<uint32_t>level.value)
else:
core_level = _python_to_core_level(logging._checkLevel(level))
c_set_logging_level(
<CLogCategory>(<uint32_t>core_category.value),
core_level
core_category.encode('ascii'), core_level
)

c_initialize_logging()
2 changes: 1 addition & 1 deletion src/kaa/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from ._kaa import (
get_core_logging_level, set_core_logging_level, CoreLogLevel,
CoreLogCategory, CoreHandler,
CoreHandler,
)


Expand Down
8 changes: 4 additions & 4 deletions src/kaa/scenes.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ from .kaacore.glue cimport CPythonicCallbackResult
from .kaacore.nodes cimport CNodePtr
from .kaacore.scenes cimport CScene
from .kaacore.engine cimport is_c_engine_initialized
from .kaacore.log cimport c_log_dynamic, CLogCategory, CLogLevel
from .kaacore.log cimport c_emit_log_dynamic, CLogLevel, _log_category_wrapper
from .kaacore.views cimport views_default_z_index


cdef cppclass CPyScene(CScene):
object py_scene_weakref

__init__(object py_scene):
c_log_dynamic(CLogLevel.debug, CLogCategory.engine,
c_emit_log_dynamic(CLogLevel.debug, _log_category_wrapper,
'Created CPyScene')
this.py_scene_weakref = PyWeakref_NewRef(py_scene, None)

Expand Down Expand Up @@ -79,8 +79,8 @@ cdef class Scene:
'Cannot create scene since engine is not initialized yet.'
)

c_log_dynamic(
CLogLevel.debug, CLogCategory.engine, 'Initializing Scene'
c_emit_log_dynamic(
CLogLevel.debug, _log_category_wrapper, 'Initializing Scene'
)
cdef CPyScene* c_scene = new CPyScene(self)
assert c_scene != NULL
Expand Down

0 comments on commit fe89047

Please sign in to comment.