File: logger.c

package info (click to toggle)
libcdio 2.2.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 16,140 kB
  • sloc: ansic: 39,407; cpp: 2,556; sh: 1,263; makefile: 826; yacc: 324; ruby: 116; perl: 34
file content (158 lines) | stat: -rw-r--r-- 4,196 bytes parent folder | download | duplicates (4)
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
#ifdef HAVE_CONFIG_H
#include "config.h"
#define __CDIO_CONFIG_H__ 1
#endif

#ifdef HAVE_STDIO_H
#include <stdio.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif

#include <cdio/logging.h>

char *last_debugmsg = NULL;
char *last_infomsg = NULL;
char *last_warnmsg = NULL;
char *last_errmsg = NULL;
char *last_assertmsg = NULL;
char *last_othermsg = NULL;

/* Here is an example of a custom log handler. */
static void
test_log_handler (cdio_log_level_t level, const char *message)
{
switch(level) {
  case CDIO_LOG_DEBUG:
    last_debugmsg = strdup(message);
    return;
  case CDIO_LOG_INFO:
    last_infomsg = strdup(message);
    return;
  case CDIO_LOG_WARN:
    last_warnmsg = strdup(message);
    return;
  case CDIO_LOG_ERROR:
    last_errmsg = strdup(message);
    return;
  case CDIO_LOG_ASSERT:
    last_assertmsg = strdup(message);
    return;
  default:
    fprintf(stderr, "level %d message: %s should not happen\n",
	   level, message);
    exit(20);
  }
}

int
main(int argc, const char *argv[])
{
    const char *test_msg = "test message";
#ifndef HAVE_WIN32_CDROM
    cdio_log_handler_t old_log_handler = (cdio_log_handler_t) NULL;

    old_log_handler = cdio_log_set_handler(test_log_handler);
    if (old_log_handler != cdio_default_log_handler) {
      fprintf(stderr, "Should have gotten old log handler back %p vs %p\n",
	      (void *) old_log_handler,
	      (void *) cdio_default_log_handler);
	exit(1);
    }
#else
    cdio_log_set_handler(test_log_handler);
#endif

    /* Check that calling each of the logger routine appears in the right variable,
       based on the log level */

    test_msg = "debug";
    cdio_debug("%s", test_msg);

    if (last_debugmsg != NULL) {
	fprintf(stderr, "debug message should have been ignored due to default log level\n");
	exit(2);
    }

    cdio_loglevel_default = CDIO_LOG_DEBUG;
    cdio_debug("%s", test_msg);
    if (last_debugmsg == NULL ||
	strncmp(test_msg, last_debugmsg, strlen(test_msg)) != 0) {
	fprintf(stderr, "debug message %s did not get handled\n",
		last_debugmsg);
        free(last_debugmsg);
	exit(2);
    }
    free(last_debugmsg);

    test_msg = "info";
    cdio_info("%s", test_msg);
    if (strncmp(test_msg, last_infomsg, strlen(test_msg)) != 0) {
	fprintf(stderr, "info message %s did not get handled\n", last_infomsg);
        free(last_infomsg);
	exit(3);
    }
    free(last_infomsg);

    test_msg = "warn";
    cdio_warn("%s", test_msg);
    if (strncmp(test_msg, last_warnmsg, strlen(test_msg)) != 0) {
	fprintf(stderr, "warn message %s did not get handled\n", last_warnmsg);
        free(last_warnmsg);
	exit(4);
    }
    free(last_warnmsg);

    test_msg = "error";
    cdio_error("%s", test_msg);
    if (strncmp(test_msg, last_errmsg, strlen(test_msg)) != 0) {
	fprintf(stderr, "error message %s did not get handled\n", last_errmsg);
        free(last_errmsg);
	exit(5);
    }
    free(last_errmsg);

    /* Try using generic cdio_log routine */

    test_msg = "debug via cdio_log";
    cdio_log(CDIO_LOG_DEBUG, "%s", test_msg);
    if (strncmp(test_msg, last_debugmsg, strlen(test_msg)) != 0) {
	fprintf(stderr, "debug message %s did not get handled, phase 2\n", last_debugmsg);
        free(last_debugmsg);
	exit(6);
    }
    free(last_debugmsg);

    test_msg = "info via cdio_log";
    cdio_log(CDIO_LOG_INFO, "%s", test_msg);
    if (strncmp(test_msg, last_infomsg, strlen(test_msg)) != 0) {
	fprintf(stderr, "info message %s did not get handled\n", last_infomsg);
        free(last_infomsg);
	exit(7);
    }
    free(last_infomsg);

    test_msg = "warn via cdio_log";
    cdio_log(CDIO_LOG_WARN, "%s", test_msg);
    if (strncmp(test_msg, last_warnmsg, strlen(test_msg)) != 0) {
	fprintf(stderr, "warn message %s did not get handled\n", last_warnmsg);
        free(last_warnmsg);
	exit(8);
    }
    free(last_warnmsg);

    test_msg = "error via cdio_log";
    cdio_log(CDIO_LOG_ERROR, "%s", test_msg);
    if (strncmp(test_msg, last_errmsg, strlen(test_msg)) != 0) {
	fprintf(stderr, "error message %s did not get handled\n", last_errmsg);
        free(last_errmsg);
	exit(9);
    }
    free(last_errmsg);

    exit(0);
}