a940d3c07a526fa231b55c0a8ba75af51f3b7693
[oweals/gnunet.git] / src / util / test_common_logging.c
1 /*
2      This file is part of GNUnet.
3      (C) 2008 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file util/test_common_logging.c
23  * @brief testcase for the logging module
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_common.h"
28
29 static void
30 my_log (void *ctx, enum GNUNET_ErrorType kind, const char *component,
31         const char *date, const char *msg)
32 {
33   unsigned int *c = ctx;
34
35   (*c)++;
36 }
37
38
39
40 int
41 main (int argc, char *argv[])
42 {
43   unsigned int failureCount = 0;
44   unsigned int logs = 0;
45
46   if (0 != setenv ("GNUNET_FORCE_LOG", "", 1))
47     fprintf (stderr,
48              "Failed to setenv: %s\n",
49              strerror (errno));
50   GNUNET_log_setup ("test-common-logging", "DEBUG", "/dev/null");
51   GNUNET_logger_add (&my_log, &logs);
52   GNUNET_logger_add (&my_log, &logs);
53   GNUNET_log (GNUNET_ERROR_TYPE_BULK, "Testing...\n");
54   GNUNET_log (GNUNET_ERROR_TYPE_BULK, "Testing...\n");
55   GNUNET_log (GNUNET_ERROR_TYPE_BULK, "Testing...\n");
56   GNUNET_log (GNUNET_ERROR_TYPE_BULK, "Testing...\n");
57   GNUNET_log (GNUNET_ERROR_TYPE_BULK, "Testing...\n");
58   GNUNET_log (GNUNET_ERROR_TYPE_BULK, "Testing...\n");
59   GNUNET_logger_remove (&my_log, &logs);
60   GNUNET_log (GNUNET_ERROR_TYPE_BULK, "Flusher...\n");
61   /* the last 6 calls should be merged (repated bulk messages!) */
62   GNUNET_logger_remove (&my_log, &logs);
63   if (logs != 4)
64     {
65       fprintf (stdout, "Expected 4 log calls, got %u\n", logs);
66       failureCount++;
67     }
68   GNUNET_break (0 ==
69                 strcmp (_("ERROR"),
70                         GNUNET_error_type_to_string
71                         (GNUNET_ERROR_TYPE_ERROR)));
72   GNUNET_break (0 ==
73                 strcmp (_("WARNING"),
74                         GNUNET_error_type_to_string
75                         (GNUNET_ERROR_TYPE_WARNING)));
76   GNUNET_break (0 ==
77                 strcmp (_("INFO"),
78                         GNUNET_error_type_to_string
79                         (GNUNET_ERROR_TYPE_INFO)));
80   GNUNET_break (0 ==
81                 strcmp (_("DEBUG"),
82                         GNUNET_error_type_to_string
83                         (GNUNET_ERROR_TYPE_DEBUG)));
84   GNUNET_log_setup ("test_common_logging", "WARNING", "/dev/null");
85   logs = 0;
86   GNUNET_logger_add (&my_log, &logs);
87   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Checker...\n");
88   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Drop me...\n");
89   GNUNET_logger_remove (&my_log, &logs);
90   if (logs != 1)
91     {
92       fprintf (stdout, "Expected 1 log call, got %u\n", logs);
93       failureCount++;
94     }
95
96   if (failureCount != 0)
97     {
98       fprintf (stdout, "%u TESTS FAILED!\n", failureCount);
99       return -1;
100     }
101   return 0;
102 }                               /* end of main */
103
104 /* end of test_common_logging.c */