|
| 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 | +import logging |
| 19 | + |
| 20 | +from pylibsshext.errors cimport LibsshSessionException |
| 21 | + |
| 22 | + |
| 23 | +ANSIBLE_PYLIBSSH_TRACE = 5 |
| 24 | + |
| 25 | +LOG_MAP = { |
| 26 | + logging.NOTSET: libssh.SSH_LOG_NONE, |
| 27 | + ANSIBLE_PYLIBSSH_TRACE: libssh.SSH_LOG_TRACE, |
| 28 | + logging.DEBUG: libssh.SSH_LOG_DEBUG, |
| 29 | + logging.INFO: libssh.SSH_LOG_INFO, |
| 30 | + logging.WARNING: libssh.SSH_LOG_WARN, |
| 31 | + logging.ERROR: libssh.SSH_LOG_WARN, |
| 32 | + logging.CRITICAL: libssh.SSH_LOG_WARN |
| 33 | +} |
| 34 | + |
| 35 | +LOG_MAP_REV = { |
| 36 | + libssh.SSH_LOG_NONE: logging.NOTSET, |
| 37 | + libssh.SSH_LOG_TRACE: ANSIBLE_PYLIBSSH_TRACE, |
| 38 | + libssh.SSH_LOG_DEBUG: logging.DEBUG, |
| 39 | + libssh.SSH_LOG_INFO: logging.INFO, |
| 40 | + libssh.SSH_LOG_WARN: logging.WARNING, |
| 41 | +} |
| 42 | + |
| 43 | +logger = logging.getLogger("libssh") |
| 44 | + |
| 45 | + |
| 46 | +def add_trace_log_level(): |
| 47 | + level_num = ANSIBLE_PYLIBSSH_TRACE |
| 48 | + level_name = "TRACE" |
| 49 | + method_name = level_name.lower() |
| 50 | + logger_class = logging.getLoggerClass() |
| 51 | + |
| 52 | + if hasattr(logging, level_name): |
| 53 | + raise AttributeError('{} already defined in logging module'.format(level_name)) |
| 54 | + if hasattr(logging, method_name): |
| 55 | + raise AttributeError('{} already defined in logging module'.format(method_name)) |
| 56 | + if hasattr(logger_class, method_name): |
| 57 | + raise AttributeError('{} already defined in logger class'.format(method_name)) |
| 58 | + |
| 59 | + def logForLevel(self, message, *args, **kwargs): |
| 60 | + if self.isEnabledFor(level_num): |
| 61 | + self._log(level_num, message, args, **kwargs) |
| 62 | + |
| 63 | + def logToRoot(message, *args, **kwargs): |
| 64 | + logging.log(level_num, message, *args, **kwargs) |
| 65 | + |
| 66 | + logging.addLevelName(level_num, level_name) |
| 67 | + setattr(logging, level_name, level_num) |
| 68 | + setattr(logging, method_name, logToRoot) |
| 69 | + setattr(logger_class, method_name, logForLevel) |
| 70 | + |
| 71 | + |
| 72 | +cdef void _pylibssh_log_wrapper(int priority, |
| 73 | + const char *function, |
| 74 | + const char *buffer, |
| 75 | + void *userdata) noexcept nogil: |
| 76 | + with gil: |
| 77 | + log_level = LOG_MAP_REV[priority] |
| 78 | + logger.log(log_level, f"{buffer}") |
| 79 | + |
| 80 | + |
| 81 | +def set_log_callback(): |
| 82 | + callbacks.ssh_set_log_callback(_pylibssh_log_wrapper) |
| 83 | + |
| 84 | + |
| 85 | +def logging_init(): |
| 86 | + try: |
| 87 | + add_trace_log_level() |
| 88 | + except AttributeError: |
| 89 | + pass |
| 90 | + set_log_callback() |
| 91 | + |
| 92 | + |
| 93 | +def set_level(level): |
| 94 | + logging_init() |
| 95 | + if level in LOG_MAP.keys(): |
| 96 | + rc = libssh.ssh_set_log_level(LOG_MAP[level]) |
| 97 | + if rc != libssh.SSH_OK: |
| 98 | + raise LibsshSessionException("Failed to set log level [%d] with error [%d]" % (level, rc)) |
| 99 | + logger.setLevel(level) |
| 100 | + else: |
| 101 | + raise LibsshSessionException("Invalid log level [%d]" % level) |
0 commit comments