ssh
src/log.c@0:c4152c628df5, 2019-11-25 (annotated)
- Committer:
- sPymbed
- Date:
- Mon Nov 25 14:24:05 2019 +0000
- Revision:
- 0:c4152c628df5
first commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sPymbed | 0:c4152c628df5 | 1 | /* log.c |
sPymbed | 0:c4152c628df5 | 2 | * |
sPymbed | 0:c4152c628df5 | 3 | * Copyright (C) 2014-2016 wolfSSL Inc. |
sPymbed | 0:c4152c628df5 | 4 | * |
sPymbed | 0:c4152c628df5 | 5 | * This file is part of wolfSSH. |
sPymbed | 0:c4152c628df5 | 6 | * |
sPymbed | 0:c4152c628df5 | 7 | * wolfSSH is free software; you can redistribute it and/or modify |
sPymbed | 0:c4152c628df5 | 8 | * it under the terms of the GNU General Public License as published by |
sPymbed | 0:c4152c628df5 | 9 | * the Free Software Foundation; either version 3 of the License, or |
sPymbed | 0:c4152c628df5 | 10 | * (at your option) any later version. |
sPymbed | 0:c4152c628df5 | 11 | * |
sPymbed | 0:c4152c628df5 | 12 | * wolfSSH is distributed in the hope that it will be useful, |
sPymbed | 0:c4152c628df5 | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
sPymbed | 0:c4152c628df5 | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
sPymbed | 0:c4152c628df5 | 15 | * GNU General Public License for more details. |
sPymbed | 0:c4152c628df5 | 16 | * |
sPymbed | 0:c4152c628df5 | 17 | * You should have received a copy of the GNU General Public License |
sPymbed | 0:c4152c628df5 | 18 | * along with wolfSSH. If not, see <http://www.gnu.org/licenses/>. |
sPymbed | 0:c4152c628df5 | 19 | */ |
sPymbed | 0:c4152c628df5 | 20 | |
sPymbed | 0:c4152c628df5 | 21 | |
sPymbed | 0:c4152c628df5 | 22 | /* |
sPymbed | 0:c4152c628df5 | 23 | * The log module contains the interface to the logging function. When |
sPymbed | 0:c4152c628df5 | 24 | * debugging is enabled and turned on, the logger will output to STDOUT. |
sPymbed | 0:c4152c628df5 | 25 | * A custom logging callback may be installed. |
sPymbed | 0:c4152c628df5 | 26 | */ |
sPymbed | 0:c4152c628df5 | 27 | |
sPymbed | 0:c4152c628df5 | 28 | |
sPymbed | 0:c4152c628df5 | 29 | #ifdef HAVE_CONFIG_H |
sPymbed | 0:c4152c628df5 | 30 | #include <config.h> |
sPymbed | 0:c4152c628df5 | 31 | #endif |
sPymbed | 0:c4152c628df5 | 32 | |
sPymbed | 0:c4152c628df5 | 33 | #include <wolfssh/ssh.h> |
sPymbed | 0:c4152c628df5 | 34 | #include <wolfssh/log.h> |
sPymbed | 0:c4152c628df5 | 35 | #include <wolfssh/error.h> |
sPymbed | 0:c4152c628df5 | 36 | |
sPymbed | 0:c4152c628df5 | 37 | #include <stdlib.h> |
sPymbed | 0:c4152c628df5 | 38 | #include <stdio.h> |
sPymbed | 0:c4152c628df5 | 39 | #include <stdarg.h> |
sPymbed | 0:c4152c628df5 | 40 | #ifndef NO_TIMESTAMP |
sPymbed | 0:c4152c628df5 | 41 | //#include <time.h> |
sPymbed | 0:c4152c628df5 | 42 | #endif |
sPymbed | 0:c4152c628df5 | 43 | |
sPymbed | 0:c4152c628df5 | 44 | //#include "esp_log.h" |
sPymbed | 0:c4152c628df5 | 45 | |
sPymbed | 0:c4152c628df5 | 46 | |
sPymbed | 0:c4152c628df5 | 47 | #ifndef WOLFSSH_DEFAULT_LOG_WIDTH |
sPymbed | 0:c4152c628df5 | 48 | #define WOLFSSH_DEFAULT_LOG_WIDTH 120 |
sPymbed | 0:c4152c628df5 | 49 | #endif |
sPymbed | 0:c4152c628df5 | 50 | |
sPymbed | 0:c4152c628df5 | 51 | |
sPymbed | 0:c4152c628df5 | 52 | static const char *TAG = "WOLFSSH"; |
sPymbed | 0:c4152c628df5 | 53 | |
sPymbed | 0:c4152c628df5 | 54 | |
sPymbed | 0:c4152c628df5 | 55 | static void DefaultLoggingCb(enum wolfSSH_LogLevel, const char *const); |
sPymbed | 0:c4152c628df5 | 56 | |
sPymbed | 0:c4152c628df5 | 57 | static wolfSSH_LoggingCb logFunction = DefaultLoggingCb; |
sPymbed | 0:c4152c628df5 | 58 | static enum wolfSSH_LogLevel logLevel = WS_LOG_USER; // esp32: max |
sPymbed | 0:c4152c628df5 | 59 | #ifdef DEBUG_WOLFSSH |
sPymbed | 0:c4152c628df5 | 60 | static int logEnable = 0; |
sPymbed | 0:c4152c628df5 | 61 | #endif |
sPymbed | 0:c4152c628df5 | 62 | |
sPymbed | 0:c4152c628df5 | 63 | |
sPymbed | 0:c4152c628df5 | 64 | /* turn debugging on if supported */ |
sPymbed | 0:c4152c628df5 | 65 | void wolfSSH_Debugging_ON(void) |
sPymbed | 0:c4152c628df5 | 66 | { |
sPymbed | 0:c4152c628df5 | 67 | #ifdef DEBUG_WOLFSSH |
sPymbed | 0:c4152c628df5 | 68 | logEnable = 1; |
sPymbed | 0:c4152c628df5 | 69 | #endif |
sPymbed | 0:c4152c628df5 | 70 | } |
sPymbed | 0:c4152c628df5 | 71 | |
sPymbed | 0:c4152c628df5 | 72 | |
sPymbed | 0:c4152c628df5 | 73 | /* turn debugging off */ |
sPymbed | 0:c4152c628df5 | 74 | void wolfSSH_Debugging_OFF(void) |
sPymbed | 0:c4152c628df5 | 75 | { |
sPymbed | 0:c4152c628df5 | 76 | #ifdef DEBUG_WOLFSSH |
sPymbed | 0:c4152c628df5 | 77 | logEnable = 0; |
sPymbed | 0:c4152c628df5 | 78 | #endif |
sPymbed | 0:c4152c628df5 | 79 | } |
sPymbed | 0:c4152c628df5 | 80 | |
sPymbed | 0:c4152c628df5 | 81 | |
sPymbed | 0:c4152c628df5 | 82 | /* set logging callback function */ |
sPymbed | 0:c4152c628df5 | 83 | void wolfSSH_SetLoggingCb(wolfSSH_LoggingCb logF) |
sPymbed | 0:c4152c628df5 | 84 | { |
sPymbed | 0:c4152c628df5 | 85 | if (logF) |
sPymbed | 0:c4152c628df5 | 86 | logFunction = logF; |
sPymbed | 0:c4152c628df5 | 87 | } |
sPymbed | 0:c4152c628df5 | 88 | |
sPymbed | 0:c4152c628df5 | 89 | |
sPymbed | 0:c4152c628df5 | 90 | int wolfSSH_LogEnabled(void) |
sPymbed | 0:c4152c628df5 | 91 | { |
sPymbed | 0:c4152c628df5 | 92 | #ifdef DEBUG_WOLFSSH |
sPymbed | 0:c4152c628df5 | 93 | return logEnable; |
sPymbed | 0:c4152c628df5 | 94 | #else |
sPymbed | 0:c4152c628df5 | 95 | return 0; |
sPymbed | 0:c4152c628df5 | 96 | #endif |
sPymbed | 0:c4152c628df5 | 97 | } |
sPymbed | 0:c4152c628df5 | 98 | |
sPymbed | 0:c4152c628df5 | 99 | |
sPymbed | 0:c4152c628df5 | 100 | /* log level string */ |
sPymbed | 0:c4152c628df5 | 101 | static const char* GetLogStr(enum wolfSSH_LogLevel level) |
sPymbed | 0:c4152c628df5 | 102 | { |
sPymbed | 0:c4152c628df5 | 103 | switch (level) { |
sPymbed | 0:c4152c628df5 | 104 | case WS_LOG_INFO: |
sPymbed | 0:c4152c628df5 | 105 | return "INFO"; |
sPymbed | 0:c4152c628df5 | 106 | |
sPymbed | 0:c4152c628df5 | 107 | case WS_LOG_WARN: |
sPymbed | 0:c4152c628df5 | 108 | return "WARNING"; |
sPymbed | 0:c4152c628df5 | 109 | |
sPymbed | 0:c4152c628df5 | 110 | case WS_LOG_ERROR: |
sPymbed | 0:c4152c628df5 | 111 | return "ERROR"; |
sPymbed | 0:c4152c628df5 | 112 | |
sPymbed | 0:c4152c628df5 | 113 | case WS_LOG_DEBUG: |
sPymbed | 0:c4152c628df5 | 114 | return "DEBUG"; |
sPymbed | 0:c4152c628df5 | 115 | |
sPymbed | 0:c4152c628df5 | 116 | case WS_LOG_USER: |
sPymbed | 0:c4152c628df5 | 117 | return "USER"; |
sPymbed | 0:c4152c628df5 | 118 | |
sPymbed | 0:c4152c628df5 | 119 | default: |
sPymbed | 0:c4152c628df5 | 120 | return "UNKNOWN"; |
sPymbed | 0:c4152c628df5 | 121 | } |
sPymbed | 0:c4152c628df5 | 122 | } |
sPymbed | 0:c4152c628df5 | 123 | |
sPymbed | 0:c4152c628df5 | 124 | |
sPymbed | 0:c4152c628df5 | 125 | void DefaultLoggingCb(enum wolfSSH_LogLevel level, const char *const msgStr) |
sPymbed | 0:c4152c628df5 | 126 | { |
sPymbed | 0:c4152c628df5 | 127 | char timeStr[80]; |
sPymbed | 0:c4152c628df5 | 128 | timeStr[0] = '\0'; |
sPymbed | 0:c4152c628df5 | 129 | #ifndef NO_TIMESTAMP |
sPymbed | 0:c4152c628df5 | 130 | { |
sPymbed | 0:c4152c628df5 | 131 | time_t current; |
sPymbed | 0:c4152c628df5 | 132 | struct tm local; |
sPymbed | 0:c4152c628df5 | 133 | |
sPymbed | 0:c4152c628df5 | 134 | current = time(NULL); |
sPymbed | 0:c4152c628df5 | 135 | if (WLOCALTIME(¤t, &local)) { |
sPymbed | 0:c4152c628df5 | 136 | /* make pretty */ |
sPymbed | 0:c4152c628df5 | 137 | strftime(timeStr, sizeof(timeStr), "%b %d %T %Y: ", &local); |
sPymbed | 0:c4152c628df5 | 138 | } |
sPymbed | 0:c4152c628df5 | 139 | } |
sPymbed | 0:c4152c628df5 | 140 | #endif /* NO_TIMESTAMP */ |
sPymbed | 0:c4152c628df5 | 141 | //fprintf(stdout, "%s[%s] %s\n", timeStr, GetLogStr(level), msgStr); |
sPymbed | 0:c4152c628df5 | 142 | //ESP_LOGI(TAG, "%s[%s] %s\n", timeStr, GetLogStr(level), msgStr); |
sPymbed | 0:c4152c628df5 | 143 | } |
sPymbed | 0:c4152c628df5 | 144 | |
sPymbed | 0:c4152c628df5 | 145 | |
sPymbed | 0:c4152c628df5 | 146 | /* our default logger */ |
sPymbed | 0:c4152c628df5 | 147 | void wolfSSH_Log(enum wolfSSH_LogLevel level, const char *const fmt, ...) |
sPymbed | 0:c4152c628df5 | 148 | { |
sPymbed | 0:c4152c628df5 | 149 | va_list vlist; |
sPymbed | 0:c4152c628df5 | 150 | char msgStr[WOLFSSH_DEFAULT_LOG_WIDTH]; |
sPymbed | 0:c4152c628df5 | 151 | |
sPymbed | 0:c4152c628df5 | 152 | if (level < logLevel) |
sPymbed | 0:c4152c628df5 | 153 | return; /* don't need to output */ |
sPymbed | 0:c4152c628df5 | 154 | |
sPymbed | 0:c4152c628df5 | 155 | /* format msg */ |
sPymbed | 0:c4152c628df5 | 156 | va_start(vlist, fmt); |
sPymbed | 0:c4152c628df5 | 157 | WVSNPRINTF(msgStr, sizeof(msgStr), fmt, vlist); |
sPymbed | 0:c4152c628df5 | 158 | va_end(vlist); |
sPymbed | 0:c4152c628df5 | 159 | |
sPymbed | 0:c4152c628df5 | 160 | if (logFunction) |
sPymbed | 0:c4152c628df5 | 161 | logFunction(level, msgStr); |
sPymbed | 0:c4152c628df5 | 162 | } |