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.

82 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. LogLog::getLogLog()->setInternalDebugging(true);
  27. {
  28. AppenderAttachableImpl aai;
  29. try {
  30. SharedObjectPtr<Appender> append_1(new ConsoleAppender());
  31. append_1->setName(LOG4CPLUS_TEXT("First"));
  32. SharedObjectPtr<Appender> append_2(new ConsoleAppender());
  33. append_2->setName(LOG4CPLUS_TEXT("Second"));
  34. InternalLoggingEvent event(
  35. Logger::getInstance(LOG4CPLUS_TEXT("test")).getName(),
  36. DEBUG_LOG_LEVEL, LOG4CPLUS_TEXT("This is a test..."), NULL, 0);
  37. aai.addAppender(append_1);
  38. aai.addAppender(append_2);
  39. aai.addAppender(append_1);
  40. aai.addAppender(append_2);
  41. printAppenderList(aai.getAllAppenders());
  42. aai.removeAppender(append_2);
  43. printAppenderList(aai.getAllAppenders());
  44. aai.removeAppender(LOG4CPLUS_TEXT("First"));
  45. printAppenderList(aai.getAllAppenders());
  46. aai.addAppender(append_1);
  47. aai.addAppender(append_2);
  48. aai.addAppender(append_1);
  49. aai.addAppender(append_2);
  50. log4cplus::tcout << "Should be First: "
  51. << aai.getAppender(LOG4CPLUS_TEXT("First"))->getName() << endl;
  52. log4cplus::tcout << "Should be Second: "
  53. << aai.getAppender(LOG4CPLUS_TEXT("Second"))->getName() << endl
  54. << endl;
  55. append_1->doAppend(event);
  56. append_2->doAppend(event);
  57. }
  58. catch(std::exception const & e) {
  59. log4cplus::tcout << "**** Exception occured: " << e.what() << endl;
  60. }
  61. // log4cplus::tcout << "*** Calling close()..." << endl;
  62. // append_2->close();
  63. // log4cplus::tcout << "*** Done calling close()..." << endl;
  64. }
  65. log4cplus::tcout << "Exiting main()..." << endl;
  66. return 0;
  67. }