glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / util / test_common_logging_dummy.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2008-2013 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14 */
15
16 /**
17  * @file util/test_common_logging_dummy.c
18  * @brief dummy labrat for the testcase for the logging module (runtime
19  * log level adjustment)
20  * @author LRN
21  */
22 #include "platform.h"
23 #undef GNUNET_EXTRA_LOGGING
24 #define GNUNET_EXTRA_LOGGING GNUNET_YES
25
26 #include "gnunet_util_lib.h"
27
28 /**
29  * Artificial delay attached to each log call that is not skipped out.
30  * This must be long enough for us to not to mistake skipped log call
31  * on a slow machine for a non-skipped one.
32  */
33 #define OUTPUT_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MICROSECONDS, 1000)
34
35 static void
36 my_log (void *ctx, enum GNUNET_ErrorType kind, const char *component,
37         const char *date, const char *msg)
38 {
39   if (strncmp ("test-common-logging-dummy", component, 25) != 0)
40     return;
41   FPRINTF (stdout, "%s", msg);
42   fflush (stdout);
43 }
44
45
46 #if !defined(GNUNET_CULL_LOGGING)
47 static int
48 expensive_func ()
49 {
50   return GNUNET_NETWORK_socket_select (NULL, NULL, NULL, OUTPUT_DELAY);
51 }
52 #endif
53
54
55 #define pr(kind,lvl) {\
56   struct GNUNET_TIME_Absolute t1, t2;\
57   t1 = GNUNET_TIME_absolute_get ();\
58   GNUNET_log (kind, "L%s %d\n", lvl, expensive_func());\
59   t2 = GNUNET_TIME_absolute_get ();\
60   printf ("1%s %llu\n", lvl,\
61           (unsigned long long) GNUNET_TIME_absolute_get_difference (t1, t2).rel_value_us); \
62 }
63
64 #define pr2(kind,lvl) {\
65   struct GNUNET_TIME_Absolute t1, t2;\
66   t1 = GNUNET_TIME_absolute_get ();\
67   GNUNET_log (kind, "L%s %d\n", lvl, expensive_func());\
68   t2 = GNUNET_TIME_absolute_get ();\
69   printf ("2%s %llu\n", lvl,\
70           (unsigned long long) GNUNET_TIME_absolute_get_difference (t1, t2).rel_value_us); \
71 }
72
73 int
74 main (int argc, char *argv[])
75 {
76   /* We set up logging with NULL level - will be overrided by
77    * GNUNET_LOG or GNUNET_FORCE_LOG at runtime.
78    */
79   GNUNET_log_setup ("test-common-logging-dummy", NULL, "/dev/null");
80   GNUNET_logger_add (&my_log, NULL);
81   pr (GNUNET_ERROR_TYPE_ERROR, "ERROR");
82   pr (GNUNET_ERROR_TYPE_WARNING, "WARNING");
83   pr (GNUNET_ERROR_TYPE_INFO, "INFO");
84   pr (GNUNET_ERROR_TYPE_DEBUG, "DEBUG");
85
86   /* We set up logging with WARNING level - will onle be overrided by
87    * GNUNET_FORCE_LOG at runtime.
88    */
89   GNUNET_log_setup ("test-common-logging-dummy", "WARNING", "/dev/null");
90   pr2 (GNUNET_ERROR_TYPE_ERROR, "ERROR");
91   pr2 (GNUNET_ERROR_TYPE_WARNING, "WARNING");
92   pr2 (GNUNET_ERROR_TYPE_INFO, "INFO");
93   pr2 (GNUNET_ERROR_TYPE_DEBUG, "DEBUG");
94   return 0;
95 }                               /* end of main */
96
97 /* end of test_common_logging_dummy.c */