example_tc_log.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /**
  2. * Tencent is pleased to support the open source community by making Tars available.
  3. *
  4. * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
  5. *
  6. * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  7. * in compliance with the License. You may obtain a copy of the License at
  8. *
  9. * https://opensource.org/licenses/BSD-3-Clause
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed
  12. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations under the License.
  15. */
  16. #include "util/tc_logger.h"
  17. using namespace tars;
  18. ostream& display(ostream& os)
  19. {
  20. // cout << &os << endl;
  21. os << "display" << endl;
  22. return os;
  23. }
  24. TC_LoggerThreadGroup group;
  25. TC_RollLogger logger;
  26. TC_DayLogger dlogger;
  27. #define DT \
  28. n1 = TC_Common::now2ms(); \
  29. t = n1 - n; \
  30. n = n1;
  31. #define OT if(t > 2) cout << i << ":" << t << endl;
  32. void test(){ofstream os; os << "abc";};
  33. void RollTest( )
  34. {
  35. int i = 1000000;
  36. while(i)
  37. {
  38. /*
  39. int64_t n = TC_Common::now2ms();
  40. int64_t n1=TC_Common::now2ms();
  41. int64_t t =TC_Common::now2ms();
  42. */
  43. // t = TC_Common::now2ms() - t;
  44. // logger.info() << "|" << i << "|" << t << endl;
  45. // DT;
  46. // ostringstream os;
  47. // test();
  48. // display(logger.debug());
  49. // logger.info();
  50. logger.debug() << "|" << i << "|" << endl;
  51. // OT;
  52. /*
  53. DT;
  54. logger.warn() << "|" << i << "|" << t << endl;
  55. OT;
  56. DT;
  57. logger.error() << "|" << i << "|" << t << endl;
  58. OT;
  59. */
  60. --i;
  61. }
  62. }
  63. void RollTestThread()
  64. {
  65. logger.init("./test", 1024000, 10);
  66. logger.modFlag(TC_RollLogger::HAS_LEVEL | TC_RollLogger::HAS_PID, true);
  67. logger.setupThread(&group);
  68. cout << TC_Common::now2str() << endl;
  69. TC_ThreadPool tpoolA;
  70. tpoolA.init(10);
  71. tpoolA.start();
  72. {
  73. for(size_t i = 0; i < tpoolA.getThreadNum(); i++)
  74. {
  75. tpoolA.exec(RollTest);
  76. }
  77. }
  78. tpoolA.waitForAllDone();
  79. cout << TC_Common::now2str() << endl;
  80. }
  81. void DayTest( )
  82. {
  83. int i = 10000000;
  84. while(i)
  85. {
  86. dlogger.any() << i << endl;
  87. --i;
  88. }
  89. }
  90. void DayTestThread()
  91. {
  92. dlogger.init("./test", "%Y%m%d%H%M");
  93. dlogger.setupThread(&group);
  94. cout << TC_Common::now2str() << endl;
  95. TC_ThreadPool tpoolA;
  96. tpoolA.init(5);
  97. tpoolA.start();
  98. {
  99. for(size_t i = 0; i < tpoolA.getThreadNum(); i++)
  100. {
  101. tpoolA.exec(DayTest);
  102. }
  103. }
  104. tpoolA.waitForAllDone();
  105. cout << TC_Common::now2str() << endl;
  106. }
  107. int main(int argc, char *argv[])
  108. {
  109. try
  110. {
  111. group.start(3);
  112. RollTestThread();
  113. // DayTestThread();
  114. }
  115. catch(exception &ex)
  116. {
  117. cout << ex.what() << endl;
  118. }
  119. return 0;
  120. }