From e52dbaf998c212364f92b87c36b036541a722118 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sat, 2 Mar 2019 14:25:50 -0500 Subject: [PATCH 1/2] logging/backend: Move CreateEntry into the Impl class This function is only ever used within this source file and makes it easier to remove static state in the following change. --- src/common/logging/backend.cpp | 51 +++++++++++++++++----------------- src/common/logging/backend.h | 4 --- 2 files changed, 26 insertions(+), 29 deletions(-) diff --git a/src/common/logging/backend.cpp b/src/common/logging/backend.cpp index b369f199f3..555addb959 100644 --- a/src/common/logging/backend.cpp +++ b/src/common/logging/backend.cpp @@ -39,8 +39,10 @@ public: Impl(Impl const&) = delete; const Impl& operator=(Impl const&) = delete; - void PushEntry(Entry e) { - message_queue.Push(std::move(e)); + void PushEntry(Class log_class, Level log_level, const char* filename, unsigned int line_num, + const char* function, std::string message) { + message_queue.Push( + CreateEntry(log_class, log_level, filename, line_num, function, std::move(message))); } void AddBackend(std::unique_ptr backend) { @@ -108,6 +110,26 @@ private: backend_thread.join(); } + Entry CreateEntry(Class log_class, Level log_level, const char* filename, unsigned int line_nr, + const char* function, std::string message) const { + using std::chrono::duration_cast; + using std::chrono::steady_clock; + + static steady_clock::time_point time_origin = steady_clock::now(); + + Entry entry; + entry.timestamp = + duration_cast(steady_clock::now() - time_origin); + entry.log_class = log_class; + entry.log_level = log_level; + entry.filename = Common::TrimSourcePath(filename); + entry.line_num = line_nr; + entry.function = function; + entry.message = std::move(message); + + return entry; + } + std::mutex writing_mutex; std::thread backend_thread; std::vector> backends; @@ -271,25 +293,6 @@ const char* GetLevelName(Level log_level) { #undef LVL } -Entry CreateEntry(Class log_class, Level log_level, const char* filename, unsigned int line_nr, - const char* function, std::string message) { - using std::chrono::duration_cast; - using std::chrono::steady_clock; - - static steady_clock::time_point time_origin = steady_clock::now(); - - Entry entry; - entry.timestamp = duration_cast(steady_clock::now() - time_origin); - entry.log_class = log_class; - entry.log_level = log_level; - entry.filename = Common::TrimSourcePath(filename); - entry.line_num = line_nr; - entry.function = function; - entry.message = std::move(message); - - return entry; -} - void SetGlobalFilter(const Filter& filter) { Impl::Instance().SetGlobalFilter(filter); } @@ -314,9 +317,7 @@ void FmtLogMessageImpl(Class log_class, Level log_level, const char* filename, if (!filter.CheckMessage(log_class, log_level)) return; - Entry entry = - CreateEntry(log_class, log_level, filename, line_num, function, fmt::vformat(format, args)); - - instance.PushEntry(std::move(entry)); + instance.PushEntry(log_class, log_level, filename, line_num, function, + fmt::vformat(format, args)); } } // namespace Log diff --git a/src/common/logging/backend.h b/src/common/logging/backend.h index a31ee69688..fca0267a1d 100644 --- a/src/common/logging/backend.h +++ b/src/common/logging/backend.h @@ -135,10 +135,6 @@ const char* GetLogClassName(Class log_class); */ const char* GetLevelName(Level log_level); -/// Creates a log entry by formatting the given source location, and message. -Entry CreateEntry(Class log_class, Level log_level, const char* filename, unsigned int line_nr, - const char* function, std::string message); - /** * The global filter will prevent any messages from even being processed if they are filtered. Each * backend can have a filter, but if the level is lower than the global filter, the backend will From 40167af31d5693d3d93a5873ae9788eed3a1d2a6 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sat, 2 Mar 2019 14:28:58 -0500 Subject: [PATCH 2/2] logging/backend: Make time_origin a class variable instead of a local static Moves local global state into the Impl class itself and initializes it at the creation of the instance instead of in the function. This makes it nicer for weakly-ordered architectures, given the CreateEntry() class won't need to have atomic loads executed for each individual call to the CreateEntry class. --- src/common/logging/backend.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/common/logging/backend.cpp b/src/common/logging/backend.cpp index 555addb959..4462ff3fbd 100644 --- a/src/common/logging/backend.cpp +++ b/src/common/logging/backend.cpp @@ -115,8 +115,6 @@ private: using std::chrono::duration_cast; using std::chrono::steady_clock; - static steady_clock::time_point time_origin = steady_clock::now(); - Entry entry; entry.timestamp = duration_cast(steady_clock::now() - time_origin); @@ -135,6 +133,7 @@ private: std::vector> backends; Common::MPSCQueue message_queue; Filter filter; + std::chrono::steady_clock::time_point time_origin{std::chrono::steady_clock::now()}; }; void ConsoleBackend::Write(const Entry& entry) {