MHEG5  18.9.0
MHEG5 Documentation
mh5debug.c
Go to the documentation of this file.
1 /*******************************************************************************
2  * Copyright © 2014 The DTVKit Open Software Foundation Ltd (www.dtvkit.org)
3  * Copyright © 2004 Ocean Blue Software Ltd
4  * Copyright © 2000 Koninklijke Philips Electronics N.V
5  *
6  * This file is part of a DTVKit Software Component
7  * You are permitted to copy, modify or distribute this file subject to the terms
8  * of the DTVKit 1.0 Licence which can be found in licence.txt or at www.dtvkit.org
9  *
10  * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
11  * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES
12  * OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
13  *
14  * If you or your organisation is not a member of DTVKit then you have access
15  * to this source code outside of the terms of the licence agreement
16  * and you are expected to delete this and any associated files immediately.
17  * Further information on DTVKit, membership and terms can be found at www.dtvkit.org
18  *******************************************************************************/
26 /*---includes for this file--------------------------------------------------*/
27 #include "mh5debug.h"
28 #include "mh5actiontokens.h"
29 #include "mh5eventtokens.h"
30 #include "mh5memory.h"
31 #include "mh5object.h"
32 #include "mh5group.h"
33 
34 
35 #include <stdio.h> /* io-stuff */
36 #include <stdarg.h> /* va_start, va_end*/
37 
38 /*---constant definitions for this file--------------------------------------*/
39 
40 
41 
42 /*---local typedef structs for this file-------------------------------------*/
43 
44 /*---local (static) variable declarations for this file----------------------*/
45 
46 #ifdef MHEG5LOG
47 
48 #ifdef MHEG5LOGTOFILE
49 static FILE *fLOG = 0;
50 #endif
51 
52 static int level = 0;
53 
54 #endif /* #ifdef MHEG5LOG */
55 
56 /*---local function definitions----------------------------------------------*/
57 
58 /*---global function definitions---------------------------------------------*/
59 
60 #ifdef TRACING
61 void MHEG5stringTrace(MHEG5String s, char *out, int start, int max)
62 {
63  int i, j;
64  max -= 2;
65  j = strlen(out);
66  out[j++] = '\"';
67  for (i = start; i < s.len && j < max; i++)
68  {
69  if (s.data[i] == '=')
70  {
71  out[j++] = '=';
72  out[j++] = '=';
73  }
74  else if ((s.data[i] >= ' ') && (s.data[i] <= 127))
75  {
76  out[j++] = s.data[i];
77  }
78  else
79  {
80  out[j++] = '=';
81  out[j++] = (s.data[i] >> 4) < 10 ? (s.data[i] >> 4) + '0' : (s.data[i] >> 4) + 'A' - 10;
82  out[j++] = (s.data[i] & 0x0F) < 10 ? (s.data[i] & 0x0F) + '0' : (s.data[i] & 0x0F) + 'A' - 10;
83  }
84  }
85  out[j] = '\"';
86  out[j + 1] = '\0';
87 }
88 
89 #endif
90 
91 
92 /* Functions within this source file are only compiled if MHEG5LOG is defined */
93 
94 #ifdef MHEG5LOG
95 
96 
97 void MHEG5LogInit(int logLevel)
98 {
99 #ifdef MHEG5LOGTOFILE
100  if (fLOG == 0)
101  {
102  fLOG = fopen(MH5LOGFILE, "w");
103  }
104 #endif
105  level = logLevel;
106 }
107 
108 void MHEG5LogExit(void)
109 {
110 #ifdef MHEG5LOGTOFILE
111  if (fLOG)
112  {
113  fclose(fLOG);
114  fLOG = 0;
115  }
116 #endif
117 }
118 
119 void MHEG5LogPrintf(int logLevel, char *format, ...)
120 {
121  va_list ap;
122 
123  if (logLevel <= level)
124  {
125  va_start(ap, format);
126 #ifdef MHEG5LOGTOFILE
127  if (fLOG)
128  {
129  vfprintf(fLOG, format, ap);
130  fflush(fLOG);
131  }
132 #else
133  vprintf(format, ap);
134  fflush(stdout);
135 #endif
136  va_end(ap);
137  }
138 }
139 
140 void MHEG5LogPrintAction(int logLevel, MHEG5ElemActionType action)
141 {
142  if (logLevel <= level)
143  {
144 #ifdef MHEG5LOGTOFILE
145  if (fLOG)
146  {
147  fprintf(fLOG, "%s", MHEG5actionToString(action));
148  fflush(fLOG);
149  }
150 #else
151  printf("%s", MHEG5actionToString(action));
152  fflush(stdout);
153 #endif
154  }
155 }
156 
157 void MHEG5LogPrintEvent(int logLevel, MHEG5EventType event)
158 {
159  if (logLevel <= level)
160  {
161 #ifdef MHEG5LOGTOFILE
162  if (fLOG)
163  {
164  fprintf(fLOG, "%s", MHEG5eventToString(event));
165  fflush(fLOG);
166  }
167 #else
168  printf("%s", MHEG5eventToString(event));
169  fflush(stdout);
170 #endif
171  }
172 }
173 
174 void MHEG5LogPrintString(int logLevel, MHEG5String string)
175 {
176  if (logLevel <= level)
177  {
178 #ifdef MHEG5LOGTOFILE
179  if (fLOG)
180  {
181  fprintf(fLOG, "%.*s", string.len, string.data);
182  fflush(fLOG);
183  }
184 #else
185  printf("%.*s", string.len, string.data);
186  fflush(stdout);
187 #endif
188  }
189 }
190 
191 void MHEG5LogPrintObjectRef(int logLevel, MH5GroupPtr sourceGroup, MHEG5Int sourceID)
192 {
193  if (logLevel <= level)
194  {
195 #ifdef MHEG5LOGTOFILE
196  if (fLOG)
197  {
198  fprintf(fLOG, "( %.*s %d )", sourceGroup->groupName.len, sourceGroup->groupName.data, sourceID);
199  fflush(fLOG);
200  }
201 #else
202  printf("( %.*s %d )", sourceGroup->groupName.len, sourceGroup->groupName.data, sourceID);
203  fflush(stdout);
204 #endif
205  }
206 }
207 
208 void MHEG5LogPrintObjectPtr(int logLevel, MHEG5Root *target)
209 {
210  if ((logLevel <= level) && (target))
211  {
212  MHEG5LogPrintObjectRef(logLevel, target->grp, target->id);
213  }
214 }
215 
216 void MHEG5InfoPrintf(char *format, ...)
217 {
218  va_list ap;
219 
220  if (MHEG5CALLS <= level)
221  {
222  va_start(ap, format);
223 #ifdef MHEG5LOGTOFILE
224  if (fLOG)
225  {
226  vfprintf(fLOG, format, ap);
227  fflush(fLOG);
228  }
229 #else
230  vprintf(format, ap);
231  fflush(stdout);
232 #endif
233  va_end(ap);
234  }
235 }
236 
237 void MHEG5DebugPrintf(char *format, ...)
238 {
239  va_list ap;
240 
241  if (MHEG5MESSAGE <= level)
242  {
243  va_start(ap, format);
244 #ifdef MHEG5LOGTOFILE
245  if (fLOG)
246  {
247  vfprintf(fLOG, format, ap);
248  fflush(fLOG);
249  }
250 #else
251  vprintf(format, ap);
252  fflush(stdout);
253 #endif
254  va_end(ap);
255  }
256 }
257 
258 void MHEG5WarningPrintf(char *format, ...)
259 {
260  va_list ap;
261 
262  if (MHEG5WARNING <= level)
263  {
264  va_start(ap, format);
265 #ifdef MHEG5LOGTOFILE
266  if (fLOG)
267  {
268  vfprintf(fLOG, format, ap);
269  fflush(fLOG);
270  }
271 #else
272  vprintf(format, ap);
273  fflush(stdout);
274 #endif
275  va_end(ap);
276  }
277 }
278 
279 void MHEG5ErrorPrintf(char *format, ...)
280 {
281  va_list ap;
282 
283  if (MHEG5ERROR <= level)
284  {
285  va_start(ap, format);
286 #ifdef MHEG5LOGTOFILE
287  if (fLOG)
288  {
289  vfprintf(fLOG, format, ap);
290  fflush(fLOG);
291  }
292 #else
293  vprintf(format, ap);
294  fflush(stdout);
295 #endif
296  va_end(ap);
297  }
298 }
299 
300 #endif /* #ifdef MHEG5LOG */
Event to String conversion.
const char * data
Definition: mh5gate.c:56
Implementation of the Group class Description Defines the structure and behaviour of objects used as ...
MHEG5Short MHEG5ElemActionType
Definition: mh5action.h:184
MH5GroupPtr grp
Definition: mh5root.h:57
MHEG5EventType
Definition: mh5base.h:114
long MHEG5Int
Definition: mh5base.h:73
const char * MHEG5actionToString(MHEG5ElemActionType action)
const char * MHEG5eventToString(MHEG5EventType event)
MHEG5Byte * data
Definition: mh5base.h:85
int len
Definition: mh5gate.c:57
Implement functions to retrieve MHEG5objects by GroupID and ID.
Mheg5 logging and debug printing.
MHEG5String groupName
Definition: mh5group.h:53
redirection include
MHEG5Int id
Definition: mh5root.h:48
action to string conversion
MHEG5Int len
Definition: mh5base.h:84