diff --git a/awscrt/io.py b/awscrt/io.py index faaa46844..4d36ba69f 100644 --- a/awscrt/io.py +++ b/awscrt/io.py @@ -30,7 +30,6 @@ def init_logging(log_level, file_name): Args: log_level (LogLevel): Display messages of this importance and higher. - `LogLevel.NoLogs` will disable logging. file_name (str): Logging destination. To write to stdout or stderr pass 'stdout' or 'stderr' as strings. Otherwise, a file path is assumed. """ @@ -40,6 +39,18 @@ def init_logging(log_level, file_name): _awscrt.init_logging(log_level, file_name) +def set_log_level(log_level): + """Change the log level of `awscrt`. init_logging() must have been called + before using this function or else an exception will be raised. + + Args: + log_level (LogLevel): Display messages of this importance and higher. + """ + assert log_level is not None + + _awscrt.set_log_level(log_level) + + class EventLoopGroup(NativeResource): """A collection of event-loops. diff --git a/source/io.h b/source/io.h index 4fbb1064d..fed29d856 100644 --- a/source/io.h +++ b/source/io.h @@ -23,6 +23,11 @@ bool aws_py_socket_options_init(struct aws_socket_options *socket_options, PyObj */ PyObject *aws_py_init_logging(PyObject *self, PyObject *args); +/** + * Change logging level of the global logger. + */ +PyObject *aws_py_set_log_level(PyObject *self, PyObject *args); + /** * Returns True if ALPN is available, False if it is not. */ diff --git a/source/module.c b/source/module.c index fff5a96b2..be4afdca1 100644 --- a/source/module.c +++ b/source/module.c @@ -86,6 +86,27 @@ PyObject *aws_py_init_logging(PyObject *self, PyObject *args) { Py_RETURN_NONE; } +PyObject *aws_py_set_log_level(PyObject *self, PyObject *args) { + (void)self; + + if (!s_logger_init) { + aws_raise_error(AWS_ERROR_INVALID_STATE); + return PyErr_AwsLastError(); + } + + int log_level = 0; + if (!PyArg_ParseTuple(args, "b", &log_level)) { + PyErr_SetNone(PyExc_ValueError); + return NULL; + } + + if (aws_logger_set_log_level(&s_logger, log_level) != AWS_OP_SUCCESS) { + return PyErr_AwsLastError(); + } + + Py_RETURN_NONE; +} + struct aws_byte_cursor aws_byte_cursor_from_pyunicode(PyObject *str) { Py_ssize_t len; const char *ptr = PyUnicode_AsUTF8AndSize(str, &len); @@ -761,6 +782,7 @@ static PyMethodDef s_module_methods[] = { AWS_PY_METHOD_DEF(tls_connection_options_set_alpn_list, METH_VARARGS), AWS_PY_METHOD_DEF(tls_connection_options_set_server_name, METH_VARARGS), AWS_PY_METHOD_DEF(init_logging, METH_VARARGS), + AWS_PY_METHOD_DEF(set_log_level, METH_VARARGS), AWS_PY_METHOD_DEF(input_stream_new, METH_VARARGS), AWS_PY_METHOD_DEF(pkcs11_lib_new, METH_VARARGS),