1 // Copyright 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "platform/impl/text_trace_logging_platform.h"
6 
7 #include <limits>
8 #include <sstream>
9 
10 #include "util/chrono_helpers.h"
11 #include "util/osp_logging.h"
12 
13 namespace openscreen {
14 
IsTraceLoggingEnabled(TraceCategory::Value category)15 bool TextTraceLoggingPlatform::IsTraceLoggingEnabled(
16     TraceCategory::Value category) {
17   constexpr uint64_t kAllLogCategoriesMask =
18       std::numeric_limits<uint64_t>::max();
19   return (kAllLogCategoriesMask & category) != 0;
20 }
21 
TextTraceLoggingPlatform()22 TextTraceLoggingPlatform::TextTraceLoggingPlatform() {
23   StartTracing(this);
24 }
25 
~TextTraceLoggingPlatform()26 TextTraceLoggingPlatform::~TextTraceLoggingPlatform() {
27   StopTracing();
28 }
29 
LogTrace(const char * name,const uint32_t line,const char * file,Clock::time_point start_time,Clock::time_point end_time,TraceIdHierarchy ids,Error::Code error)30 void TextTraceLoggingPlatform::LogTrace(const char* name,
31                                         const uint32_t line,
32                                         const char* file,
33                                         Clock::time_point start_time,
34                                         Clock::time_point end_time,
35                                         TraceIdHierarchy ids,
36                                         Error::Code error) {
37   auto total_runtime = to_microseconds(end_time - start_time).count();
38   constexpr auto microseconds_symbol = "\u03BCs";  // Greek Mu + 's'
39   std::stringstream ss;
40   ss << "TRACE [" << std::hex << ids.root << ":" << ids.parent << ":"
41      << ids.current << "] (" << std::dec << total_runtime << microseconds_symbol
42      << ") " << name << "<" << file << ":" << line << "> " << error;
43 
44   OSP_LOG_INFO << ss.str();
45 }
46 
LogAsyncStart(const char * name,const uint32_t line,const char * file,Clock::time_point timestamp,TraceIdHierarchy ids)47 void TextTraceLoggingPlatform::LogAsyncStart(const char* name,
48                                              const uint32_t line,
49                                              const char* file,
50                                              Clock::time_point timestamp,
51                                              TraceIdHierarchy ids) {
52   std::stringstream ss;
53   ss << "ASYNC TRACE START [" << std::hex << ids.root << ":" << ids.parent
54      << ":" << ids.current << std::dec << "] (" << timestamp << ") " << name
55      << "<" << file << ":" << line << ">";
56 
57   OSP_LOG_INFO << ss.str();
58 }
59 
LogAsyncEnd(const uint32_t line,const char * file,Clock::time_point timestamp,TraceId trace_id,Error::Code error)60 void TextTraceLoggingPlatform::LogAsyncEnd(const uint32_t line,
61                                            const char* file,
62                                            Clock::time_point timestamp,
63                                            TraceId trace_id,
64                                            Error::Code error) {
65   OSP_LOG_INFO << "ASYNC TRACE END [" << std::hex << trace_id << std::dec
66                << "] (" << timestamp << ") " << error;
67 }
68 
69 }  // namespace openscreen
70