Skip to content

Commit 40eb9fc

Browse files
committed
log: expose ly_temp_log_options with a context manager
When one wishes to disable logging temporarily (e.g., when calling API functions when an error result is expected and OK), libyang provides ly_temp_log_options(). We need access to this in python, so export access to the function and add a python context (i.e., "with") manager API for using it idiomatically as well. ex usage: ``` with temp_log_options(0): ly_unwanted_logging_call(); ``` Signed-off-by: Christian Hopps <[email protected]>
1 parent 17216d8 commit 40eb9fc

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

cffi/cdefs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ enum ly_stmt {
182182
#define LY_LOSTORE ...
183183
#define LY_LOSTORE_LAST ...
184184
int ly_log_options(int);
185+
uint32_t *ly_temp_log_options(uint32_t *);
185186

186187
LY_LOG_LEVEL ly_log_level(LY_LOG_LEVEL);
187188
extern "Python" void lypy_log_cb(LY_LOG_LEVEL, const char *, const char *, const char *, uint64_t);

libyang/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
)
6666
from .extension import ExtensionPlugin, LibyangExtensionError
6767
from .keyed_list import KeyedList
68-
from .log import configure_logging
68+
from .log import configure_logging, temp_log_options
6969
from .schema import (
7070
Extension,
7171
ExtensionCompiled,

libyang/log.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
# Copyright (c) 2020 6WIND S.A.
33
# SPDX-License-Identifier: MIT
44

5+
from contextlib import contextmanager
56
import logging
67

8+
import cffi
9+
710
from _libyang import ffi, lib
811
from .util import c2str
912

@@ -26,6 +29,16 @@ def get_libyang_level(py_level):
2629
return None
2730

2831

32+
@contextmanager
33+
def temp_log_options(opt: int = 0):
34+
_ffi = cffi.FFI()
35+
opts = _ffi.new("uint32_t *", opt)
36+
37+
lib.ly_temp_log_options(opts)
38+
yield
39+
lib.ly_temp_log_options(ffi.NULL)
40+
41+
2942
@ffi.def_extern(name="lypy_log_cb")
3043
def libyang_c_logging_callback(level, msg, data_path, schema_path, line):
3144
args = [c2str(msg)]

0 commit comments

Comments
 (0)