-
-
Notifications
You must be signed in to change notification settings - Fork 33
Fix the log levels mapping #597
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: devel
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Fixed log level mapping to cover whole libssh range -- by :user:`Jakuje`. | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Made libssh use python logging system -- by :user:`Jakuje`. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# distutils: libraries = ssh | ||
Jakuje marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# | ||
# This file is part of the pylibssh library | ||
# | ||
# This library is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU Lesser General Public | ||
# License as published by the Free Software Foundation; either | ||
# version 2.1 of the License, or (at your option) any later version. | ||
# | ||
# This library is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
# Lesser General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Lesser General Public | ||
# License along with this library; if not, see file LICENSE.rst in this | ||
# repository. | ||
# | ||
|
||
# This file is needed to allow imports from the adjacent pyx file |
Jakuje marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
# | ||
# This file is part of the pylibssh library | ||
# | ||
# This library is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU Lesser General Public | ||
# License as published by the Free Software Foundation; either | ||
# version 2.1 of the License, or (at your option) any later version. | ||
# | ||
# This library is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
# Lesser General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Lesser General Public | ||
# License along with this library; if not, see file LICENSE.rst in this | ||
# repository. | ||
|
||
""" | ||
The Logging module of pylibssh providers interface between libssh logging and | ||
python log facility. | ||
|
||
It provides two new log levels, that can be used with to set the log verbosity | ||
using ``set_log_level()`` method on ``Session`` object. | ||
|
||
.. data:: ANSIBLE_PYLIBSSH_NOLOG | ||
|
||
Indicates the pylibssh will not log any events. | ||
|
||
.. data:: ANSIBLE_PYLIBSSH_TRACE | ||
|
||
Indicates the pylibssh will produce all possible logs, generally useful for debugging low-level libssh operations. | ||
|
||
""" | ||
|
||
import logging | ||
|
||
from pylibsshext.errors cimport LibsshSessionException | ||
from pylibsshext.includes cimport callbacks, libssh | ||
|
||
|
||
ANSIBLE_PYLIBSSH_NOLOG = logging.FATAL * 2 | ||
ANSIBLE_PYLIBSSH_TRACE = int(logging.DEBUG / 2) | ||
|
||
_NOT_SET_SENTINEL = object() | ||
|
||
LOG_MAP = { | ||
logging.NOTSET: _NOT_SET_SENTINEL, | ||
ANSIBLE_PYLIBSSH_TRACE: libssh.SSH_LOG_TRACE, | ||
logging.DEBUG: libssh.SSH_LOG_DEBUG, | ||
logging.INFO: libssh.SSH_LOG_INFO, | ||
logging.WARNING: libssh.SSH_LOG_WARN, | ||
ANSIBLE_PYLIBSSH_NOLOG: libssh.SSH_LOG_NONE, | ||
} | ||
|
||
LOG_MAP_REV = { | ||
**{ | ||
libssh_name: py_name | ||
for py_name, libssh_name in LOG_MAP.items() | ||
}, | ||
} | ||
|
||
# mapping aliases | ||
LOG_MAP[logging.ERROR] = libssh.SSH_LOG_WARN | ||
LOG_MAP[logging.CRITICAL] = libssh.SSH_LOG_WARN | ||
|
||
|
||
_logger = logging.getLogger("ansible-pylibssh") | ||
|
||
|
||
def _add_trace_log_level(): | ||
""" | ||
Adds a trace log level to the python logging system. | ||
""" | ||
level_num = ANSIBLE_PYLIBSSH_TRACE | ||
level_name = "TRACE" | ||
Jakuje marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
logging.addLevelName(level_num, level_name) | ||
|
||
|
||
cdef void _pylibssh_log_wrapper(int priority, | ||
const char *function, | ||
const char *buffer, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note to myself: |
||
void *userdata) noexcept: | ||
log_level = LOG_MAP_REV[priority] | ||
_logger.log(log_level, buffer.decode('utf-8')) | ||
|
||
|
||
def _set_log_callback(): | ||
""" | ||
Note, that we could also set the set_log_userdata() to access the logger object, | ||
but I did not find it much useful when it is global already. | ||
""" | ||
callbacks.ssh_set_log_callback(_pylibssh_log_wrapper) | ||
|
||
|
||
def _initialize_logging(): | ||
""" | ||
This is done globally, as the libssh logging is not tied to specific session | ||
(its thread-local state in libssh) so either very good care needs to be taken | ||
to make sure the logger is in place when callback can be called almost from | ||
anywhere in the code or just keep it global. | ||
""" | ||
_add_trace_log_level() | ||
_set_log_callback() | ||
|
||
|
||
def _set_level(level): | ||
""" | ||
Set logging level to the given value from LOG_MAP | ||
|
||
:param level: The level to set. | ||
|
||
:raises LibsshSessionException: If the log level is not known by pylibssh. | ||
|
||
:return: Nothing. | ||
:rtype: NoneType | ||
""" | ||
_initialize_logging() | ||
if level not in LOG_MAP: | ||
raise LibsshSessionException(f'Invalid log level [{level:d}]') | ||
|
||
# Special case not-set does not go deeper | ||
if level == _NOT_SET_SENTINEL: | ||
return | ||
|
||
rc = libssh.ssh_set_log_level(LOG_MAP[level]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this special-case There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is not clear to me as the libssh mapping and python is that different. I understand the NOTSET value that we should log everything and let the underlying code handle that. If we would skip this call on NOTSET, we would just keep the previously set log level, which might be TRACE, NONE, or anything in between, which does not sound right to me. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Jakuje so the way the
>>> import logging
>>> logging.NOTSET
0 I should note, though, that loggers have hierarchy. And so there's a "root logger" and "child" loggers. When a logger has a level of So I'd say |
||
if rc != libssh.SSH_OK: | ||
raise LibsshSessionException( | ||
f'Failed to set log level [{level:d}] with error [{rc:d}]', | ||
) | ||
_logger.setLevel(level) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure about this change note, it doesn't seem to reveal the effect of the change on the end users. But let's circle back to it later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's unclear if the change might constitute a feature.