summaryrefslogtreecommitdiffstats
path: root/clib/src/trace_indent.c
blob: c1dd2808cad0c70fb5737ae872d2393ffadc585a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
 * Copyright (c) International Business Machines Corp., 2011
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
 * the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * Author: B. Rafanello
 *
 * Module: trace_indent.c
 *
 * Implements indentation for the trace output from bb_trace
 *
 * Version 0.0.0.1
 */

#include "trace_indent.h"
#include "stdio.h"

/*--------------------------------------------------
 * Private Constants
 --------------------------------------------------*/

#define DEFAULT_INDENT_SIZE   5
#define MAX_INDENT_SIZE       129
#define DEFAULT_INDENT        "     "

/*--------------------------------------------------
 * Global variables.
 --------------------------------------------------*/

/* The current trace level. */
unsigned char BB_TRACE_LEVEL = 0;
unsigned char BB_PREVIOUS_TRACE_LEVEL = 0;

/*--------------------------------------------------
 * Private global variables.
 --------------------------------------------------*/

static unsigned int Current_Indent_Level = 0;
static unsigned int Current_Indent_Size = DEFAULT_INDENT_SIZE;
static char Indent[MAX_INDENT_SIZE] = DEFAULT_INDENT;

/*--------------------------------------------------
 * Public Functions
 --------------------------------------------------*/

/*********************************************************************/
/*                                                                   */
/*   Function Name: Set_Indent_Size                                  */
/*                                                                   */
/*   Descriptive Name: Sets the number of spaces to use per indent.  */
/*                     If the current indent level is 5 and this     */
/*                     function is called to set the indent size to  */
/*                     10, then 50 spaces will be printed by the     */
/*                     trace macros before each line of trace        */
/*                     output at this indent level.                  */
/*                                                                   */
/*   Input: unsigned int Spaces_Per_Indent - A value less than 128.  */
/*                                                                   */
/*   Output: If Success : The specified value will be used for the   */
/*                        size of an indent.                         */
/*                                                                   */
/*           If Failure : This should only happen if the value       */
/*                        specified is >= 128, in which case it is   */
/*                        ignored and the previous value is retained.*/
/*                                                                   */
/*   Error Handling: Bad values for Spaces_Per_Indent are ignored.   */
/*                                                                   */
/*   Side Effects: The number of spaces per indent may be changed.   */
/*                                                                   */
/*   Notes:                                                          */
/*                                                                   */
/*********************************************************************/
void Set_Indent_Size(unsigned int Spaces_Per_Indent)
{
	unsigned int i;

	if (Spaces_Per_Indent <= MAX_INDENT_SIZE) {
		Current_Indent_Size = Spaces_Per_Indent;
		for (i = 0; i < Current_Indent_Size; i++) {
			Indent[i] = ' ';
		}
		Indent[i] = 0x00;
	}

	return;
}

/*********************************************************************/
/*                                                                   */
/*   Function Name: Indent_Trace_Output                              */
/*                                                                   */
/*   Descriptive Name: This function increases the current indent    */
/*                     level by one.                                 */
/*                                                                   */
/*   Input: None.                                                    */
/*                                                                   */
/*   Output: If Success : None                                       */
/*                                                                   */
/*           If Failure : None                                       */
/*                                                                   */
/*   Error Handling: None                                            */
/*                                                                   */
/*   Side Effects: The current indent level for trace output will    */
/*                 be increased by 1.                                */
/*                                                                   */
/*   Notes:                                                          */
/*                                                                   */
/*********************************************************************/
void Indent_Trace_Output(void)
{
	Current_Indent_Level += 1;
	return;
}

/*********************************************************************/
/*                                                                   */
/*   Function Name: Outdent_Trace_Output                             */
/*                                                                   */
/*   Descriptive Name: This function reduces the current indent      */
/*                     level if it is greater than 0.                */
/*                                                                   */
/*   Input: None                                                     */
/*                                                                   */
/*   Output: If Success : None                                       */
/*                                                                   */
/*           If Failure : None                                       */
/*                                                                   */
/*   Error Handling: If the current indent level is zero, then this  */
/*                   function does nothing.                          */
/*                                                                   */
/*   Side Effects: The current indent level for trace output may be  */
/*                 decreased by 1.                                   */
/*                                                                   */
/*   Notes:                                                          */
/*                                                                   */
/*********************************************************************/
void Outdent_Trace_Output(void)
{
	if (Current_Indent_Level > 0)
		Current_Indent_Level -= 1;
	return;
}

/*********************************************************************/
/*                                                                   */
/*   Function Name: Do_Indent                                        */
/*                                                                   */
/*   Descriptive Name: This function prints to stderr the number of  */
/*                     spaces corresponding to the current indent    */
/*                     level.                                        */
/*                                                                   */
/*   Input: None                                                     */
/*                                                                   */
/*   Output: If Success : None                                       */
/*                                                                   */
/*           If Failure : None                                       */
/*                                                                   */
/*   Error Handling: None                                            */
/*                                                                   */
/*   Side Effects: Some number of space may be output to stderr.     */
/*                                                                   */
/*   Notes:                                                          */
/*                                                                   */
/*********************************************************************/
void Do_Indent(void)
{
	unsigned int i;

	for (i = 0; i < Current_Indent_Level; i++) {
		fprintf(stderr, "%s", Indent);
	}

}
OpenPOWER on IntegriCloud