afa78aad064417d73e558a2b2e96e807f31eb4af
[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      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /**
20  * @file util/test_common_logging_dummy.c
21  * @brief dummy labrat for the testcase for the logging module (runtime
22  * log level adjustment)
23  * @author LRN
24  */
25 #include "platform.h"
26 #undef GNUNET_EXTRA_LOGGING
27 #define GNUNET_EXTRA_LOGGING GNUNET_YES
28
29 #include "gnunet_util_lib.h"
30
31 /**
32  * Artificial delay attached to each log call that is not skipped out.
33  * This must be long enough for us to not to mistake skipped log call
34  * on a slow machine for a non-skipped one.
35  */
36 #define OUTPUT_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MICROSECONDS, 1000)
37
38 static void
39 my_log (void *ctx, enum GNUNET_ErrorType kind, const char *component,
40         const char *date, const char *msg)
41 {
42   if (strncmp ("test-common-logging-dummy", component, 25) != 0)
43     return;
44   FPRINTF (stdout, "%s", msg);
45   fflush (stdout);
46 }
47
48
49 #if !defined(GNUNET_CULL_LOGGING)
50 static int
51 expensive_func ()
52 {
53   return GNUNET_NETWORK_socket_select (NULL, NULL, NULL, OUTPUT_DELAY);
54 }
55 #endif
56
57
58 #define pr(kind,lvl) {\
59   struct GNUNET_TIME_Absolute t1, t2;\
60   t1 = GNUNET_TIME_absolute_get ();\
61   GNUNET_log (kind, "L%s %d\n", lvl, expensive_func());\
62   t2 = GNUNET_TIME_absolute_get ();\
63   printf ("1%s %llu\n", lvl,\
64           (unsigned long long) GNUNET_TIME_absolute_get_difference (t1, t2).rel_value_us); \
65 }
66
67 #define pr2(kind,lvl) {\
68   struct GNUNET_TIME_Absolute t1, t2;\
69   t1 = GNUNET_TIME_absolute_get ();\
70   GNUNET_log (kind, "L%s %d\n", lvl, expensive_func());\
71   t2 = GNUNET_TIME_absolute_get ();\
72   printf ("2%s %llu\n", lvl,\
73           (unsigned long long) GNUNET_TIME_absolute_get_difference (t1, t2).rel_value_us); \
74 }
75
76 int
77 main (int argc, char *argv[])
78 {
79   /* We set up logging with NULL level - will be overrided by
80    * GNUNET_LOG or GNUNET_FORCE_LOG at runtime.
81    */
82   GNUNET_log_setup ("test-common-logging-dummy", NULL, "/dev/null");
83   GNUNET_logger_add (&my_log, NULL);
84   pr (GNUNET_ERROR_TYPE_ERROR, "ERROR");
85   pr (GNUNET_ERROR_TYPE_WARNING, "WARNING");
86   pr (GNUNET_ERROR_TYPE_INFO, "INFO");
87   pr (GNUNET_ERROR_TYPE_DEBUG, "DEBUG");
88
89   /* We set up logging with WARNING level - will onle be overrided by
90    * GNUNET_FORCE_LOG at runtime.
91    */
92   GNUNET_log_setup ("test-common-logging-dummy", "WARNING", "/dev/null");
93   pr2 (GNUNET_ERROR_TYPE_ERROR, "ERROR");
94   pr2 (GNUNET_ERROR_TYPE_WARNING, "WARNING");
95   pr2 (GNUNET_ERROR_TYPE_INFO, "INFO");
96   pr2 (GNUNET_ERROR_TYPE_DEBUG, "DEBUG");
97   return 0;
98 }                               /* end of main */
99
100 /* end of test_common_logging_dummy.c */