Jordan Bayles | 20f3df6 | 2022-11-10 15:31:38 -0800 | [diff] [blame^] | 1 | // Copyright 2022 The Chromium Authors |
| 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/api/trace_event.h" |
| 6 | |
| 7 | #include <sstream> |
| 8 | |
| 9 | namespace openscreen { |
| 10 | |
| 11 | TraceEvent::TraceEvent(TraceCategory category, |
| 12 | Clock::time_point start_time, |
| 13 | const char* name, |
| 14 | const char* file_name, |
| 15 | uint32_t line_number) |
| 16 | : category(category), |
| 17 | start_time(start_time), |
| 18 | name(name), |
| 19 | file_name(file_name), |
| 20 | line_number(line_number) {} |
| 21 | |
| 22 | TraceEvent::TraceEvent() = default; |
| 23 | TraceEvent::TraceEvent(TraceEvent&&) noexcept = default; |
| 24 | TraceEvent::TraceEvent(const TraceEvent&) = default; |
| 25 | TraceEvent& TraceEvent::operator=(TraceEvent&&) = default; |
| 26 | TraceEvent& TraceEvent::operator=(const TraceEvent&) = default; |
| 27 | TraceEvent::~TraceEvent() = default; |
| 28 | |
| 29 | std::string TraceEvent::ToString() const { |
| 30 | std::ostringstream oss; |
| 31 | |
| 32 | oss << ids << " " << openscreen::ToString(category) << "::" << name << " <" |
| 33 | << file_name << ":" << line_number << ">"; |
| 34 | |
| 35 | // We only support two arguments in total. |
| 36 | if (!arguments.empty()) { |
| 37 | oss << " { " << arguments[0].first << ": " << arguments[0].second; |
| 38 | if (arguments.size() > 1) { |
| 39 | oss << ", " << arguments[1].first << ": " << arguments[1].second; |
| 40 | } |
| 41 | oss << " }"; |
| 42 | } |
| 43 | return oss.str(); |
| 44 | } |
| 45 | |
| 46 | void TraceEvent::TruncateStrings() { |
| 47 | for (auto& argument : arguments) { |
| 48 | if (argument.second.size() > kMaxStringLength) { |
| 49 | argument.second.resize(kMaxStringLength); |
| 50 | // Populate last three digits with ellipses to indicate that |
| 51 | // we truncated this string. |
| 52 | argument.second.replace(kMaxStringLength - 3, 3, "..."); |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | } // namespace openscreen |