Initialize Tizen 2.3
[framework/multimedia/gstreamer0.10.git] / mobile / libs / gst / check / libcheck / check_log.c
1 /*
2  * Check: a unit test framework for C
3  * Copyright (C) 2001, 2002 Arien Malec
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include "config.h"
22
23 #include <stdlib.h>
24 #include <stdio.h>
25 #ifdef HAVE_SYS_TIME_H
26 #include <sys/time.h>
27 #endif
28 #include <time.h>
29 #include <check.h>
30 #if HAVE_SUBUNIT_CHILD_H
31 #include <subunit/child.h>
32 #endif
33
34 #include "check_error.h"
35 #include "check_list.h"
36 #include "check_impl.h"
37 #include "check_log.h"
38 #include "check_print.h"
39 #include "check_str.h"
40
41 /* localtime_r is apparently not available on Windows */
42 #ifndef HAVE_LOCALTIME_R
43 static struct tm *
44 localtime_r (const time_t * clock, struct tm *result)
45 {
46   struct tm *now = localtime (clock);
47   if (now == NULL) {
48     return NULL;
49   } else {
50     *result = *now;
51   }
52   return result;
53 }
54 #endif /* HAVE_DECL_LOCALTIME_R */
55
56 static void srunner_send_evt (SRunner * sr, void *obj, enum cl_event evt);
57
58 void
59 srunner_set_log (SRunner * sr, const char *fname)
60 {
61   if (sr->log_fname)
62     return;
63   sr->log_fname = fname;
64 }
65
66 int
67 srunner_has_log (SRunner * sr)
68 {
69   return sr->log_fname != NULL;
70 }
71
72 const char *
73 srunner_log_fname (SRunner * sr)
74 {
75   return sr->log_fname;
76 }
77
78
79 void
80 srunner_set_xml (SRunner * sr, const char *fname)
81 {
82   if (sr->xml_fname)
83     return;
84   sr->xml_fname = fname;
85 }
86
87 int
88 srunner_has_xml (SRunner * sr)
89 {
90   return sr->xml_fname != NULL;
91 }
92
93 const char *
94 srunner_xml_fname (SRunner * sr)
95 {
96   return sr->xml_fname;
97 }
98
99 void
100 srunner_register_lfun (SRunner * sr, FILE * lfile, int close,
101     LFun lfun, enum print_output printmode)
102 {
103   Log *l = emalloc (sizeof (Log));
104
105   if (printmode == CK_ENV) {
106     printmode = get_env_printmode ();
107   }
108
109   l->lfile = lfile;
110   l->lfun = lfun;
111   l->close = close;
112   l->mode = printmode;
113   list_add_end (sr->loglst, l);
114   return;
115 }
116
117 void
118 log_srunner_start (SRunner * sr)
119 {
120   srunner_send_evt (sr, NULL, CLSTART_SR);
121 }
122
123 void
124 log_srunner_end (SRunner * sr)
125 {
126   srunner_send_evt (sr, NULL, CLEND_SR);
127 }
128
129 void
130 log_suite_start (SRunner * sr, Suite * s)
131 {
132   srunner_send_evt (sr, s, CLSTART_S);
133 }
134
135 void
136 log_suite_end (SRunner * sr, Suite * s)
137 {
138   srunner_send_evt (sr, s, CLEND_S);
139 }
140
141 void
142 log_test_start (SRunner * sr, TCase * tc, TF * tfun)
143 {
144   char buffer[100];
145   snprintf (buffer, 99, "%s:%s", tc->name, tfun->name);
146   srunner_send_evt (sr, buffer, CLSTART_T);
147 }
148
149 void
150 log_test_end (SRunner * sr, TestResult * tr)
151 {
152   srunner_send_evt (sr, tr, CLEND_T);
153 }
154
155 static void
156 srunner_send_evt (SRunner * sr, void *obj, enum cl_event evt)
157 {
158   List *l;
159   Log *lg;
160   l = sr->loglst;
161   for (list_front (l); !list_at_end (l); list_advance (l)) {
162     lg = list_val (l);
163     fflush (lg->lfile);
164     lg->lfun (sr, lg->lfile, lg->mode, obj, evt);
165     fflush (lg->lfile);
166   }
167 }
168
169 void
170 stdout_lfun (SRunner * sr, FILE * file, enum print_output printmode,
171     void *obj, enum cl_event evt)
172 {
173   Suite *s;
174
175   if (printmode == CK_ENV) {
176     printmode = get_env_printmode ();
177   }
178
179   switch (evt) {
180     case CLINITLOG_SR:
181       break;
182     case CLENDLOG_SR:
183       break;
184     case CLSTART_SR:
185       if (printmode > CK_SILENT) {
186         fprintf (file, "Running suite(s):");
187       }
188       break;
189     case CLSTART_S:
190       s = obj;
191       if (printmode > CK_SILENT) {
192         fprintf (file, " %s\n", s->name);
193       }
194       break;
195     case CLEND_SR:
196       if (printmode > CK_SILENT) {
197         /* we don't want a newline before printing here, newlines should
198            come after printing a string, not before.  it's better to add
199            the newline above in CLSTART_S.
200          */
201         srunner_fprint (file, sr, printmode);
202       }
203       break;
204     case CLEND_S:
205       s = obj;
206       break;
207     case CLSTART_T:
208       break;
209     case CLEND_T:
210       break;
211     default:
212       eprintf ("Bad event type received in stdout_lfun", __FILE__, __LINE__);
213   }
214
215
216 }
217
218 void
219 lfile_lfun (SRunner * sr, FILE * file,
220     enum print_output printmode CK_ATTRIBUTE_UNUSED, void *obj,
221     enum cl_event evt)
222 {
223   TestResult *tr;
224   Suite *s;
225
226   switch (evt) {
227     case CLINITLOG_SR:
228       break;
229     case CLENDLOG_SR:
230       break;
231     case CLSTART_SR:
232       break;
233     case CLSTART_S:
234       s = obj;
235       fprintf (file, "Running suite %s\n", s->name);
236       break;
237     case CLEND_SR:
238       fprintf (file, "Results for all suites run:\n");
239       srunner_fprint (file, sr, CK_MINIMAL);
240       break;
241     case CLEND_S:
242       s = obj;
243       break;
244     case CLSTART_T:
245       break;
246     case CLEND_T:
247       tr = obj;
248       tr_fprint (file, tr, CK_VERBOSE);
249       break;
250     default:
251       eprintf ("Bad event type received in lfile_lfun", __FILE__, __LINE__);
252   }
253
254
255 }
256
257 void
258 xml_lfun (SRunner * sr CK_ATTRIBUTE_UNUSED, FILE * file,
259     enum print_output printmode CK_ATTRIBUTE_UNUSED, void *obj,
260     enum cl_event evt)
261 {
262   TestResult *tr;
263   Suite *s;
264   static struct timeval inittv, endtv;
265   static char t[sizeof "yyyy-mm-dd hh:mm:ss"] = { 0 };
266
267   if (t[0] == 0) {
268     struct tm now;
269     gettimeofday (&inittv, NULL);
270     localtime_r (&(inittv.tv_sec), &now);
271     strftime (t, sizeof ("yyyy-mm-dd hh:mm:ss"), "%Y-%m-%d %H:%M:%S", &now);
272   }
273
274   switch (evt) {
275     case CLINITLOG_SR:
276       fprintf (file, "<?xml version=\"1.0\"?>\n");
277       fprintf (file,
278           "<testsuites xmlns=\"http://check.sourceforge.net/ns\">\n");
279       fprintf (file, "  <datetime>%s</datetime>\n", t);
280       break;
281     case CLENDLOG_SR:
282       gettimeofday (&endtv, NULL);
283       fprintf (file, "  <duration>%f</duration>\n",
284           (endtv.tv_sec + (float) (endtv.tv_usec) / 1000000) -
285           (inittv.tv_sec + (float) (inittv.tv_usec / 1000000)));
286       fprintf (file, "</testsuites>\n");
287       break;
288     case CLSTART_SR:
289       break;
290     case CLSTART_S:
291       s = obj;
292       fprintf (file, "  <suite>\n");
293       fprintf (file, "    <title>%s</title>\n", s->name);
294       break;
295     case CLEND_SR:
296       break;
297     case CLEND_S:
298       fprintf (file, "  </suite>\n");
299       s = obj;
300       break;
301     case CLSTART_T:
302       break;
303     case CLEND_T:
304       tr = obj;
305       tr_xmlprint (file, tr, CK_VERBOSE);
306       break;
307     default:
308       eprintf ("Bad event type received in xml_lfun", __FILE__, __LINE__);
309   }
310
311 }
312
313 #if ENABLE_SUBUNIT
314 void
315 subunit_lfun (SRunner * sr, FILE * file, enum print_output printmode,
316     void *obj, enum cl_event evt)
317 {
318   TestResult *tr;
319   Suite *s;
320   char const *name;
321
322   /* assert(printmode == CK_SUBUNIT); */
323
324   switch (evt) {
325     case CLINITLOG_SR:
326       break;
327     case CLENDLOG_SR:
328       break;
329     case CLSTART_SR:
330       break;
331     case CLSTART_S:
332       s = obj;
333       break;
334     case CLEND_SR:
335       if (printmode > CK_SILENT) {
336         fprintf (file, "\n");
337         srunner_fprint (file, sr, printmode);
338       }
339       break;
340     case CLEND_S:
341       s = obj;
342       break;
343     case CLSTART_T:
344       name = obj;
345       subunit_test_start (name);
346       break;
347     case CLEND_T:
348       tr = obj;
349       {
350         char *name = ck_strdup_printf ("%s:%s", tr->tcname, tr->tname);
351         char *msg = tr_short_str (tr);
352         switch (tr->rtype) {
353           case CK_PASS:
354             subunit_test_pass (name);
355             break;
356           case CK_FAILURE:
357             subunit_test_fail (name, msg);
358             break;
359           case CK_ERROR:
360             subunit_test_error (name, msg);
361             break;
362           default:
363             eprintf ("Bad result type in subunit_lfun", __FILE__, __LINE__);
364             free (name);
365             free (msg);
366         }
367       }
368       break;
369     default:
370       eprintf ("Bad event type received in subunit_lfun", __FILE__, __LINE__);
371   }
372 }
373 #endif
374
375 FILE *
376 srunner_open_lfile (SRunner * sr)
377 {
378   FILE *f = NULL;
379   if (srunner_has_log (sr)) {
380     f = fopen (sr->log_fname, "w");
381     if (f == NULL)
382       eprintf ("Error in call to fopen while opening log file %s:", __FILE__,
383           __LINE__ - 2, sr->log_fname);
384   }
385   return f;
386 }
387
388 FILE *
389 srunner_open_xmlfile (SRunner * sr)
390 {
391   FILE *f = NULL;
392   if (srunner_has_xml (sr)) {
393     f = fopen (sr->xml_fname, "w");
394     if (f == NULL)
395       eprintf ("Error in call to fopen while opening xml file %s:", __FILE__,
396           __LINE__ - 2, sr->xml_fname);
397   }
398   return f;
399 }
400
401 void
402 srunner_init_logging (SRunner * sr, enum print_output print_mode)
403 {
404   FILE *f;
405   sr->loglst = check_list_create ();
406 #if ENABLE_SUBUNIT
407   if (print_mode != CK_SUBUNIT)
408 #endif
409     srunner_register_lfun (sr, stdout, 0, stdout_lfun, print_mode);
410 #if ENABLE_SUBUNIT
411   else
412     srunner_register_lfun (sr, stdout, 0, subunit_lfun, print_mode);
413 #endif
414   f = srunner_open_lfile (sr);
415   if (f) {
416     srunner_register_lfun (sr, f, 1, lfile_lfun, print_mode);
417   }
418   f = srunner_open_xmlfile (sr);
419   if (f) {
420     srunner_register_lfun (sr, f, 2, xml_lfun, print_mode);
421   }
422   srunner_send_evt (sr, NULL, CLINITLOG_SR);
423 }
424
425 void
426 srunner_end_logging (SRunner * sr)
427 {
428   List *l;
429   int rval;
430
431   srunner_send_evt (sr, NULL, CLENDLOG_SR);
432
433   l = sr->loglst;
434   for (list_front (l); !list_at_end (l); list_advance (l)) {
435     Log *lg = list_val (l);
436     if (lg->close) {
437       rval = fclose (lg->lfile);
438       if (rval != 0)
439         eprintf ("Error in call to fclose while closing log file:", __FILE__,
440             __LINE__ - 2);
441     }
442     free (lg);
443   }
444   list_free (l);
445   sr->loglst = NULL;
446 }