blob: 61965dca10c9b0bd6bfa52d9806e44772fae18a3 [file] [log] [blame]
Hanno Beckerb9100162021-01-12 09:46:03 +00001/*
2 * Message Processing Stack, Trace module
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of Mbed TLS (https://tls.mbed.org)
20 */
21
22#include "common.h"
23
24#if defined(MBEDTLS_MPS_TRACE)
25
26#include "trace.h"
27#include <stdarg.h>
28
29static int trace_depth_ = 0;
30
31#define color_default "\x1B[0m"
32#define color_red "\x1B[1;31m"
33#define color_green "\x1B[1;32m"
34#define color_yellow "\x1B[1;33m"
35#define color_blue "\x1B[1;34m"
36#define color_magenta "\x1B[1;35m"
37#define color_cyan "\x1B[1;36m"
38#define color_white "\x1B[1;37m"
39
40static char const * colors[] =
41{
42 color_default,
43 color_green,
44 color_yellow,
45 color_magenta,
46 color_cyan,
47 color_blue,
48 color_white
49};
50
51#define MPS_TRACE_BUF_SIZE 100
52
53void trace_print_msg( int id, int line, const char *format, ... )
54{
55 int ret;
56 char str[MPS_TRACE_BUF_SIZE];
57 va_list argp;
58 va_start( argp, format );
59 ret = mbedtls_vsnprintf( str, MPS_TRACE_BUF_SIZE, format, argp );
60 va_end( argp );
61
62 if( ret >= 0 && ret < MPS_TRACE_BUF_SIZE )
63 {
64 str[ret] = '\0';
65 mbedtls_printf( "[%d|L%d]: %s\n", id, line, str );
66 }
67}
68
69int trace_get_depth()
70{
71 return trace_depth_;
72}
73void trace_dec_depth()
74{
75 trace_depth_--;
76}
77void trace_inc_depth()
78{
79 trace_depth_++;
80}
81
82void trace_color( int id )
83{
84 if( id > (int) ( sizeof( colors ) / sizeof( *colors ) ) )
85 return;
86 printf( "%s", colors[ id ] );
87}
88
89void trace_indent( int level, trace_type ty )
90{
91 if( level > 0 )
92 {
93 while( --level )
94 printf( "| " );
95
96 printf( "| " );
97 }
98
99 switch( ty )
100 {
101 case trace_comment:
102 mbedtls_printf( "@ " );
103 break;
104
105 case trace_call:
106 mbedtls_printf( "+--> " );
107 break;
108
109 case trace_error:
110 mbedtls_printf( "E " );
111 break;
112
113 case trace_return:
114 mbedtls_printf( "< " );
115 break;
116
117 default:
118 break;
119 }
120}
121
122#endif /* MBEDTLS_MPS_TRACE */