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.

150 lines
4.0 KiB

  1. #include <log4cplus/consoleappender.h>
  2. #include <log4cplus/layout.h>
  3. #include <log4cplus/logger.h>
  4. #include <log4cplus/ndc.h>
  5. #include <log4cplus/helpers/loglog.h>
  6. #include <log4cplus/thread/threads.h>
  7. #include <log4cplus/helpers/sleep.h>
  8. #include <log4cplus/streams.h>
  9. #include <log4cplus/loggingmacros.h>
  10. #include <log4cplus/tracelogger.h>
  11. #include <exception>
  12. #include <iostream>
  13. #include <string>
  14. using namespace std;
  15. using namespace log4cplus;
  16. using namespace log4cplus::helpers;
  17. using namespace log4cplus::thread;
  18. #define MILLIS_TO_NANOS 1000
  19. #define NUM_THREADS 4
  20. #define NUM_LOOPS 10
  21. class SlowObject {
  22. public:
  23. SlowObject()
  24. : logger(Logger::getInstance(LOG4CPLUS_TEXT("SlowObject")))
  25. {
  26. logger.setLogLevel(TRACE_LOG_LEVEL);
  27. }
  28. void doSomething()
  29. {
  30. LOG4CPLUS_TRACE_METHOD(logger, LOG4CPLUS_TEXT("SlowObject::doSomething()"));
  31. {
  32. log4cplus::thread::MutexGuard guard (mutex);
  33. LOG4CPLUS_INFO(logger, LOG4CPLUS_TEXT("Actually doing something..."));
  34. sleep(0, 75 * MILLIS_TO_NANOS);
  35. LOG4CPLUS_INFO_FMT(logger,
  36. LOG4CPLUS_TEXT (
  37. "Actually doing something...%d, %d, %d, %ls...DONE"),
  38. 1, 2, 3, L"testing");
  39. }
  40. log4cplus::thread::yield();
  41. }
  42. ~SlowObject ()
  43. { }
  44. private:
  45. log4cplus::thread::Mutex mutex;
  46. Logger logger;
  47. };
  48. class TestThread : public AbstractThread {
  49. public:
  50. TestThread (tstring const & n, SlowObject * so)
  51. : name(n)
  52. , slow(so)
  53. , logger(Logger::getInstance(LOG4CPLUS_TEXT("test.TestThread")))
  54. { }
  55. virtual void run();
  56. private:
  57. tstring name;
  58. SlowObject * slow;
  59. Logger logger;
  60. };
  61. int
  62. main()
  63. {
  64. try
  65. {
  66. auto_ptr<SlowObject> slowObject(new SlowObject());
  67. log4cplus::helpers::LogLog::getLogLog()->setInternalDebugging(true);
  68. Logger logger = Logger::getInstance(LOG4CPLUS_TEXT("main"));
  69. Logger::getRoot().setLogLevel(INFO_LOG_LEVEL);
  70. LogLevel ll = logger.getLogLevel();
  71. tcout << "main Priority: " << getLogLevelManager().toString(ll) << endl;
  72. helpers::SharedObjectPtr<Appender> append_1(new ConsoleAppender());
  73. append_1->setLayout( std::auto_ptr<Layout>(new log4cplus::TTCCLayout()) );
  74. Logger::getRoot().addAppender(append_1);
  75. append_1->setName(LOG4CPLUS_TEXT("cout"));
  76. append_1 = 0;
  77. log4cplus::helpers::SharedObjectPtr<TestThread> threads[NUM_THREADS];
  78. int i = 0;
  79. for(i=0; i<NUM_THREADS; ++i) {
  80. tostringstream s;
  81. s << "Thread-" << i;
  82. threads[i] = new TestThread(s.str(), slowObject.get());
  83. }
  84. for(i=0; i<NUM_THREADS; ++i) {
  85. threads[i]->start();
  86. }
  87. LOG4CPLUS_DEBUG(logger, "All Threads started...");
  88. for(i=0; i<NUM_THREADS; ++i) {
  89. while(threads[i]->isRunning()) {
  90. sleep(0, 200 * MILLIS_TO_NANOS);
  91. }
  92. }
  93. LOG4CPLUS_INFO(logger, "Exiting main()...");
  94. }
  95. catch(std::exception &e) {
  96. LOG4CPLUS_FATAL(Logger::getRoot(), "main()- Exception occured: " << e.what());
  97. }
  98. catch(...) {
  99. LOG4CPLUS_FATAL(Logger::getRoot(), "main()- Exception occured");
  100. }
  101. log4cplus::Logger::shutdown();
  102. return 0;
  103. }
  104. void
  105. TestThread::run()
  106. {
  107. try {
  108. LOG4CPLUS_WARN(logger, name + LOG4CPLUS_TEXT(" TestThread.run()- Starting..."));
  109. NDC& ndc = getNDC();
  110. NDCContextCreator _first_ndc(name);
  111. LOG4CPLUS_DEBUG(logger, "Entering Run()...");
  112. for(int i=0; i<NUM_LOOPS; ++i) {
  113. NDCContextCreator _ndc(LOG4CPLUS_TEXT("loop"));
  114. slow->doSomething();
  115. }
  116. LOG4CPLUS_DEBUG(logger, "Exiting run()...");
  117. ndc.remove();
  118. }
  119. catch(std::exception const & e) {
  120. LOG4CPLUS_FATAL(logger, "TestThread.run()- Exception occurred: " << e.what());
  121. }
  122. catch(...) {
  123. LOG4CPLUS_FATAL(logger, "TestThread.run()- Exception occurred!!");
  124. }
  125. LOG4CPLUS_WARN(logger, name << " TestThread.run()- Finished");
  126. } // end "run"