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.

52 lines
1.5KB

  1. /*
  2. * Copyright (c) 2016 The ZLToolKit project authors. All Rights Reserved.
  3. *
  4. * This file is part of ZLToolKit(https://github.com/ZLMediaKit/ZLToolKit).
  5. *
  6. * Use of this source code is governed by MIT license that can be found in the
  7. * LICENSE file in the root of the source tree. All contributing project authors
  8. * may be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include <csignal>
  11. #include <iostream>
  12. #include "Util/util.h"
  13. #include "Util/logger.h"
  14. #include "Util/TimeTicker.h"
  15. #include "Poller/Timer.h"
  16. using namespace std;
  17. using namespace toolkit;
  18. int main() {
  19. //设置日志
  20. Logger::Instance().add(std::make_shared<ConsoleChannel>());
  21. Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
  22. Ticker ticker0;
  23. Timer::Ptr timer0 = std::make_shared<Timer>(0.5f,[&](){
  24. TraceL << "timer0重复:" << ticker0.elapsedTime();
  25. ticker0.resetTime();
  26. return true;
  27. }, nullptr);
  28. Timer::Ptr timer1 = std::make_shared<Timer>(1.0f,[](){
  29. DebugL << "timer1不再重复";
  30. return false;
  31. },nullptr);
  32. Ticker ticker2;
  33. Timer::Ptr timer2 = std::make_shared<Timer>(2.0f,[&]() -> bool {
  34. InfoL << "timer2,测试任务中抛异常" << ticker2.elapsedTime();
  35. ticker2.resetTime();
  36. throw std::runtime_error("timer2,测试任务中抛异常");
  37. },nullptr);
  38. //退出程序事件处理
  39. static semaphore sem;
  40. signal(SIGINT, [](int) { sem.post(); });// 设置退出信号
  41. sem.wait();
  42. return 0;
  43. }