test_common_logging_dummy.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2008-2013 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  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. #include "gnunet_util_lib.h"
  26. /**
  27. * Artificial delay attached to each log call that is not skipped out.
  28. * This must be long enough for us to not to mistake skipped log call
  29. * on a slow machine for a non-skipped one.
  30. */
  31. #define OUTPUT_DELAY \
  32. GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MICROSECONDS, 1000)
  33. static void
  34. my_log (void *ctx,
  35. enum GNUNET_ErrorType kind,
  36. const char *component,
  37. const char *date,
  38. const char *msg)
  39. {
  40. (void) ctx;
  41. (void) kind;
  42. (void) component;
  43. (void) date;
  44. if (strncmp ("test-common-logging-dummy", component, 25) != 0)
  45. return;
  46. fprintf (stdout, "%s", msg);
  47. fflush (stdout);
  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. #define pr(kind, lvl) \
  57. { \
  58. struct GNUNET_TIME_Absolute t1, t2; \
  59. t1 = GNUNET_TIME_absolute_get (); \
  60. GNUNET_log (kind, "L%s %d\n", lvl, expensive_func ()); \
  61. t2 = GNUNET_TIME_absolute_get (); \
  62. printf ("1%s %llu\n", \
  63. lvl, \
  64. (unsigned long long) GNUNET_TIME_absolute_get_difference (t1, t2) \
  65. .rel_value_us); \
  66. }
  67. #define pr2(kind, lvl) \
  68. { \
  69. struct GNUNET_TIME_Absolute t1, t2; \
  70. t1 = GNUNET_TIME_absolute_get (); \
  71. GNUNET_log (kind, "L%s %d\n", lvl, expensive_func ()); \
  72. t2 = GNUNET_TIME_absolute_get (); \
  73. printf ("2%s %llu\n", \
  74. lvl, \
  75. (unsigned long long) GNUNET_TIME_absolute_get_difference (t1, t2) \
  76. .rel_value_us); \
  77. }
  78. int
  79. main (int argc, char *argv[])
  80. {
  81. (void) argc;
  82. (void) argv;
  83. /* We set up logging with NULL level - will be overridden by
  84. * GNUNET_LOG or GNUNET_FORCE_LOG at runtime.
  85. */
  86. GNUNET_log_setup ("test-common-logging-dummy", NULL, "/dev/null");
  87. GNUNET_logger_add (&my_log, NULL);
  88. pr (GNUNET_ERROR_TYPE_ERROR, "ERROR");
  89. pr (GNUNET_ERROR_TYPE_WARNING, "WARNING");
  90. pr (GNUNET_ERROR_TYPE_INFO, "INFO");
  91. pr (GNUNET_ERROR_TYPE_DEBUG, "DEBUG");
  92. /* We set up logging with WARNING level - will onle be overridden by
  93. * GNUNET_FORCE_LOG at runtime.
  94. */
  95. GNUNET_log_setup ("test-common-logging-dummy", "WARNING", "/dev/null");
  96. pr2 (GNUNET_ERROR_TYPE_ERROR, "ERROR");
  97. pr2 (GNUNET_ERROR_TYPE_WARNING, "WARNING");
  98. pr2 (GNUNET_ERROR_TYPE_INFO, "INFO");
  99. pr2 (GNUNET_ERROR_TYPE_DEBUG, "DEBUG");
  100. return 0;
  101. } /* end of main */
  102. /* end of test_common_logging_dummy.c */