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.

151 lines
4.1 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. log4cplus::initialize();
  65. try
  66. {
  67. auto_ptr<SlowObject> slowObject(new SlowObject());
  68. log4cplus::helpers::LogLog::getLogLog()->setInternalDebugging(true);
  69. Logger logger = Logger::getInstance(LOG4CPLUS_TEXT("main"));
  70. Logger::getRoot().setLogLevel(INFO_LOG_LEVEL);
  71. LogLevel ll = logger.getLogLevel();
  72. tcout << "main Priority: " << getLogLevelManager().toString(ll) << endl;
  73. helpers::SharedObjectPtr<Appender> append_1(new ConsoleAppender());
  74. append_1->setLayout( std::auto_ptr<Layout>(new log4cplus::TTCCLayout()) );
  75. Logger::getRoot().addAppender(append_1);
  76. append_1->setName(LOG4CPLUS_TEXT("cout"));
  77. append_1 = 0;
  78. log4cplus::helpers::SharedObjectPtr<TestThread> threads[NUM_THREADS];
  79. int i = 0;
  80. for(i=0; i<NUM_THREADS; ++i) {
  81. tostringstream s;
  82. s << "Thread-" << i;
  83. threads[i] = new TestThread(s.str(), slowObject.get());
  84. }
  85. for(i=0; i<NUM_THREADS; ++i) {
  86. threads[i]->start();
  87. }
  88. LOG4CPLUS_DEBUG(logger, "All Threads started...");
  89. for(i=0; i<NUM_THREADS; ++i) {
  90. while(threads[i]->isRunning()) {
  91. sleep(0, 200 * MILLIS_TO_NANOS);
  92. }
  93. }
  94. LOG4CPLUS_INFO(logger, "Exiting main()...");
  95. }
  96. catch(std::exception &e) {
  97. LOG4CPLUS_FATAL(Logger::getRoot(), "main()- Exception occured: " << e.what());
  98. }
  99. catch(...) {
  100. LOG4CPLUS_FATAL(Logger::getRoot(), "main()- Exception occured");
  101. }
  102. log4cplus::Logger::shutdown();
  103. return 0;
  104. }
  105. void
  106. TestThread::run()
  107. {
  108. try {
  109. LOG4CPLUS_WARN(logger, name + LOG4CPLUS_TEXT(" TestThread.run()- Starting..."));
  110. NDC& ndc = getNDC();
  111. NDCContextCreator _first_ndc(name);
  112. LOG4CPLUS_DEBUG(logger, "Entering Run()...");
  113. for(int i=0; i<NUM_LOOPS; ++i) {
  114. NDCContextCreator _ndc(LOG4CPLUS_TEXT("loop"));
  115. slow->doSomething();
  116. }
  117. LOG4CPLUS_DEBUG(logger, "Exiting run()...");
  118. ndc.remove();
  119. }
  120. catch(std::exception const & e) {
  121. LOG4CPLUS_FATAL(logger, "TestThread.run()- Exception occurred: " << e.what());
  122. }
  123. catch(...) {
  124. LOG4CPLUS_FATAL(logger, "TestThread.run()- Exception occurred!!");
  125. }
  126. LOG4CPLUS_WARN(logger, name << " TestThread.run()- Finished");
  127. } // end "run"