From 0a057a0427b90b2ac4cae52a44d354a02ccda8e1 Mon Sep 17 00:00:00 2001 From: winlin Date: Mon, 13 Jul 2020 11:19:34 +0800 Subject: [PATCH] Log: Refine log context, use const --- trunk/src/kernel/srs_kernel_log.hpp | 6 +++--- trunk/src/protocol/srs_service_log.cpp | 16 ++++++++-------- trunk/src/protocol/srs_service_log.hpp | 4 ++-- trunk/src/utest/srs_utest_service.cpp | 6 +++--- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/trunk/src/kernel/srs_kernel_log.hpp b/trunk/src/kernel/srs_kernel_log.hpp index dbf149cf2..3d6122521 100644 --- a/trunk/src/kernel/srs_kernel_log.hpp +++ b/trunk/src/kernel/srs_kernel_log.hpp @@ -93,10 +93,10 @@ public: // @remark We do not set to current thread, user should do this. virtual SrsContextId generate_id() = 0; // Get the context id of current thread. - virtual SrsContextId get_id() = 0; + virtual const SrsContextId& get_id() = 0; // Set the context id of current thread. - // @return the previous context id. - virtual SrsContextId set_id(SrsContextId v) = 0; + // @return the current context id. + virtual const SrsContextId& set_id(const SrsContextId& v) = 0; }; // @global User must provides a log object diff --git a/trunk/src/protocol/srs_service_log.cpp b/trunk/src/protocol/srs_service_log.cpp index ca30daf2f..9d2384adc 100644 --- a/trunk/src/protocol/srs_service_log.cpp +++ b/trunk/src/protocol/srs_service_log.cpp @@ -49,22 +49,22 @@ SrsContextId SrsThreadContext::generate_id() return cid; } -SrsContextId SrsThreadContext::get_id() +const SrsContextId& SrsThreadContext::get_id() { return cache[srs_thread_self()]; } -SrsContextId SrsThreadContext::set_id(SrsContextId v) +const SrsContextId& SrsThreadContext::set_id(const SrsContextId& v) { srs_thread_t self = srs_thread_self(); - - SrsContextId ov; - if (cache.find(self) != cache.end()) { - ov = cache[self]; + + if (cache.find(self) == cache.end()) { + cache[self] = v; + return v; } - + + const SrsContextId& ov = cache[self]; cache[self] = v; - return ov; } diff --git a/trunk/src/protocol/srs_service_log.hpp b/trunk/src/protocol/srs_service_log.hpp index 656723e64..b441dc3db 100644 --- a/trunk/src/protocol/srs_service_log.hpp +++ b/trunk/src/protocol/srs_service_log.hpp @@ -43,8 +43,8 @@ public: virtual ~SrsThreadContext(); public: virtual SrsContextId generate_id(); - virtual SrsContextId get_id(); - virtual SrsContextId set_id(SrsContextId v); + virtual const SrsContextId& get_id(); + virtual const SrsContextId& set_id(const SrsContextId& v); public: virtual void clear_cid(); }; diff --git a/trunk/src/utest/srs_utest_service.cpp b/trunk/src/utest/srs_utest_service.cpp index ab5250422..9f075e477 100644 --- a/trunk/src/utest/srs_utest_service.cpp +++ b/trunk/src/utest/srs_utest_service.cpp @@ -1382,12 +1382,12 @@ VOID TEST(TCPServerTest, ContextUtility) if (true) { SrsThreadContext ctx; - EXPECT_TRUE(ctx.set_id(SrsContextId("100")).empty()); - EXPECT_TRUE(!ctx.set_id(SrsContextId("1000")).compare(SrsContextId("100"))); + EXPECT_TRUE(!ctx.set_id(SrsContextId("100")).compare(SrsContextId("100"))); + EXPECT_TRUE(!ctx.set_id(SrsContextId("1000")).compare(SrsContextId("1000"))); EXPECT_TRUE(!ctx.get_id().compare(SrsContextId("1000"))); ctx.clear_cid(); - EXPECT_TRUE(ctx.set_id(SrsContextId("100")).empty()); + EXPECT_TRUE(!ctx.set_id(SrsContextId("100")).compare(SrsContextId("100"))); } int base_size = 0;