/src/connectedhomeip/examples/common/tracing/TraceHandlers.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2021 Project CHIP Authors |
3 | | * All rights reserved. |
4 | | * |
5 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
6 | | * you may not use this file except in compliance with the License. |
7 | | * You may obtain a copy of the License at |
8 | | * |
9 | | * http://www.apache.org/licenses/LICENSE-2.0 |
10 | | * |
11 | | * Unless required by applicable law or agreed to in writing, software |
12 | | * distributed under the License is distributed on an "AS IS" BASIS, |
13 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 | | * See the License for the specific language governing permissions and |
15 | | * limitations under the License. |
16 | | * |
17 | | */ |
18 | | |
19 | | #pragma once |
20 | | |
21 | | #include <fstream> |
22 | | #include <iostream> |
23 | | #include <lib/support/logging/CHIPLogging.h> |
24 | | #include <string> |
25 | | #include <sys/time.h> |
26 | | |
27 | | namespace chip { |
28 | | namespace trace { |
29 | | |
30 | | class TraceStream |
31 | | { |
32 | | public: |
33 | 0 | virtual ~TraceStream() = default; |
34 | | virtual void StartEvent(const std::string & label) = 0; |
35 | | virtual void AddField(const std::string & tag, const std::string & data) = 0; |
36 | | virtual void FinishEvent() = 0; |
37 | | }; |
38 | | |
39 | | class TraceStreamLog : public TraceStream |
40 | | { |
41 | | public: |
42 | | void StartEvent(const std::string & label) override |
43 | 0 | { |
44 | 0 | mStreamLine = ">>>" + label + "<<<\t"; |
45 | 0 | mIsFirstField = true; |
46 | 0 | } |
47 | | |
48 | | void AddField(const std::string & tag, const std::string & data) override |
49 | 0 | { |
50 | 0 | mStreamLine += (mIsFirstField ? "" : "\t") + tag + "|" + data; |
51 | 0 | mIsFirstField = false; |
52 | 0 | } |
53 | | |
54 | 0 | void FinishEvent() override { ChipLogAutomation("TraceStream:%s", mStreamLine.c_str()); } |
55 | | |
56 | | protected: |
57 | | std::string mStreamLine; |
58 | | bool mIsFirstField = true; |
59 | | }; |
60 | | |
61 | | class TraceStreamFile : public TraceStream |
62 | | { |
63 | | public: |
64 | 0 | TraceStreamFile(const char * fileName) { mFile.open(fileName); } |
65 | 0 | ~TraceStreamFile() { mFile.close(); } |
66 | | |
67 | | void AddField(const std::string & tag, const std::string & data) override |
68 | 0 | { |
69 | 0 | if (mFile.is_open()) |
70 | 0 | { |
71 | 0 | mFile << " " << tag.data() << "\t" << data.data() << "\n"; |
72 | 0 | mFile.flush(); |
73 | 0 | } |
74 | 0 | } |
75 | | |
76 | | void StartEvent(const std::string & label) override |
77 | 0 | { |
78 | 0 | if (mFile.is_open()) |
79 | 0 | { |
80 | 0 | struct timeval tv; |
81 | 0 | gettimeofday(&tv, nullptr); |
82 | 0 | mFile << "\f[" << tv.tv_sec << "." << tv.tv_usec << "]\t" << label << "\n"; |
83 | 0 | mFile.flush(); |
84 | 0 | } |
85 | 0 | } |
86 | | |
87 | 0 | void FinishEvent() override {} |
88 | | |
89 | | private: |
90 | | std::ofstream mFile; |
91 | | }; |
92 | | |
93 | | void AddTraceStream(TraceStream * stream); |
94 | | void InitTrace(); |
95 | | void DeInitTrace(); |
96 | | |
97 | | } // namespace trace |
98 | | } // namespace chip |