Initial version of donated sources by Avertec, 3.4p5.
[tas-yagle.git] / distrib / sources / avt / avt_trace.c
1 /****************************************************************************/
2 /* */
3 /* Chaine de verification */
4 /* */
5 /* Produit : AVT Version 1 */
6 /* Fichier : avt_util.c */
7 /* */
8 /* (c) copyright 1998-1999 AVERTEC */
9 /* Tous droits reserves */
10 /* Support : e-mail support@avertec.com */
11 /* */
12 /* Auteur(s) : AUGUSTINS Gilles */
13 /* */
14 /****************************************************************************/
15
16 #include "avt_headers.h"
17
18 #define BG_NONE ""
19 #define FG_NONE ""
20
21 #define CL_RESET "" // Reset All Attributes (return to normal mode)
22 #define CL_BOLD "1" // Bright (Usually turns on BOLD)
23 #define CL_DIM "2" // Dim
24 #define CL_UDL "3" // Underline
25 #define CL_BLINK "5" // Blink
26 #define CL_REV "7" // Reverse
27 // 8 Hidden
28
29 #define FG_BLACK "30" // Black
30 #define FG_RED "31" // Red
31 #define FG_GREEN "32" // Green
32 #define FG_YELLOW "33" // Yellow
33 #define FG_BLUE "34" // Blue
34 #define FG_MAGENTA "35" // Magenta
35 #define FG_CYAN "36" // Cyan
36 #define FG_WHITE "37" // White
37
38 #define BG_BLACK "40" // Black
39 #define BG_RED "41" // Red
40 #define BG_GREEN "42" // Green
41 #define BG_YELLOW "43" // Yellow
42 #define BG_BLUE "44" // Blue
43 #define BG_MAGENTA "45" // Magenta
44 #define BG_CYAN "46" // Cyan
45 #define BG_WHITE "47" // White
46
47
48 #define COLOR(a) "\x1B[" a "m"
49
50 char *colors[]=
51 {
52 COLOR(FG_BLACK),
53 COLOR(FG_MAGENTA),
54 COLOR(FG_BLUE),
55 COLOR(FG_CYAN),
56 COLOR(FG_YELLOW),
57 COLOR(FG_WHITE),
58 COLOR(FG_RED),
59 COLOR(FG_GREEN)
60 };
61
62 #define NBCOLORS ((signed)(sizeof(colors)/sizeof(*colors)))
63
64 char *leveltab[]=
65 {
66 "",
67 " ",
68 " ",
69 " ",
70 " ",
71 " ",
72 " "
73 };
74
75 void readusercolor()
76 {
77 unsigned int i;
78 char temp[100], *e;
79 for (i=0;(unsigned)i<sizeof(colors)/sizeof(*colors);i++)
80 {
81 sprintf(temp,"AVT_COL_%d",i);
82 e=avt_gethashvar(temp);
83 if (e!=NULL)
84 {
85 strcpy(temp,"\x1B[");
86 switch(*e)
87 {
88 case '+': strcat(temp,CL_BOLD); strcat(temp,";"); e++; break;
89 case '-': strcat(temp,CL_DIM); strcat(temp,";"); e++; break;
90 default: break;
91 }
92 switch(*e)
93 {
94 case 'B': strcat(temp,FG_BLACK); break;
95 case 'r': strcat(temp,FG_RED); break;
96 case 'g': strcat(temp,FG_GREEN); break;
97 case 'y': strcat(temp,FG_YELLOW); break;
98 case 'b': strcat(temp,FG_BLUE); break;
99 case 'm': strcat(temp,FG_MAGENTA); break;
100 case 'c': strcat(temp,FG_CYAN); break;
101 case 'w': strcat(temp,FG_WHITE); break;
102 default: break;
103 }
104 strcat(temp,"m");
105 colors[i]=strdup(temp);
106 }
107 }
108
109 }
110
111 void avt_error (char *lib, int code, int severity, char *fmt, ...)
112 {
113 va_list pa;
114 char *typemsg;
115 char buf[8192], error_msg[8192];
116 int car;
117
118 va_start (pa, fmt);
119 vsprintf(buf, fmt, pa);
120 va_end(pa);
121
122 switch(severity)
123 {
124 case AVT_ERR: typemsg="¤6¤+Error"; break;
125 case AVT_WAR: typemsg="¤4Warning"; break;
126 case AVT_INFO:
127 default:
128 typemsg="¤1Info";
129 }
130 if (code<=0)
131 sprintf (error_msg, "[%s¤.][¤+%s¤.] ", typemsg, lib);
132 else
133 sprintf (error_msg, "[%s #%d¤.][¤+%s¤.] ", typemsg, code, lib);
134
135 car = strlen (error_msg);
136
137 strcpy(&error_msg[car], buf);
138 avt_fprintf (stderr, "%s", error_msg);
139 }
140
141 void avt_trace (int level, FILE *output, char *fmt, ...)
142 {
143 va_list pa;
144 int color = 0;
145 static int avt_color = 0;
146 static int color_env = 1;
147 char *str = NULL;
148
149 if (color_env) {
150 if ((str = avt_gethashvar ("AVT_COL")))
151 if (!strcmp (str, "yes")) {
152 avt_color = 1;
153 readusercolor();
154 }
155 color_env = 0;
156 }
157
158 if (isatty (fileno(output)))
159 if (avt_color)
160 color = 1;
161
162 if (color) {
163 if (level>=0 && (unsigned)level<sizeof(colors)/sizeof(*colors))
164 fprintf (output, "%s%s",colors[level],leveltab[level]);
165 else
166 fprintf (output, "%s",leveltab[0]);
167 }
168 else
169 fprintf (output, "%s",level>=0?leveltab[level]:"");
170
171 va_start (pa, fmt);
172 vfprintf(output, fmt, pa);
173 va_end(pa);
174
175 if (color)
176 fputs(COLOR(CL_RESET), output);
177 }
178
179 void avt_trace_va (int level, FILE *output, char *fmt, va_list pa)
180 {
181 int color = 0;
182 static int avt_color = 0;
183 static int color_env = 1;
184 char *str = NULL;
185
186 if (color_env) {
187 if ((str = avt_gethashvar ("AVT_COL")))
188 if (!strcmp (str, "yes"))
189 {
190 avt_color = 1;
191 readusercolor();
192 }
193 color_env = 0;
194 }
195
196 if (isatty (fileno(output)))
197 if (avt_color)
198 color = 1;
199
200 if (color)
201 {
202 if (level>=0 && (unsigned)level<sizeof(colors)/sizeof(*colors))
203 fprintf (output, "%s%s",colors[level],leveltab[level]);
204 else
205 fprintf (output, "%s",leveltab[0]);
206 }
207 else
208 fprintf (output, "%s",level>=0?leveltab[level]:"");
209
210 vfprintf(output, fmt, pa);
211
212 if (color)
213 fputs(COLOR(CL_RESET), output);
214 }
215
216 int avt_terminal(FILE *output)
217 {
218 if (isatty (fileno(output)))
219 return 1;
220 return 0;
221 }
222
223 void avt_back_fprintf(FILE *output, int length)
224 {
225 if (avt_terminal(output))
226 {
227 int i;
228 for (i=1; i<=length;i++)
229 fprintf (output, "\b");
230 }
231 }
232
233 void avt_fprintf(FILE *output, char *fmt, ...)
234 {
235 va_list pa;
236 int color = 0, ln;
237 static int avt_color = 0;
238 static int color_env = 1;
239 char *str = NULL, *e;
240 char temp[4096];
241
242 if (color_env) {
243 if (V_BOOL_TAB[__AVT_COLOR].VALUE)
244 {
245 avt_color = 1;
246 readusercolor();
247 }
248 color_env = 0;
249 }
250
251 if (avt_terminal(output))
252 if (avt_color)
253 color = 1;
254
255 va_start (pa, fmt);
256 vsprintf(temp, fmt, pa);
257 va_end(pa);
258
259 str=temp;
260 e=strchr(str,'¤');
261 while (e!=NULL)
262 {
263 if (*(e+1)>='0' && *(e+1)<='0'+NBCOLORS-1)
264 {
265 *e='\0';
266 fputs(str, output);
267 if (color) fputs(colors[*(e+1)-'0'], output);
268 str=e+2;
269 }
270 else
271 switch(*(e+1))
272 {
273 case '+':
274 *e='\0';
275 fputs(str, output);
276 if (color) fputs(COLOR(CL_BOLD), output);
277 str=e+2;
278 break;
279 case '-':
280 *e='\0';
281 fputs(str, output);
282 if (color) fputs(COLOR(CL_DIM), output);
283 str=e+2;
284 break;
285 case '.':
286 *e='\0';
287 fputs(str, output);
288 if (color) fputs(COLOR(CL_RESET), output);
289 str=e+2;
290 break;
291 case '_':
292 *e='\0';
293 fputs(str, output);
294 if (color) fputs(COLOR(CL_UDL), output);
295 str=e+2;
296 break;
297 case '~':
298 *e='\0';
299 fputs(str, output);
300 if (color) fputs(COLOR(CL_REV), output);
301 str=e+2;
302 break;
303 default:
304 *e='\0';
305 fprintf(output,"%s¤",str);
306 str=e+1;
307 }
308 e=strchr(str,'¤');
309 }
310
311 fputs (str, output);
312
313 if (color && (ln=strlen(str))>0 && str[ln-1]=='\n')
314 fputs(COLOR(CL_RESET), output);
315 }
316
317 int avt_text_real_length(char *buf)
318 {
319 int cnt=0;
320 while (*buf!='\0')
321 {
322 if (*buf=='¤' && *(buf+1)!='\0') cnt-=1;
323 else cnt++;
324 buf++;
325 }
326 return cnt;
327 }
328
329 void avt_format_text(char *resbuf, char *origbuf, int decal, int max)
330 {
331 int i=0, j=0, k, cnt=decal;
332
333 while (origbuf[i]!='\0')
334 {
335 resbuf[j++]=origbuf[i];
336 if (origbuf[i]=='¤' && origbuf[i+1]!='\0') cnt-=1;
337 else cnt++;
338 i++;
339 if (origbuf[i]=='\n' || cnt>=max)
340 {
341 resbuf[j++]='\n';
342 if (origbuf[i]=='\n') i++;
343 if (origbuf[i]!='\0') for (k=0; k<decal; k++) resbuf[j++]=' ';
344 cnt=decal;
345 }
346 }
347 resbuf[j++]='\0';
348 }