mirror of
https://github.com/JakeHillion/drgn.git
synced 2024-12-23 09:43:06 +00:00
81c8672d4d
Rather than coming up with our own, separate logging API for the Python bindings, let's integrate with the logging module. The straightforward part is creating a logger from the C extension and adding a log callback that calls its log() method. However, syncing the log level between the logging module and libdrgn requires monkey patching. Signed-off-by: Omar Sandoval <osandov@osandov.com>
31 lines
921 B
Python
31 lines
921 B
Python
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
# SPDX-License-Identifier: LGPL-2.1-or-later
|
|
|
|
import logging
|
|
import sys
|
|
import unittest
|
|
|
|
from drgn import Program
|
|
from tests import TestCase
|
|
|
|
|
|
# Test that our monkey patch to sync the log level between the logging module
|
|
# and libdrgn works.
|
|
class TestLogging(TestCase):
|
|
def test_set_level_before(self):
|
|
logger = logging.getLogger("drgn")
|
|
with self.assertLogs(logger, "DEBUG") as cm:
|
|
prog = Program()
|
|
prog._log(0, "foo")
|
|
self.assertIn("DEBUG:drgn:foo", cm.output)
|
|
|
|
@unittest.skipIf(
|
|
sys.version_info < (3, 7), "syncing log level only works since Python 3.7"
|
|
)
|
|
def test_set_level_after(self):
|
|
prog = Program()
|
|
logger = logging.getLogger("drgn")
|
|
with self.assertLogs(logger, "DEBUG") as cm:
|
|
prog._log(0, "bar")
|
|
self.assertIn("DEBUG:drgn:bar", cm.output)
|