buffer_logger_test.cc 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <log/macros.h>
  15. #include <log/logger_support.h>
  16. #include <log/logger_manager.h>
  17. #include <log/log_messages.h>
  18. #include <util/interprocess_sync_null.h>
  19. using namespace isc::log;
  20. namespace {
  21. void usage() {
  22. std::cout << "Usage: buffer_logger_test [-nv]" << std::endl;
  23. }
  24. } // end unnamed namespace
  25. /// \brief Test InitLogger
  26. ///
  27. /// A program used in testing the logger that initializes logging with
  28. /// buffering enabled, so that initial log messages are not immediately
  29. /// logged, but are not lost (they should be logged the moment process()
  30. /// is called.
  31. ///
  32. /// If -n is given as an argument, process() is never called. In this
  33. /// case, upon exit, all leftover log messages should be printed to
  34. /// stdout, but without normal logging additions (such as time and
  35. /// logger name)
  36. int
  37. main(int argc, char** argv) {
  38. bool do_process = true;
  39. int opt;
  40. while ((opt = getopt(argc, argv, "n")) != -1) {
  41. switch (opt) {
  42. case 'n':
  43. do_process = false;
  44. break;
  45. default:
  46. usage();
  47. return (1);
  48. }
  49. }
  50. // Note, level is set to DEBUG here, but back to INFO
  51. // in process(), so when flushing to stdout (-n), the DEBUG
  52. // message should show up
  53. initLogger("buffertest", isc::log::DEBUG, 99, NULL, true);
  54. Logger logger("log");
  55. // No need for file interprocess locking in this test
  56. logger.setInterprocessSync(new isc::util::InterprocessSyncNull("logger"));
  57. LOG_INFO(logger, LOG_BAD_SEVERITY).arg("info");
  58. LOG_DEBUG(logger, 50, LOG_BAD_DESTINATION).arg("debug-50");
  59. LOG_INFO(logger, LOG_BAD_SEVERITY).arg("info");
  60. // process should cause them to be logged
  61. if (do_process) {
  62. LoggerManager logger_manager;
  63. logger_manager.process();
  64. }
  65. return (0);
  66. }