1 | /* logging.c
|
---|
2 | *
|
---|
3 | * Copyright (C) 2006-2015 wolfSSL Inc.
|
---|
4 | *
|
---|
5 | * This file is part of wolfSSL. (formerly known as CyaSSL)
|
---|
6 | *
|
---|
7 | * wolfSSL is free software; you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation; either version 2 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * wolfSSL is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with this program; if not, write to the Free Software
|
---|
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
---|
20 | */
|
---|
21 |
|
---|
22 | #ifdef HAVE_CONFIG_H
|
---|
23 | #include <config.h>
|
---|
24 | #endif
|
---|
25 |
|
---|
26 | #include <wolfssl/wolfcrypt/settings.h>
|
---|
27 |
|
---|
28 | /* submitted by eof */
|
---|
29 |
|
---|
30 | #include <wolfssl/wolfcrypt/logging.h>
|
---|
31 | #include <wolfssl/wolfcrypt/error-crypt.h>
|
---|
32 |
|
---|
33 |
|
---|
34 | #ifdef __cplusplus
|
---|
35 | extern "C" {
|
---|
36 | #endif
|
---|
37 | WOLFSSL_API int wolfSSL_Debugging_ON(void);
|
---|
38 | WOLFSSL_API void wolfSSL_Debugging_OFF(void);
|
---|
39 | #ifdef __cplusplus
|
---|
40 | }
|
---|
41 | #endif
|
---|
42 |
|
---|
43 |
|
---|
44 | #ifdef DEBUG_WOLFSSL
|
---|
45 |
|
---|
46 | /* Set these to default values initially. */
|
---|
47 | static wolfSSL_Logging_cb log_function = 0;
|
---|
48 | static int loggingEnabled = 0;
|
---|
49 |
|
---|
50 | #endif /* DEBUG_WOLFSSL */
|
---|
51 |
|
---|
52 |
|
---|
53 | int wolfSSL_SetLoggingCb(wolfSSL_Logging_cb f)
|
---|
54 | {
|
---|
55 | #ifdef DEBUG_WOLFSSL
|
---|
56 | int res = 0;
|
---|
57 |
|
---|
58 | if (f)
|
---|
59 | log_function = f;
|
---|
60 | else
|
---|
61 | res = BAD_FUNC_ARG;
|
---|
62 |
|
---|
63 | return res;
|
---|
64 | #else
|
---|
65 | (void)f;
|
---|
66 | return NOT_COMPILED_IN;
|
---|
67 | #endif
|
---|
68 | }
|
---|
69 |
|
---|
70 |
|
---|
71 | int wolfSSL_Debugging_ON(void)
|
---|
72 | {
|
---|
73 | #ifdef DEBUG_WOLFSSL
|
---|
74 | loggingEnabled = 1;
|
---|
75 | return 0;
|
---|
76 | #else
|
---|
77 | return NOT_COMPILED_IN;
|
---|
78 | #endif
|
---|
79 | }
|
---|
80 |
|
---|
81 |
|
---|
82 | void wolfSSL_Debugging_OFF(void)
|
---|
83 | {
|
---|
84 | #ifdef DEBUG_WOLFSSL
|
---|
85 | loggingEnabled = 0;
|
---|
86 | #endif
|
---|
87 | }
|
---|
88 |
|
---|
89 |
|
---|
90 | #ifdef DEBUG_WOLFSSL
|
---|
91 |
|
---|
92 | #ifdef FREESCALE_MQX
|
---|
93 | #if MQX_USE_IO_OLD
|
---|
94 | #include <fio.h>
|
---|
95 | #else
|
---|
96 | #include <nio.h>
|
---|
97 | #endif
|
---|
98 | #else
|
---|
99 | #include <stdio.h> /* for default printf stuff */
|
---|
100 | #endif
|
---|
101 |
|
---|
102 | #ifdef THREADX
|
---|
103 | int dc_log_printf(char*, ...);
|
---|
104 | #endif
|
---|
105 |
|
---|
106 | static void wolfssl_log(const int logLevel, const char *const logMessage)
|
---|
107 | {
|
---|
108 | if (log_function)
|
---|
109 | log_function(logLevel, logMessage);
|
---|
110 | else {
|
---|
111 | if (loggingEnabled) {
|
---|
112 | #ifdef THREADX
|
---|
113 | dc_log_printf("%s\n", logMessage);
|
---|
114 | #elif defined(MICRIUM)
|
---|
115 | #if (NET_SECURE_MGR_CFG_EN == DEF_ENABLED)
|
---|
116 | NetSecure_TraceOut((CPU_CHAR *)logMessage);
|
---|
117 | #endif
|
---|
118 | #elif defined(WOLFSSL_MDK_ARM)
|
---|
119 | fflush(stdout) ;
|
---|
120 | printf("%s\n", logMessage);
|
---|
121 | fflush(stdout) ;
|
---|
122 | #else
|
---|
123 | fprintf(stderr, "%s\n", logMessage);
|
---|
124 | #endif
|
---|
125 | }
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 |
|
---|
130 | void WOLFSSL_MSG(const char* msg)
|
---|
131 | {
|
---|
132 | if (loggingEnabled)
|
---|
133 | wolfssl_log(INFO_LOG , msg);
|
---|
134 | }
|
---|
135 |
|
---|
136 |
|
---|
137 | void WOLFSSL_ENTER(const char* msg)
|
---|
138 | {
|
---|
139 | if (loggingEnabled) {
|
---|
140 | char buffer[80];
|
---|
141 | sprintf(buffer, "wolfSSL Entering %s", msg);
|
---|
142 | wolfssl_log(ENTER_LOG , buffer);
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 |
|
---|
147 | void WOLFSSL_LEAVE(const char* msg, int ret)
|
---|
148 | {
|
---|
149 | if (loggingEnabled) {
|
---|
150 | char buffer[80];
|
---|
151 | sprintf(buffer, "wolfSSL Leaving %s, return %d", msg, ret);
|
---|
152 | wolfssl_log(LEAVE_LOG , buffer);
|
---|
153 | }
|
---|
154 | }
|
---|
155 |
|
---|
156 |
|
---|
157 | void WOLFSSL_ERROR(int error)
|
---|
158 | {
|
---|
159 | if (loggingEnabled) {
|
---|
160 | char buffer[80];
|
---|
161 | sprintf(buffer, "wolfSSL error occured, error = %d", error);
|
---|
162 | wolfssl_log(ERROR_LOG , buffer);
|
---|
163 | }
|
---|
164 | }
|
---|
165 |
|
---|
166 | #endif /* DEBUG_WOLFSSL */
|
---|