Debian Codegraph @

tests/logging.c

raw symbols SCIP
1 /* liblouis Braille Translation and Back-Translation Library
2
3 Copyright (C) 2014 Swiss Library for the Blind, Visually Impaired and Print Disabled
4
5 Copying and distribution of this file, with or without modification,
6 are permitted in any medium without royalty provided the copyright
7 notice and this notice are preserved. This file is offered as-is,
8 without any warranty. */
9
10 #include <config.h>
11
12 #include <stdio.h>
13 #include <string.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include "internal.h"
17
18 static char log_buffer[1024];
19 static int log_buffer_pos = 0;
20
21 static void
22 log_to_buffer(logLevels level, const char *message)
23 {
24 switch(level) {
25 case LOU_LOG_DEBUG:
26 log_buffer_pos += sprintf(&log_buffer[log_buffer_pos], "[DEBUG] %s\n", message);
27 break;
28 case LOU_LOG_INFO:
29 log_buffer_pos += sprintf(&log_buffer[log_buffer_pos], "[INFO] %s\n", message);
30 break;
31 case LOU_LOG_WARN:
32 log_buffer_pos += sprintf(&log_buffer[log_buffer_pos], "[WARN] %s\n", message);
33 break;
34 case LOU_LOG_ERROR:
35 log_buffer_pos += sprintf(&log_buffer[log_buffer_pos], "[ERROR] %s\n", message);
36 break;
37 case LOU_LOG_FATAL:
38 log_buffer_pos += sprintf(&log_buffer[log_buffer_pos], "[FATAL] %s\n", message);
39 break;
40 }
41 }
42
43 static int
44 assert_string_equals(char * expected, char * actual)
45 {
46 if (strcmp(expected, actual))
47 {
48 printf("Expected \"%s\" but received \"%s\"\n", expected, actual);
49 return 1;
50 }
51 return 0;
52 }
53
54 static int
55 assert_log_buffer_equals(char * expected)
56 {
57 return assert_string_equals(expected, log_buffer);
58 }
59
60 int
61 main(int argc, char **argv)
62 {
63 lou_registerLogCallback(log_to_buffer);
64 log_buffer_pos = 0;
65 lou_setLogLevel(LOU_LOG_WARN);
66 _lou_logMessage(LOU_LOG_ERROR, "foo");
67 _lou_logMessage(LOU_LOG_INFO, "bar");
68 lou_setLogLevel(LOU_LOG_INFO);
69 _lou_logMessage(LOU_LOG_INFO, "baz");
70 return assert_log_buffer_equals("[ERROR] foo\n[INFO] baz\n");
71 }