1 // Copyright 2020 The Pigweed Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 4 // use this file except in compliance with the License. You may obtain a copy of 5 // the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 // License for the specific language governing permissions and limitations under 13 // the License. 14 15 #pragma once 16 17 // Enable traces 18 #define PW_TRACE_TYPE_INSTANT trace_fake_backend::Instantaneous 19 #define PW_TRACE_TYPE_INSTANT_GROUP trace_fake_backend::InstantaneousGroup 20 #define PW_TRACE_TYPE_DURATION_START trace_fake_backend::DurationStart 21 #define PW_TRACE_TYPE_DURATION_END trace_fake_backend::DurationEnd 22 23 #define PW_TRACE_TYPE_DURATION_GROUP_START \ 24 trace_fake_backend::DurationGroupStart 25 #define PW_TRACE_TYPE_DURATION_GROUP_END trace_fake_backend::DurationGroupEnd 26 27 #define PW_TRACE_TYPE_ASYNC_START trace_fake_backend::AsyncStart 28 #define PW_TRACE_TYPE_ASYNC_INSTANT trace_fake_backend::AsyncStep 29 #define PW_TRACE_TYPE_ASYNC_END trace_fake_backend::AsyncEnd 30 31 namespace trace_fake_backend { 32 33 typedef enum { 34 Invalid, 35 Instantaneous, 36 InstantaneousGroup, 37 AsyncStart, 38 AsyncStep, 39 AsyncEnd, 40 DurationStart, 41 DurationEnd, 42 DurationGroupStart, 43 DurationGroupEnd, 44 } pw_trace_EventType; 45 46 // Define a helper class for holding events and checking equality. 47 class Event { 48 public: Event()49 Event() 50 : event_type_(Invalid), 51 flags_(0), 52 label_(nullptr), 53 group_(nullptr), 54 trace_id_(0), 55 has_data_(false), 56 data_format_string_(nullptr), 57 data_(nullptr), 58 data_size_(0) {} Event(pw_trace_EventType event_type,uint8_t flags,const char * label,const char * group,uint32_t trace_id)59 Event(pw_trace_EventType event_type, 60 uint8_t flags, 61 const char* label, 62 const char* group, 63 uint32_t trace_id) 64 : event_type_(event_type), 65 flags_(flags), 66 label_(label), 67 group_(group), 68 trace_id_(trace_id), 69 has_data_(false), 70 data_format_string_(nullptr), 71 data_(nullptr), 72 data_size_(0) {} Event(pw_trace_EventType event_type,uint8_t flags,const char * label,const char * group,uint32_t trace_id,const char * data_type,const char * data,size_t size)73 Event(pw_trace_EventType event_type, 74 uint8_t flags, 75 const char* label, 76 const char* group, 77 uint32_t trace_id, 78 const char* data_type, 79 const char* data, 80 size_t size) 81 : event_type_(event_type), 82 flags_(flags), 83 label_(label), 84 group_(group), 85 trace_id_(trace_id), 86 has_data_(true), 87 data_format_string_(data_type), 88 data_(data), 89 data_size_(size) {} 90 bool operator==(const Event& rhs) const { 91 return event_type_ == rhs.event_type_ && // 92 flags_ == rhs.flags_ && // 93 label_ == rhs.label_ && // 94 group_ == rhs.group_ && trace_id_ == rhs.trace_id_ && // 95 has_data_ == rhs.has_data_ && // 96 data_format_string_ == rhs.data_format_string_ && // 97 data_size_ == rhs.data_size_ && // 98 (memcmp(data_, rhs.data_, data_size_) == 0); 99 } 100 IsEqualIgnoreLabel(const Event & rhs)101 bool IsEqualIgnoreLabel(const Event& rhs) const { 102 return event_type_ == rhs.event_type_ && // 103 flags_ == rhs.flags_ && // 104 group_ == rhs.group_ && trace_id_ == rhs.trace_id_ && // 105 has_data_ == rhs.has_data_ && // 106 data_format_string_ == rhs.data_format_string_ && // 107 data_size_ == rhs.data_size_ && // 108 (memcmp(data_, rhs.data_, data_size_) == 0); 109 } 110 111 private: 112 pw_trace_EventType event_type_; 113 uint8_t flags_; 114 const char* label_; 115 const char* group_; 116 uint32_t trace_id_; 117 118 bool has_data_; 119 const char* data_format_string_; 120 const char* data_; 121 size_t data_size_; 122 }; 123 124 class LastEvent { 125 public: Instance()126 static LastEvent& Instance() { return instance_; } Get()127 Event& Get() { return last_event_; } Set(const Event & event)128 void Set(const Event& event) { last_event_ = event; } 129 130 private: 131 Event last_event_{}; 132 static LastEvent instance_; 133 }; 134 135 #define PW_TRACE(event_type, flags, label, group, trace_id) \ 136 LastEvent::Instance().Set(Event(event_type, flags, label, group, trace_id)); 137 138 #define PW_TRACE_DATA( \ 139 event_type, flags, label, group, trace_id, type, data, size) \ 140 LastEvent::Instance().Set( \ 141 Event(event_type, flags, label, group, trace_id, type, data, size)); 142 143 } // namespace trace_fake_backend