1 /*
2 *   Copyright (c) 2016, Szymon Tomasz Stefanek
3 *
4 *   This source code is released for free distribution under the terms of the
5 *   GNU General Public License version 2 or (at your option) any later version.
6 *
7 *   Tracing facility.
8 */
9 
10 #include "general.h"
11 #include "trace.h"
12 
13 #ifdef DO_TRACING
14 
15 #include "options.h"
16 #include "read.h"
17 
18 #include <stdio.h>
19 #include <stdarg.h>
20 
traceEnter(const char * szFunction,const char * szFormat,...)21 void traceEnter(const char * szFunction,const char * szFormat,...)
22 {
23 	if (!isTraced())
24 		return;
25 
26 	debugIndent ();
27 
28 	fprintf(stderr,"[>> %s][at %lu] ",szFunction,getInputLineNumber());
29 
30 	va_list va;
31 	va_start(va,szFormat);
32 	vfprintf(stderr,szFormat,va);
33 	va_end(va);
34 
35 	fprintf(stderr,"\n");
36 
37 	debugInc();
38 }
39 
traceLeave(const char * szFunction,const char * szFormat,...)40 void traceLeave(const char * szFunction,const char * szFormat,...)
41 {
42 	if (!isTraced())
43 		return;
44 
45 	debugDec();
46 	debugIndent ();
47 
48 	fprintf(stderr,"[<< %s][at %lu] ",szFunction,getInputLineNumber());
49 
50 	va_list va;
51 	va_start(va,szFormat);
52 	vfprintf(stderr,szFormat,va);
53 	va_end(va);
54 
55 	fprintf(stderr,"\n");
56 }
57 
tracePrintFmtVa(const char * szFormat,va_list va)58 static void tracePrintFmtVa(const char * szFormat, va_list va)
59 {
60 	if (!isTraced())
61 		return;
62 
63 	vfprintf(stderr,szFormat,va);
64 }
65 
tracePrint(const char * szFunction,const char * szFormat,...)66 void tracePrint(const char * szFunction, const char * szFormat,...)
67 {
68 	if (!isTraced())
69 		return;
70 
71 	tracePrintPrefix(szFunction);
72 
73 	va_list va;
74 	va_start(va,szFormat);
75 	tracePrintFmtVa (szFormat,va);
76 	va_end(va);
77 
78 	tracePrintNewline();
79 }
80 
tracePrintFmt(const char * szFormat,...)81 void tracePrintFmt(const char * szFormat,...)
82 {
83 	va_list va;
84 	va_start(va,szFormat);
85 	tracePrintFmtVa (szFormat,va);
86 	va_end(va);
87 }
88 
tracePrintPrefix(const char * szFunction)89 void tracePrintPrefix(const char * szFunction)
90 {
91 	if (!isTraced())
92 		return;
93 
94 	debugIndent();
95 
96 	fprintf(stderr,"[%s][at %lu] ",szFunction,getInputLineNumber());
97 }
98 
tracePrintNewline(void)99 void tracePrintNewline(void)
100 {
101 	if (!isTraced())
102 		return;
103 
104 	fprintf(stderr,"\n");
105 }
106 
107 static bool tracingMain;
108 
traceMain(void)109 void traceMain(void)
110 {
111 	verbose("Tracing main part\n");
112 	tracingMain = true;
113 }
114 
isMainTraced(void)115 bool isMainTraced(void)
116 {
117 	return tracingMain;
118 }
119 
120 #endif // DO_TRACING
121