Renesas / SecureDweet
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers logging.c Source File

logging.c

00001 /* logging.c
00002  *
00003  * Copyright (C) 2006-2016 wolfSSL Inc.
00004  *
00005  * This file is part of wolfSSL.
00006  *
00007  * wolfSSL is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 2 of the License, or
00010  * (at your option) any later version.
00011  *
00012  * wolfSSL is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
00020  */
00021 
00022 
00023 #ifdef HAVE_CONFIG_H
00024     #include <config.h>
00025 #endif
00026 
00027 #include <wolfssl/wolfcrypt/settings.h>
00028 
00029 /* submitted by eof */
00030 
00031 #include <wolfssl/wolfcrypt/logging.h>
00032 #include <wolfssl/wolfcrypt/error-crypt.h>
00033 
00034 
00035 #ifdef __cplusplus
00036     extern "C" {
00037 #endif
00038     WOLFSSL_API int  wolfSSL_Debugging_ON(void);
00039     WOLFSSL_API void wolfSSL_Debugging_OFF(void);
00040 #ifdef __cplusplus
00041     } 
00042 #endif
00043 
00044 
00045 #ifdef DEBUG_WOLFSSL
00046 
00047 /* Set these to default values initially. */
00048 static wolfSSL_Logging_cb log_function = 0;
00049 static int loggingEnabled = 0;
00050 
00051 #endif /* DEBUG_WOLFSSL */
00052 
00053 
00054 int wolfSSL_SetLoggingCb(wolfSSL_Logging_cb f)
00055 {
00056 #ifdef DEBUG_WOLFSSL
00057     int res = 0;
00058 
00059     if (f)
00060         log_function = f;
00061     else
00062         res = BAD_FUNC_ARG;
00063 
00064     return res;
00065 #else
00066     (void)f;
00067     return NOT_COMPILED_IN;
00068 #endif
00069 }
00070 
00071 
00072 int wolfSSL_Debugging_ON(void)
00073 {
00074 #ifdef DEBUG_WOLFSSL
00075     loggingEnabled = 1;
00076     return 0;
00077 #else
00078     return NOT_COMPILED_IN;
00079 #endif
00080 }
00081 
00082 
00083 void wolfSSL_Debugging_OFF(void)
00084 {
00085 #ifdef DEBUG_WOLFSSL
00086     loggingEnabled = 0;
00087 #endif
00088 }
00089 
00090 
00091 #ifdef DEBUG_WOLFSSL
00092 
00093 #if defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
00094     #if MQX_USE_IO_OLD
00095         #include <fio.h>
00096     #else
00097         #include <nio.h>
00098     #endif
00099 #else
00100     #include <stdio.h>   /* for default printf stuff */
00101 #endif
00102 
00103 #ifdef THREADX
00104     int dc_log_printf(char*, ...);
00105 #endif
00106 
00107 static void wolfssl_log(const int logLevel, const char *const logMessage)
00108 {
00109     if (log_function)
00110         log_function(logLevel, logMessage);
00111     else {
00112         if (loggingEnabled) {
00113 #ifdef THREADX
00114             dc_log_printf("%s\n", logMessage);
00115 #elif defined(MICRIUM)
00116         #if (NET_SECURE_MGR_CFG_EN == DEF_ENABLED)
00117             NetSecure_TraceOut((CPU_CHAR *)logMessage);
00118         #endif
00119 #elif defined(WOLFSSL_MDK_ARM)
00120             fflush(stdout) ;
00121             printf("%s\n", logMessage);
00122             fflush(stdout) ;
00123 #elif defined(WOLFSSL_LOG_PRINTF)
00124             printf("%s\n", logMessage);
00125 #else
00126             fprintf(stderr, "%s\n", logMessage);
00127 #endif
00128         }
00129     }
00130 }
00131 
00132 
00133 void WOLFSSL_MSG(const char* msg)
00134 {
00135     if (loggingEnabled)
00136         wolfssl_log(INFO_LOG , msg);
00137 }
00138 
00139 
00140 void WOLFSSL_BUFFER(byte* buffer, word32 length)
00141 {
00142     #define LINE_LEN 16
00143 
00144     if (loggingEnabled) {
00145         word32 i;
00146         char line[80];
00147 
00148         if (!buffer) {
00149             wolfssl_log(INFO_LOG, "\tNULL");
00150 
00151             return;
00152         }
00153 
00154         sprintf(line, "\t");
00155 
00156         for (i = 0; i < LINE_LEN; i++) {
00157             if (i < length)
00158                 sprintf(line + 1 + i * 3,"%02x ", buffer[i]);
00159             else
00160                 sprintf(line + 1 + i * 3, "   ");
00161         }
00162 
00163         sprintf(line + 1 + LINE_LEN * 3, "| ");
00164 
00165         for (i = 0; i < LINE_LEN; i++)
00166             if (i < length)
00167                 sprintf(line + 3 + LINE_LEN * 3 + i,
00168                      "%c", 31 < buffer[i] && buffer[i] < 127 ? buffer[i] : '.');
00169 
00170         wolfssl_log(INFO_LOG, line);
00171 
00172         if (length > LINE_LEN)
00173             WOLFSSL_BUFFER(buffer + LINE_LEN, length - LINE_LEN);
00174     }
00175 }
00176 
00177 
00178 void WOLFSSL_ENTER(const char* msg)
00179 {
00180     if (loggingEnabled) {
00181         char buffer[80];
00182         sprintf(buffer, "wolfSSL Entering %s", msg);
00183         wolfssl_log(ENTER_LOG , buffer);
00184     }
00185 }
00186 
00187 
00188 void WOLFSSL_LEAVE(const char* msg, int ret)
00189 {
00190     if (loggingEnabled) {
00191         char buffer[80];
00192         sprintf(buffer, "wolfSSL Leaving %s, return %d", msg, ret);
00193         wolfssl_log(LEAVE_LOG , buffer);
00194     }
00195 }
00196 
00197 
00198 void WOLFSSL_ERROR(int error)
00199 {
00200     if (loggingEnabled) {
00201         char buffer[80];
00202         sprintf(buffer, "wolfSSL error occurred, error = %d", error);
00203         wolfssl_log(ERROR_LOG , buffer);
00204     }
00205 }
00206 
00207 #endif  /* DEBUG_WOLFSSL */
00208