• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://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,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "texgine/utils/trace.h"
17 
18 #include <chrono>
19 #include <cstdio>
20 #include <list>
21 #include <string>
22 
23 namespace OHOS {
24 namespace Rosen {
25 namespace TextEngine {
26 class TraceContent {
27 public:
GetInstance()28     static TraceContent &GetInstance()
29     {
30         static TraceContent tc;
31         return tc;
32     }
33 
~TraceContent()34     ~TraceContent()
35     {
36         auto fp = fopen("texgine.trace", "w");
37         if (fp == nullptr) {
38             return;
39         }
40 
41         fprintf(fp, "# tracer: nop\n");
42         for (const auto &[timeUs, content] : contents_) {
43             fprintf(fp, "texgine-1 (1) [000] .... %.6lf: tracing_mark_write: %c|1|%s\n",
44                     timeUs / 1e6, content[0], content.substr(1).c_str());
45         }
46 
47         fclose(fp);
48     }
49 
Disable()50     void Disable()
51     {
52         enable_ = false;
53     }
54 
AppendContent(char action,const std::string & content)55     void AppendContent(char action, const std::string &content)
56     {
57         if (enable_ == false) {
58             return;
59         }
60 
61         auto nowUs = std::chrono::duration_cast<std::chrono::microseconds>(
62             std::chrono::steady_clock::now().time_since_epoch()).count();
63         std::string c;
64         c.push_back(action);
65         c.insert(c.end(), content.begin(), content.end());
66         contents_.push_back(std::make_pair(nowUs, std::move(c)));
67     }
68 
69 private:
70     std::list<std::pair<uint64_t, std::string>> contents_;
71     bool enable_ = true;
72 };
73 
Start(const std::string & proc)74 void Trace::Start(const std::string &proc)
75 {
76     TraceContent::GetInstance().AppendContent('B', proc);
77 }
78 
Finish()79 void Trace::Finish()
80 {
81     TraceContent::GetInstance().AppendContent('E', "");
82 }
83 
Count(const std::string & key,int val)84 void Trace::Count(const std::string &key, int val)
85 {
86     TraceContent::GetInstance().AppendContent('C', key + "|" + std::to_string(val));
87 }
88 
Disable()89 void Trace::Disable()
90 {
91     TraceContent::GetInstance().Disable();
92 }
93 } // namespace TextEngine
94 } // namespace Rosen
95 } // namespace OHOS
96