Skip to content

Commit

Permalink
use forwarded logs
Browse files Browse the repository at this point in the history
  • Loading branch information
theomonnom committed Nov 17, 2023
1 parent 413d563 commit 171e435
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 18 deletions.
34 changes: 34 additions & 0 deletions livekit-rtc/livekit/rtc/_ffi_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

import asyncio
import logging
import ctypes
import os
import platform
Expand All @@ -24,6 +25,8 @@
from ._proto import ffi_pb2 as proto_ffi
from ._utils import Queue

logger = logging.getLogger("livekit")


def get_ffi_lib_path():
# allow to override the lib path using an env var
Expand Down Expand Up @@ -121,9 +124,39 @@ def ffi_event_callback(
event_data = bytes(data_ptr[: int(data_len)])
event = proto_ffi.FfiEvent()
event.ParseFromString(event_data)

which = event.WhichOneof("message")
if which == "logs":
for record in event.logs.records:
logger.log(
to_python_level(record.level),
"%s:%s:%s - %s",
record.target,
record.line,
record.module_path,
record.message,
)

return # no need to queue the logs

ffi_client.queue.put(event)


def to_python_level(level: proto_ffi.LogLevel.ValueType) -> int:
if level == proto_ffi.LogLevel.LOG_ERROR:
return logging.ERROR
elif level == proto_ffi.LogLevel.LOG_WARN:
return logging.WARN
elif level == proto_ffi.LogLevel.LOG_INFO:
return logging.INFO
elif level == proto_ffi.LogLevel.LOG_DEBUG:
return logging.DEBUG
elif level == proto_ffi.LogLevel.LOG_TRACE:
return logging.DEBUG

raise Exception("unreachable")


class FfiClient:
def __init__(self) -> None:
self._lock = threading.RLock()
Expand All @@ -133,6 +166,7 @@ def __init__(self) -> None:
req = proto_ffi.FfiRequest()
cb_callback = int(ctypes.cast(ffi_event_callback, ctypes.c_void_p).value) # type: ignore
req.initialize.event_callback_ptr = cb_callback
req.initialize.capture_logs = True # capture logs on Python
self.request(req)

@property
Expand Down
30 changes: 18 additions & 12 deletions livekit-rtc/livekit/rtc/_proto/ffi_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 171e435

Please sign in to comment.