Skip to content

Commit ad059d6

Browse files
choppsv1rjarry
authored andcommitted
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 cec98f8 commit ad059d6

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-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
Enum,
7171
Extension,

libyang/log.py

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

5+
from contextlib import contextmanager
56
import logging
67

78
from _libyang import ffi, lib
@@ -26,6 +27,15 @@ def get_libyang_level(py_level):
2627
return None
2728

2829

30+
@contextmanager
31+
def temp_log_options(opt: int = 0):
32+
opts = ffi.new("uint32_t *", opt)
33+
34+
lib.ly_temp_log_options(opts)
35+
yield
36+
lib.ly_temp_log_options(ffi.NULL)
37+
38+
2939
@ffi.def_extern(name="lypy_log_cb")
3040
def libyang_c_logging_callback(level, msg, data_path, schema_path, line):
3141
args = [c2str(msg)]

0 commit comments

Comments
 (0)