Skip to content

Commit bd5fe35

Browse files
committed
bind out setting of loglevel
1 parent 1d9521e commit bd5fe35

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

awscrt/io.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@ def init_logging(log_level, file_name):
4040
_awscrt.init_logging(log_level, file_name)
4141

4242

43+
def set_log_level(log_level):
44+
"""Change the log level of `awscrt`.
45+
46+
Args:
47+
log_level (LogLevel): Display messages of this importance and higher.
48+
`LogLevel.NoLogs` will disable logging.
49+
"""
50+
assert log_level is not None
51+
52+
_awscrt.set_log_level(log_level)
53+
4354
class EventLoopGroup(NativeResource):
4455
"""A collection of event-loops.
4556

source/io.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ bool aws_py_socket_options_init(struct aws_socket_options *socket_options, PyObj
2323
*/
2424
PyObject *aws_py_init_logging(PyObject *self, PyObject *args);
2525

26+
/**
27+
* Change logging level of the global logger.
28+
*/
29+
PyObject *aws_py_set_log_level(PyObject *self, PyObject *args);
30+
2631
/**
2732
* Returns True if ALPN is available, False if it is not.
2833
*/

source/module.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,27 @@ PyObject *aws_py_init_logging(PyObject *self, PyObject *args) {
8686
Py_RETURN_NONE;
8787
}
8888

89+
PyObject *aws_py_set_log_level(PyObject *self, PyObject *args) {
90+
(void)self;
91+
92+
if (!s_logger_init) {
93+
aws_raise_error(AWS_ERROR_INVALID_STATE);
94+
return PyErr_AwsLastError();
95+
}
96+
97+
int log_level = 0;
98+
if (!PyArg_ParseTuple(args, "b", &log_level)) {
99+
PyErr_SetNone(PyExc_ValueError);
100+
return NULL;
101+
}
102+
103+
if (aws_logger_set_log_level(&s_logger, log_level) != AWS_OP_SUCCESS) {
104+
return PyErr_AwsLastError();
105+
}
106+
107+
Py_RETURN_NONE;
108+
}
109+
89110
struct aws_byte_cursor aws_byte_cursor_from_pyunicode(PyObject *str) {
90111
Py_ssize_t len;
91112
const char *ptr = PyUnicode_AsUTF8AndSize(str, &len);
@@ -761,6 +782,7 @@ static PyMethodDef s_module_methods[] = {
761782
AWS_PY_METHOD_DEF(tls_connection_options_set_alpn_list, METH_VARARGS),
762783
AWS_PY_METHOD_DEF(tls_connection_options_set_server_name, METH_VARARGS),
763784
AWS_PY_METHOD_DEF(init_logging, METH_VARARGS),
785+
AWS_PY_METHOD_DEF(set_log_level, METH_VARARGS),
764786
AWS_PY_METHOD_DEF(input_stream_new, METH_VARARGS),
765787
AWS_PY_METHOD_DEF(pkcs11_lib_new, METH_VARARGS),
766788

0 commit comments

Comments
 (0)