summaryrefslogtreecommitdiff
path: root/libs/gst/check/libcheck/check_log.c
blob: ad5bb7955687dd8a504b31b8c0c869d628fd18a7 (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
/*
 * Check: a unit test framework for C
 * Copyright (C) 2001, 2002 Arien Malec
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "libcompat.h"

#include <stdlib.h>
#include <stdio.h>
#include <internal-check.h>
#if ENABLE_SUBUNIT
#include <subunit/child.h>
#endif

#include "check_error.h"
#include "check_list.h"
#include "check_impl.h"
#include "check_log.h"
#include "check_print.h"
#include "check_str.h"

/*
 * If a log file is specified to be "-", then instead of
 * opening a file the log output is printed to stdout.
 */
#define STDOUT_OVERRIDE_LOG_FILE_NAME "-"

static void srunner_send_evt (SRunner * sr, void *obj, enum cl_event evt);

void
srunner_set_log (SRunner * sr, const char *fname)
{
  if (sr->log_fname)
    return;
  sr->log_fname = fname;
}

int
srunner_has_log (SRunner * sr)
{
  return srunner_log_fname (sr) != NULL;
}

const char *
srunner_log_fname (SRunner * sr)
{
  /* check if log filename have been set explicitly */
  if (sr->log_fname != NULL)
    return sr->log_fname;

  return getenv ("CK_LOG_FILE_NAME");
}


void
srunner_set_xml (SRunner * sr, const char *fname)
{
  if (sr->xml_fname)
    return;
  sr->xml_fname = fname;
}

int
srunner_has_xml (SRunner * sr)
{
  return srunner_xml_fname (sr) != NULL;
}

const char *
srunner_xml_fname (SRunner * sr)
{
  /* check if XML log filename have been set explicitly */
  if (sr->xml_fname != NULL) {
    return sr->xml_fname;
  }

  return getenv ("CK_XML_LOG_FILE_NAME");
}

void
srunner_set_tap (SRunner * sr, const char *fname)
{
  if (sr->tap_fname)
    return;
  sr->tap_fname = fname;
}

int
srunner_has_tap (SRunner * sr)
{
  return srunner_tap_fname (sr) != NULL;
}

const char *
srunner_tap_fname (SRunner * sr)
{
  /* check if tap log filename have been set explicitly */
  if (sr->tap_fname != NULL) {
    return sr->tap_fname;
  }

  return getenv ("CK_TAP_LOG_FILE_NAME");
}

void
srunner_register_lfun (SRunner * sr, FILE * lfile, int close,
    LFun lfun, enum print_output printmode)
{
  Log *l = (Log *) emalloc (sizeof (Log));

  if (printmode == CK_ENV) {
    printmode = get_env_printmode ();
  }

  l->lfile = lfile;
  l->lfun = lfun;
  l->close = close;
  l->mode = printmode;
  check_list_add_end (sr->loglst, l);
  return;
}

void
log_srunner_start (SRunner * sr)
{
  srunner_send_evt (sr, NULL, CLSTART_SR);
}

void
log_srunner_end (SRunner * sr)
{
  srunner_send_evt (sr, NULL, CLEND_SR);
}

void
log_suite_start (SRunner * sr, Suite * s)
{
  srunner_send_evt (sr, s, CLSTART_S);
}

void
log_suite_end (SRunner * sr, Suite * s)
{
  srunner_send_evt (sr, s, CLEND_S);
}

void
log_test_start (SRunner * sr, TCase * tc, TF * tfun)
{
  char buffer[100];

  snprintf (buffer, 99, "%s:%s", tc->name, tfun->name);
  srunner_send_evt (sr, buffer, CLSTART_T);
}

void
log_test_end (SRunner * sr, TestResult * tr)
{
  srunner_send_evt (sr, tr, CLEND_T);
}

static void
srunner_send_evt (SRunner * sr, void *obj, enum cl_event evt)
{
  List *l;
  Log *lg;

  l = sr->loglst;
  for (check_list_front (l); !check_list_at_end (l); check_list_advance (l)) {
    lg = (Log *) check_list_val (l);
    fflush (lg->lfile);
    lg->lfun (sr, lg->lfile, lg->mode, obj, evt);
    fflush (lg->lfile);
  }
}

void
stdout_lfun (SRunner * sr, FILE * file, enum print_output printmode,
    void *obj, enum cl_event evt)
{
  Suite *s;

  switch (evt) {
    case CLINITLOG_SR:
      break;
    case CLENDLOG_SR:
      break;
    case CLSTART_SR:
      if (printmode > CK_SILENT) {
        fprintf (file, "Running suite(s):");
      }
      break;
    case CLSTART_S:
      s = (Suite *) obj;
      if (printmode > CK_SILENT) {
        fprintf (file, " %s\n", s->name);
      }
      break;
    case CLEND_SR:
      if (printmode > CK_SILENT) {
        /* we don't want a newline before printing here, newlines should
           come after printing a string, not before.  it's better to add
           the newline above in CLSTART_S.
         */
        srunner_fprint (file, sr, printmode);
      }
      break;
    case CLEND_S:
      break;
    case CLSTART_T:
      break;
    case CLEND_T:
      break;
    default:
      eprintf ("Bad event type received in stdout_lfun", __FILE__, __LINE__);
  }


}

void
lfile_lfun (SRunner * sr, FILE * file,
    enum print_output printmode CK_ATTRIBUTE_UNUSED, void *obj,
    enum cl_event evt)
{
  TestResult *tr;
  Suite *s;

  switch (evt) {
    case CLINITLOG_SR:
      break;
    case CLENDLOG_SR:
      break;
    case CLSTART_SR:
      break;
    case CLSTART_S:
      s = (Suite *) obj;
      fprintf (file, "Running suite %s\n", s->name);
      break;
    case CLEND_SR:
      fprintf (file, "Results for all suites run:\n");
      srunner_fprint (file, sr, CK_MINIMAL);
      break;
    case CLEND_S:
      break;
    case CLSTART_T:
      break;
    case CLEND_T:
      tr = (TestResult *) obj;
      tr_fprint (file, tr, CK_VERBOSE);
      break;
    default:
      eprintf ("Bad event type received in lfile_lfun", __FILE__, __LINE__);
  }


}

void
xml_lfun (SRunner * sr CK_ATTRIBUTE_UNUSED, FILE * file,
    enum print_output printmode CK_ATTRIBUTE_UNUSED, void *obj,
    enum cl_event evt)
{
  TestResult *tr;
  Suite *s;
  static struct timespec ts_start = { 0, 0 };
  static char t[sizeof "yyyy-mm-dd hh:mm:ss"] = { 0 };

  if (t[0] == 0) {
    struct timeval inittv;
    struct tm now;

    gettimeofday (&inittv, NULL);
    clock_gettime (check_get_clockid (), &ts_start);
    if (localtime_r ((const time_t *) &(inittv.tv_sec), &now) != NULL) {
      strftime (t, sizeof ("yyyy-mm-dd hh:mm:ss"), "%Y-%m-%d %H:%M:%S", &now);
    }
  }

  switch (evt) {
    case CLINITLOG_SR:
      fprintf (file, "<?xml version=\"1.0\"?>\n");
      fprintf (file,
          "<?xml-stylesheet type=\"text/xsl\" href=\"http://check.sourceforge.net/xml/check_unittest.xslt\"?>\n");
      fprintf (file,
          "<testsuites xmlns=\"http://check.sourceforge.net/ns\">\n");
      fprintf (file, "  <datetime>%s</datetime>\n", t);
      break;
    case CLENDLOG_SR:
    {
      struct timespec ts_end = { 0, 0 };
      unsigned long duration;

      /* calculate time the test were running */
      clock_gettime (check_get_clockid (), &ts_end);
      duration = (unsigned long) DIFF_IN_USEC (ts_start, ts_end);
      fprintf (file, "  <duration>%lu.%06lu</duration>\n",
          duration / US_PER_SEC, duration % US_PER_SEC);
      fprintf (file, "</testsuites>\n");
    }
      break;
    case CLSTART_SR:
      break;
    case CLSTART_S:
      s = (Suite *) obj;
      fprintf (file, "  <suite>\n");
      fprintf (file, "    <title>");
      fprint_xml_esc (file, s->name);
      fprintf (file, "</title>\n");
      break;
    case CLEND_SR:
      break;
    case CLEND_S:
      fprintf (file, "  </suite>\n");
      break;
    case CLSTART_T:
      break;
    case CLEND_T:
      tr = (TestResult *) obj;
      tr_xmlprint (file, tr, CK_VERBOSE);
      break;
    default:
      eprintf ("Bad event type received in xml_lfun", __FILE__, __LINE__);
  }

}

void
tap_lfun (SRunner * sr CK_ATTRIBUTE_UNUSED, FILE * file,
    enum print_output printmode CK_ATTRIBUTE_UNUSED, void *obj,
    enum cl_event evt)
{
  TestResult *tr;

  static int num_tests_run = 0;

  switch (evt) {
    case CLINITLOG_SR:
      /* As this is a new log file, reset the number of tests executed */
      num_tests_run = 0;
      break;
    case CLENDLOG_SR:
      /* Output the test plan as the last line */
      fprintf (file, "1..%d\n", num_tests_run);
      fflush (file);
      break;
    case CLSTART_SR:
      break;
    case CLSTART_S:
      break;
    case CLEND_SR:
      break;
    case CLEND_S:
      break;
    case CLSTART_T:
      break;
    case CLEND_T:
      /* Print the test result to the tap file */
      num_tests_run += 1;
      tr = (TestResult *) obj;
      fprintf (file, "%s %d - %s:%s:%s: %s\n",
          tr->rtype == CK_PASS ? "ok" : "not ok", num_tests_run,
          tr->file, tr->tcname, tr->tname, tr->msg);
      fflush (file);
      break;
    default:
      eprintf ("Bad event type received in tap_lfun", __FILE__, __LINE__);
  }
}

#if ENABLE_SUBUNIT
void
subunit_lfun (SRunner * sr, FILE * file, enum print_output printmode,
    void *obj, enum cl_event evt)
{
  TestResult *tr;
  char const *name;

  /* assert(printmode == CK_SUBUNIT); */

  switch (evt) {
    case CLINITLOG_SR:
      break;
    case CLENDLOG_SR:
      break;
    case CLSTART_SR:
      break;
    case CLSTART_S:
      break;
    case CLEND_SR:
      if (printmode > CK_SILENT) {
        fprintf (file, "\n");
        srunner_fprint (file, sr, printmode);
      }
      break;
    case CLEND_S:
      break;
    case CLSTART_T:
      name = (const char *) obj;
      subunit_test_start (name);
      break;
    case CLEND_T:
      tr = (TestResult *) obj;
      {
        char *name = ck_strdup_printf ("%s:%s", tr->tcname, tr->tname);
        char *msg = tr_short_str (tr);

        switch (tr->rtype) {
          case CK_PASS:
            subunit_test_pass (name);
            break;
          case CK_FAILURE:
            subunit_test_fail (name, msg);
            break;
          case CK_ERROR:
            subunit_test_error (name, msg);
            break;
          case CK_TEST_RESULT_INVALID:
          default:
            eprintf ("Bad result type in subunit_lfun", __FILE__, __LINE__);
            free (name);
            free (msg);
        }
      }
      break;
    default:
      eprintf ("Bad event type received in subunit_lfun", __FILE__, __LINE__);
  }
}
#endif

static FILE *
srunner_open_file (const char *filename)
{
  FILE *f = NULL;

  if (strcmp (filename, STDOUT_OVERRIDE_LOG_FILE_NAME) == 0) {
    f = stdout;
  } else {
    f = fopen (filename, "w");
    if (f == NULL) {
      eprintf ("Error in call to fopen while opening file %s:", __FILE__,
          __LINE__ - 2, filename);
    }
  }
  return f;
}

FILE *
srunner_open_lfile (SRunner * sr)
{
  FILE *f = NULL;

  if (srunner_has_log (sr)) {
    f = srunner_open_file (srunner_log_fname (sr));
  }
  return f;
}

FILE *
srunner_open_xmlfile (SRunner * sr)
{
  FILE *f = NULL;

  if (srunner_has_xml (sr)) {
    f = srunner_open_file (srunner_xml_fname (sr));
  }
  return f;
}

FILE *
srunner_open_tapfile (SRunner * sr)
{
  FILE *f = NULL;

  if (srunner_has_tap (sr)) {
    f = srunner_open_file (srunner_tap_fname (sr));
  }
  return f;
}

void
srunner_init_logging (SRunner * sr, enum print_output print_mode)
{
  FILE *f;

  sr->loglst = check_list_create ();
#if ENABLE_SUBUNIT
  if (print_mode != CK_SUBUNIT)
#endif
    srunner_register_lfun (sr, stdout, 0, stdout_lfun, print_mode);
#if ENABLE_SUBUNIT
  else
    srunner_register_lfun (sr, stdout, 0, subunit_lfun, print_mode);
#endif
  f = srunner_open_lfile (sr);
  if (f) {
    srunner_register_lfun (sr, f, f != stdout, lfile_lfun, print_mode);
  }
  f = srunner_open_xmlfile (sr);
  if (f) {
    srunner_register_lfun (sr, f, f != stdout, xml_lfun, print_mode);
  }
  f = srunner_open_tapfile (sr);
  if (f) {
    srunner_register_lfun (sr, f, f != stdout, tap_lfun, print_mode);
  }
  srunner_send_evt (sr, NULL, CLINITLOG_SR);
}

void
srunner_end_logging (SRunner * sr)
{
  List *l;
  int rval;

  srunner_send_evt (sr, NULL, CLENDLOG_SR);

  l = sr->loglst;
  for (check_list_front (l); !check_list_at_end (l); check_list_advance (l)) {
    Log *lg = (Log *) check_list_val (l);

    if (lg->close) {
      rval = fclose (lg->lfile);
      if (rval != 0)
        eprintf ("Error in call to fclose while closing log file:",
            __FILE__, __LINE__ - 2);
    }
    free (lg);
  }
  check_list_free (l);
  sr->loglst = NULL;
}