logger_support_test.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright (C) 2011 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. /// \brief Example Program
  15. ///
  16. /// Simple example program showing how to use the logger.
  17. #include <stdlib.h>
  18. #include <unistd.h>
  19. #include <string.h>
  20. #include <iostream>
  21. #include <log/logger.h>
  22. #include <log/macros.h>
  23. #include <log/logger_support.h>
  24. #include <log/root_logger_name.h>
  25. // Include a set of message definitions.
  26. #include <log/messagedef.h>
  27. using namespace isc::log;
  28. // Declare logger to use an example.
  29. Logger logger_ex("example");
  30. // The program is invoked:
  31. //
  32. // logger_support_test [-s severity] [-d level ] [local_file]
  33. //
  34. // "severity" is one of "debug", "info", "warn", "error", "fatal"
  35. // "level" is the debug level, a number between 0 and 99
  36. // "local_file" is the name of a local file.
  37. //
  38. // The program sets the attributes on the root logger and logs a set of
  39. // messages. Looking at the output determines whether the program worked.
  40. int main(int argc, char** argv) {
  41. isc::log::Severity severity = isc::log::INFO; // Default logger severity
  42. int dbglevel = -1; // Logger debug level
  43. const char* localfile = NULL; // Local message file
  44. int option; // For getopt() processing
  45. Logger logger_dlm("dlm", true); // Another example logger
  46. // Parse options
  47. while ((option = getopt(argc, argv, "s:d:")) != -1) {
  48. switch (option) {
  49. case 's':
  50. if (strcmp(optarg, "debug") == 0) {
  51. severity = isc::log::DEBUG;
  52. } else if (strcmp(optarg, "info") == 0) {
  53. severity = isc::log::INFO;
  54. } else if (strcmp(optarg, "warn") == 0) {
  55. severity = isc::log::WARN;
  56. } else if (strcmp(optarg, "error") == 0) {
  57. severity = isc::log::ERROR;
  58. } else if (strcmp(optarg, "fatal") == 0) {
  59. severity = isc::log::FATAL;
  60. } else {
  61. std::cout << "Unrecognised severity option: " <<
  62. optarg << "\n";
  63. exit(1);
  64. }
  65. break;
  66. case 'd':
  67. dbglevel = atoi(optarg);
  68. break;
  69. default:
  70. std::cout << "Unrecognised option: " <<
  71. static_cast<char>(option) << "\n";
  72. }
  73. }
  74. if (optind < argc) {
  75. localfile = argv[optind];
  76. }
  77. // Update the logging parameters
  78. initLogger("alpha", severity, dbglevel, localfile);
  79. // Log a few messages
  80. LOG_FATAL(logger_ex, MSG_MSGWRTERR).arg("test1").arg("42");
  81. LOG_ERROR(logger_ex, MSG_UNRECDIR).arg("false");
  82. LOG_WARN(logger_dlm, MSG_MSGRDERR).arg("a.txt").arg("dummy test");
  83. LOG_INFO(logger_dlm, MSG_OPNMSGIN).arg("example.msg").arg("dummy test");
  84. LOG_DEBUG(logger_ex, 0, MSG_UNRECDIR).arg("[abc]");
  85. LOG_DEBUG(logger_ex, 24, MSG_UNRECDIR).arg("[24]");
  86. LOG_DEBUG(logger_ex, 25, MSG_UNRECDIR).arg("[25]");
  87. LOG_DEBUG(logger_ex, 26, MSG_UNRECDIR).arg("[26]");
  88. return (0);
  89. }