|
| 1 | +# |
| 2 | +# This file is part of the pylibssh library |
| 3 | +# |
| 4 | +# This library is free software; you can redistribute it and/or |
| 5 | +# modify it under the terms of the GNU Lesser General Public |
| 6 | +# License as published by the Free Software Foundation; either |
| 7 | +# version 2.1 of the License, or (at your option) any later version. |
| 8 | +# |
| 9 | +# This library is distributed in the hope that it will be useful, |
| 10 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | +# Lesser General Public License for more details. |
| 13 | +# |
| 14 | +# You should have received a copy of the GNU Lesser General Public |
| 15 | +# License along with this library; if not, see file LICENSE.rst in this |
| 16 | +# repository. |
| 17 | + |
| 18 | +""" |
| 19 | +The Logging module of pylibssh providers interface between libssh logging and |
| 20 | +python log facility. |
| 21 | +
|
| 22 | +It provides two new log levels, that can be used with to set the log verbosity |
| 23 | +using `set_log_level()` method on `Session` object. |
| 24 | +
|
| 25 | +.. data:: ANSIBLE_PYLIBSSH_NOLOG |
| 26 | +
|
| 27 | + Indicates the pylibssh will not log any events. |
| 28 | +
|
| 29 | +.. data:: ANSIBLE_PYLIBSSH_TRACE |
| 30 | +
|
| 31 | + Indicates the pylibssh will produce all possible logs, generally useful for debugging low-level libssh operations. |
| 32 | +
|
| 33 | +""" |
| 34 | + |
| 35 | +import logging |
| 36 | + |
| 37 | +from pylibsshext.errors cimport LibsshSessionException |
| 38 | +from pylibsshext.includes cimport callbacks, libssh |
| 39 | + |
| 40 | + |
| 41 | +ANSIBLE_PYLIBSSH_NOLOG = logging.FATAL * 2 |
| 42 | +ANSIBLE_PYLIBSSH_TRACE = int(logging.DEBUG / 2) |
| 43 | + |
| 44 | +_NOT_SET_SENTINEL = object() |
| 45 | + |
| 46 | +LOG_MAP = { |
| 47 | + logging.NOTSET: _NOT_SET_SENTINEL, |
| 48 | + ANSIBLE_PYLIBSSH_TRACE: libssh.SSH_LOG_TRACE, |
| 49 | + logging.DEBUG: libssh.SSH_LOG_DEBUG, |
| 50 | + logging.INFO: libssh.SSH_LOG_INFO, |
| 51 | + logging.WARNING: libssh.SSH_LOG_WARN, |
| 52 | + ANSIBLE_PYLIBSSH_NOLOG: libssh.SSH_LOG_NONE, |
| 53 | +} |
| 54 | + |
| 55 | +LOG_MAP_REV = { |
| 56 | + **{ |
| 57 | + libssh_name: py_name |
| 58 | + for py_name, libssh_name in LOG_MAP.items() |
| 59 | + }, |
| 60 | +} |
| 61 | + |
| 62 | +# mapping aliases |
| 63 | +LOG_MAP[logging.ERROR] = libssh.SSH_LOG_WARN |
| 64 | +LOG_MAP[logging.CRITICAL] = libssh.SSH_LOG_WARN |
| 65 | + |
| 66 | + |
| 67 | +_logger = logging.getLogger("ansible-pylibssh") |
| 68 | + |
| 69 | + |
| 70 | +def _add_trace_log_level(): |
| 71 | + """ |
| 72 | + Adds a trace log level to the python logging system. |
| 73 | + """ |
| 74 | + level_num = ANSIBLE_PYLIBSSH_TRACE |
| 75 | + level_name = "TRACE" |
| 76 | + |
| 77 | + logging.addLevelName(level_num, level_name) |
| 78 | + |
| 79 | + |
| 80 | +cdef void _pylibssh_log_wrapper(int priority, |
| 81 | + const char *function, |
| 82 | + const char *buffer, |
| 83 | + void *userdata) noexcept: |
| 84 | + log_level = LOG_MAP_REV[priority] |
| 85 | + _logger.log(log_level, str(buffer)) |
| 86 | + |
| 87 | + |
| 88 | +def _set_log_callback(): |
| 89 | + """ |
| 90 | + Note, that we could also set the set_log_userdata() to access the logger object, |
| 91 | + but I did not find it much useful when it is global already. |
| 92 | + """ |
| 93 | + callbacks.ssh_set_log_callback(_pylibssh_log_wrapper) |
| 94 | + |
| 95 | + |
| 96 | +def _initialize_logging(): |
| 97 | + """ |
| 98 | + This is done globally, as the libssh logging is not tied to specific session |
| 99 | + (its thread-local state in libssh) so either very good care needs to be taken |
| 100 | + to make sure the logger is in place when callback can be called almost from |
| 101 | + anywhere in the code or just keep it global. |
| 102 | + """ |
| 103 | + _add_trace_log_level() |
| 104 | + _set_log_callback() |
| 105 | + |
| 106 | + |
| 107 | +def _set_level(level): |
| 108 | + """ |
| 109 | + Set logging level to the given value from LOG_MAP |
| 110 | +
|
| 111 | + :param level: The level to set. |
| 112 | +
|
| 113 | + :raises LibsshSessionException: If the log level is not known by pylibssh. |
| 114 | +
|
| 115 | + :return: Nothing. |
| 116 | + :rtype: NoneType |
| 117 | + """ |
| 118 | + _initialize_logging() |
| 119 | + if level not in LOG_MAP: |
| 120 | + raise LibsshSessionException(f'Invalid log level [{level:d}]') |
| 121 | + |
| 122 | + # Special case not-set does not go deeper |
| 123 | + if level == _NOT_SET_SENTINEL: |
| 124 | + return |
| 125 | + |
| 126 | + rc = libssh.ssh_set_log_level(LOG_MAP[level]) |
| 127 | + if rc != libssh.SSH_OK: |
| 128 | + raise LibsshSessionException( |
| 129 | + f'Failed to set log level [{level:d}] with error [{rc:d}]', |
| 130 | + ) |
| 131 | + _logger.setLevel(level) |
0 commit comments