|
| 1 | +# |
| 2 | +# This file is part of the ansible-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 ansible-pylibssh provides interface between libssh logging and |
| 20 | +Python :py:mod:`logging` 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 that ``ansible-pylibssh`` will not emit any events into the |
| 28 | + :external+python:mod:`logging` subsystem. |
| 29 | +
|
| 30 | +.. data:: ANSIBLE_PYLIBSSH_TRACE |
| 31 | +
|
| 32 | + Indicates that ``ansible-pylibssh`` will make all possible logs available |
| 33 | + to the :mod:`logging` subsystem, generally useful for debugging low-level |
| 34 | + libssh operations. |
| 35 | +
|
| 36 | +The default log level is set to the ``ANSIBLE_PYLIBSSH_TRACE``, which means |
| 37 | +all the messages are fed into the python. Setting different levels from |
| 38 | +the above lost or any value from Python :py:mod:`logging` module will allow |
| 39 | +prevent libssh emitting these logs. |
| 40 | +""" |
| 41 | + |
| 42 | +import logging |
| 43 | + |
| 44 | +from pylibsshext.errors cimport LibsshSessionException |
| 45 | +from pylibsshext.includes cimport callbacks, libssh |
| 46 | + |
| 47 | + |
| 48 | +ANSIBLE_PYLIBSSH_NOLOG = logging.FATAL * 2 |
| 49 | +ANSIBLE_PYLIBSSH_TRACE = int(logging.DEBUG / 2) |
| 50 | + |
| 51 | +LOG_MAP = { |
| 52 | + ANSIBLE_PYLIBSSH_TRACE: libssh.SSH_LOG_TRACE, |
| 53 | + logging.DEBUG: libssh.SSH_LOG_DEBUG, |
| 54 | + logging.INFO: libssh.SSH_LOG_INFO, |
| 55 | + logging.WARNING: libssh.SSH_LOG_WARN, |
| 56 | + ANSIBLE_PYLIBSSH_NOLOG: libssh.SSH_LOG_NONE, |
| 57 | +} |
| 58 | + |
| 59 | +LOG_MAP_REV = { |
| 60 | + **{ |
| 61 | + libssh_name: py_name |
| 62 | + for py_name, libssh_name in LOG_MAP.items() |
| 63 | + }, |
| 64 | +} |
| 65 | + |
| 66 | +# mapping aliases |
| 67 | +LOG_MAP[logging.NOTSET] = libssh.SSH_LOG_TRACE |
| 68 | +LOG_MAP[logging.ERROR] = libssh.SSH_LOG_WARN |
| 69 | +LOG_MAP[logging.CRITICAL] = libssh.SSH_LOG_WARN |
| 70 | + |
| 71 | + |
| 72 | +def _add_trace_log_level(): |
| 73 | + """ |
| 74 | + Add a trace log level to the Python :mod:`logging` system. |
| 75 | + """ |
| 76 | + level_num = ANSIBLE_PYLIBSSH_TRACE |
| 77 | + level_name = "TRACE" |
| 78 | + |
| 79 | + logging.addLevelName(level_num, level_name) |
| 80 | + |
| 81 | + |
| 82 | +cdef void _pylibssh_log_wrapper(int priority, |
| 83 | + const char *function, |
| 84 | + const char *buffer, |
| 85 | + void *userdata) noexcept: |
| 86 | + log_level = LOG_MAP_REV[priority] |
| 87 | + logging.getLogger("ansible-pylibssh").log(log_level, buffer.decode('utf-8')) |
| 88 | + |
| 89 | + |
| 90 | +def _set_log_callback(): |
| 91 | + """ |
| 92 | + Set libssh logging callback |
| 93 | +
|
| 94 | + Our function redirects the messages to python :py:mod:`logging` facility. |
| 95 | + """ |
| 96 | + # Note, that we could also set the set_log_userdata() to access the logger |
| 97 | + # object, but I did not find it much useful when it is global already. |
| 98 | + callbacks.ssh_set_log_callback(_pylibssh_log_wrapper) |
| 99 | + |
| 100 | + |
| 101 | +def _initialize_logging(): |
| 102 | + """ |
| 103 | + Initialize libssh logging subsystem |
| 104 | +
|
| 105 | + This is implemenbed by adding our own log level and registering our callback |
| 106 | + with libssh. |
| 107 | + """ |
| 108 | + # This is done globally, as the libssh logging is not tied to specific |
| 109 | + # session (its thread-local state in libssh) so either very good care |
| 110 | + # needs to be taken to make sure the logger is in place when callback |
| 111 | + # can be called almost from anywhere in the code or just keep it global. |
| 112 | + _add_trace_log_level() |
| 113 | + _set_log_callback() |
| 114 | + |
| 115 | + |
| 116 | +def _set_level(level): |
| 117 | + """ |
| 118 | + Set logging level to the given value from ``LOG_MAP``. |
| 119 | +
|
| 120 | + :param level: The level to set. |
| 121 | + :type level: int |
| 122 | +
|
| 123 | + :raises LibsshSessionException: If the log level is not known to libssh or pylibssh. |
| 124 | +
|
| 125 | + :returns: Nothing. |
| 126 | + :rtype: None |
| 127 | + """ |
| 128 | + if level not in LOG_MAP: |
| 129 | + raise LibsshSessionException(f'Invalid log level [{level:d}]') |
| 130 | + |
| 131 | + # can never fail for valid values from the map |
| 132 | + libssh.ssh_set_log_level(LOG_MAP[level]) |
0 commit comments