You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.6 KiB

  1. #include <exception>
  2. #include <iostream>
  3. #include <string>
  4. #include "log4cplus/logger.h"
  5. #include "log4cplus/consoleappender.h"
  6. #include "log4cplus/helpers/appenderattachableimpl.h"
  7. #include "log4cplus/helpers/loglog.h"
  8. #include "log4cplus/helpers/pointer.h"
  9. #include "log4cplus/spi/loggingevent.h"
  10. using namespace std;
  11. using namespace log4cplus;
  12. using namespace log4cplus::helpers;
  13. using namespace log4cplus::spi;
  14. void
  15. printAppenderList(SharedAppenderPtrList list)
  16. {
  17. cout << "List size: " << list.size() << endl;
  18. for(SharedAppenderPtrList::iterator it=list.begin(); it!=list.end(); ++it) {
  19. log4cplus::tcout << "Loop Body: Appender name = " << (*it)->getName()
  20. << endl;
  21. }
  22. }
  23. int
  24. main()
  25. {
  26. log4cplus::initialize ();
  27. LogLog::getLogLog()->setInternalDebugging(true);
  28. {
  29. AppenderAttachableImpl aai;
  30. try {
  31. SharedObjectPtr<Appender> append_1(new ConsoleAppender());
  32. append_1->setName(LOG4CPLUS_TEXT("First"));
  33. SharedObjectPtr<Appender> append_2(new ConsoleAppender());
  34. append_2->setName(LOG4CPLUS_TEXT("Second"));
  35. InternalLoggingEvent event(
  36. Logger::getInstance(LOG4CPLUS_TEXT("test")).getName(),
  37. DEBUG_LOG_LEVEL, LOG4CPLUS_TEXT("This is a test..."), NULL, 0);
  38. aai.addAppender(append_1);
  39. aai.addAppender(append_2);
  40. aai.addAppender(append_1);
  41. aai.addAppender(append_2);
  42. printAppenderList(aai.getAllAppenders());
  43. aai.removeAppender(append_2);
  44. printAppenderList(aai.getAllAppenders());
  45. aai.removeAppender(LOG4CPLUS_TEXT("First"));
  46. printAppenderList(aai.getAllAppenders());
  47. aai.addAppender(append_1);
  48. aai.addAppender(append_2);
  49. aai.addAppender(append_1);
  50. aai.addAppender(append_2);
  51. log4cplus::tcout << "Should be First: "
  52. << aai.getAppender(LOG4CPLUS_TEXT("First"))->getName() << endl;
  53. log4cplus::tcout << "Should be Second: "
  54. << aai.getAppender(LOG4CPLUS_TEXT("Second"))->getName() << endl
  55. << endl;
  56. append_1->doAppend(event);
  57. append_2->doAppend(event);
  58. }
  59. catch(std::exception const & e) {
  60. log4cplus::tcout << "**** Exception occured: " << e.what() << endl;
  61. }
  62. // log4cplus::tcout << "*** Calling close()..." << endl;
  63. // append_2->close();
  64. // log4cplus::tcout << "*** Done calling close()..." << endl;
  65. }
  66. log4cplus::tcout << "Exiting main()..." << endl;
  67. return 0;
  68. }