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