From b43cedacc9a613519aed4c64a91b29686c74c874 Mon Sep 17 00:00:00 2001 From: hevake_lcj Date: Mon, 25 Jul 2022 09:51:06 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9util/TimeCounter=EF=BC=8C?= =?UTF-8?q?=E5=B0=86=E5=85=B6=E8=BE=93=E5=87=BA=E6=89=93=E5=8D=B0=E6=94=B9?= =?UTF-8?q?=E6=88=90cout=EF=BC=8C=E5=B9=B6=E8=A7=A3=E5=86=B3=E5=85=B6?= =?UTF-8?q?=E5=9C=A8=E5=90=8C=E4=B8=80=E5=9F=9F=E4=B8=AD=E5=A4=9A=E6=AC=A1?= =?UTF-8?q?=E4=BD=BF=E7=94=A8SetTimeCountr()=E5=BC=95=E8=B5=B7=E7=9A=84?= =?UTF-8?q?=E7=BC=96=E8=AF=91=E9=94=99=E8=AF=AF=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- base/scope_exit_test.cpp | 10 ++++++++++ util/time_counter.cpp | 5 +++-- util/time_counter.h | 3 ++- util/time_counter_test.cpp | 1 + 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/base/scope_exit_test.cpp b/base/scope_exit_test.cpp index 08ae702..0342dbc 100644 --- a/base/scope_exit_test.cpp +++ b/base/scope_exit_test.cpp @@ -14,6 +14,16 @@ TEST(ScopeExitAction, no_name) EXPECT_TRUE(tag); } +TEST(ScopeExitAction, no_name_1) +{ + int count = 3; + { + SetScopeExitAction([&] { ++count; }); + SetScopeExitAction([&] { count *= 2; }); + } + EXPECT_EQ(count, 7); +} + TEST(ScopeExitAction, named) { bool tag = false; diff --git a/util/time_counter.cpp b/util/time_counter.cpp index f6ef3b1..b850df9 100644 --- a/util/time_counter.cpp +++ b/util/time_counter.cpp @@ -1,9 +1,10 @@ #include "time_counter.h" -#include +#include namespace tbox { namespace util { +using namespace std; using namespace std::chrono; TimeCounter::TimeCounter(const char *file_name, const char *func_name, int line) : @@ -27,7 +28,7 @@ void TimeCounter::stop() return; auto cost = now - start_time_point_; - LogPrintfFunc("TC", func_name_, file_name_, line_, LOG_LEVEL_TRACE, true, "timecost: %llu ns", cost.count()); + cout << "TimeCounter at " << file_name_ << ",L" << line_ << " in " << func_name_ << "(): " << cost.count() << " ns" << endl; stoped_ = true; } diff --git a/util/time_counter.h b/util/time_counter.h index 0dd717f..87e2713 100644 --- a/util/time_counter.h +++ b/util/time_counter.h @@ -26,7 +26,8 @@ class TimeCounter { } //! 无名计时器,用行号命名 -#define _TimeCounter_0(file,func,line) tbox::util::TimeCounter _timer_counter_at_##line(file,func,line) +#define _TimeCounter_1(file,func,line) tbox::util::TimeCounter _timer_counter_at_##line(file,func,line) +#define _TimeCounter_0(file,func,line) _TimeCounter_1(file,func,line) #define SetTimeCounter() _TimeCounter_0(__FILE__, __func__, __LINE__) //! 有名计时器 diff --git a/util/time_counter_test.cpp b/util/time_counter_test.cpp index 5418912..3ea9a95 100644 --- a/util/time_counter_test.cpp +++ b/util/time_counter_test.cpp @@ -10,6 +10,7 @@ TEST(TimeCounter, basic) SetTimeCounter(); std::this_thread::sleep_for(std::chrono::seconds(1)); + SetTimeCounter(); } TEST(TimeCounter, stop) -- Gitee