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.

94 lines
3.1KB

  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 <iostream>
  11. #include "Util/logger.h"
  12. #include "Network/Socket.h"
  13. using namespace std;
  14. using namespace toolkit;
  15. class TestLog
  16. {
  17. public:
  18. template<typename T>
  19. TestLog(const T &t){
  20. _ss << t;
  21. };
  22. ~TestLog(){};
  23. //通过此友元方法,可以打印自定义数据类型
  24. friend ostream& operator<<(ostream& out,const TestLog& obj){
  25. return out << obj._ss.str();
  26. }
  27. private:
  28. stringstream _ss;
  29. };
  30. int main() {
  31. //初始化日志系统
  32. Logger::Instance().add(std::make_shared<ConsoleChannel> ());
  33. Logger::Instance().add(std::make_shared<FileChannel>());
  34. Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
  35. InfoL << "测试std::cout风格打印:";
  36. //ostream支持的数据类型都支持,可以通过友元的方式打印自定义类型数据
  37. TraceL << "object int"<< TestLog((int)1) << endl;
  38. DebugL << "object short:"<<TestLog((short)2) << endl;
  39. InfoL << "object float:" << TestLog((float)3.12345678) << endl;
  40. WarnL << "object double:" << TestLog((double)4.12345678901234567) << endl;
  41. ErrorL << "object void *:" << TestLog((void *)0x12345678) << endl;
  42. ErrorL << "object string:" << TestLog("test string") << endl;
  43. //这是ostream原生支持的数据类型
  44. TraceL << "int"<< (int)1 << endl;
  45. DebugL << "short:"<< (short)2 << endl;
  46. InfoL << "float:" << (float)3.12345678 << endl;
  47. WarnL << "double:" << (double)4.12345678901234567 << endl;
  48. ErrorL << "void *:" << (void *)0x12345678 << endl;
  49. //根据RAII的原理,此处不需要输入 endl,也会在被函数栈pop时打印log
  50. ErrorL << "without endl!";
  51. PrintI("测试printf风格打印:");
  52. PrintT("this is a %s test:%d", "printf trace", 124);
  53. PrintD("this is a %s test:%p", "printf debug", (void*)124);
  54. PrintI("this is a %s test:%c", "printf info", 'a');
  55. PrintW("this is a %s test:%X", "printf warn", 0x7F);
  56. PrintE("this is a %s test:%x", "printf err", 0xab);
  57. LogI("测试可变长度模板样式打印:");
  58. LogT(1, "+", "2", '=', 3);
  59. LogD(1, "+", "2", '=', 3);
  60. LogI(1, "+", "2", '=', 3);
  61. LogW(1, "+", "2", '=', 3);
  62. LogE(1, "+", "2", '=', 3);
  63. for (int i = 0; i < 2; ++i) {
  64. DebugL << "this is a repeat 2 times log";
  65. this_thread::sleep_for(chrono::milliseconds(10));
  66. }
  67. for (int i = 0; i < 3; ++i) {
  68. DebugL << "this is a repeat 3 times log";
  69. this_thread::sleep_for(chrono::milliseconds(10));
  70. }
  71. for (int i = 0; i < 100; ++i) {
  72. DebugL << "this is a repeat 100 log";
  73. this_thread::sleep_for(chrono::milliseconds(10));
  74. }
  75. toolkit::SockException ex((ErrCode)1, "test");
  76. DebugL << "sock exception: " << ex;
  77. InfoL << "done!";
  78. return 0;
  79. }