• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial
14  * portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25 
26 #include "trace.h"
27 
28 #include <cstdarg>
29 #include <cstdio>
30 #include <memory.h>
31 #include <mutex>
32 #include <unistd.h>
33 
34 #include <hilog/log.h>
35 
36 using Cstr = const char *;
37 
38 class IWestonTrace {
39 public:
40     virtual ~IWestonTrace() = default;
41 
42     virtual void Output(Cstr label, Cstr func, int32_t line, Cstr color, Cstr str) = 0;
43     virtual void LevelInc() = 0;
44     virtual void LevelDec() = 0;
45 };
46 
47 class WestonTraceImpl : public IWestonTrace {
48 public:
WestonTraceImpl()49     WestonTraceImpl()
50     {
51         memset(space, ' ', sizeof(space));
52         space[0] = 0;
53     }
54 
Output(Cstr label,Cstr func,int32_t line,Cstr color,Cstr str)55     virtual void Output(Cstr label, Cstr func, int32_t line, Cstr color, Cstr str) override
56     {
57         OHOS::HiviewDFX::HiLog::Info({(LogType)3, 0, "Weston"}, "\033[31m%{public}-12s |"
58             " \033[33m%{public}-45s|\033[34m%{public}-5d\033[0m:%{public}s %{public}s%{public}s\033[0m",
59             label, func, line, space, color, str);
60     }
61 
LevelInc()62     virtual void LevelInc() override
63     {
64         std::lock_guard<std::mutex> lock(levelSpaceMutex);
65         space[level * 2] = ' ';
66         level++;
67         space[level * 2] = 0;
68     }
69 
LevelDec()70     virtual void LevelDec() override
71     {
72         std::lock_guard<std::mutex> lock(levelSpaceMutex);
73         space[level * 2] = ' ';
74         level--;
75         space[level * 2] = 0;
76     }
77 
78 private:
79     int32_t level = 0;
80     char space[64] = {};
81     std::mutex levelSpaceMutex;
82 };
83 
84 class WestonTraceNoop : public IWestonTrace {
85 public:
Output(Cstr label,Cstr func,int32_t line,Cstr color,Cstr str)86     virtual void Output(Cstr label, Cstr func, int32_t line, Cstr color, Cstr str) override
87     {
88         if (strcmp(color, "\033[33m") == 0) {
89             return;
90         }
91 
92         OHOS::HiviewDFX::HiLog::Info({(LogType)3, 0, "WAYLAND_SERVER"},
93             "%{public}s/%{public}s<%{public}d>: %{public}s", label, func, line, str);
94     }
95 
LevelInc()96     virtual void LevelInc() override
97     {
98     }
99 
LevelDec()100     virtual void LevelDec() override
101     {
102     }
103 };
104 
105 static IWestonTrace *g_westonTrace = nullptr;
106 
log_init()107 void log_init()
108 {
109     if (access("/data/weston_trace", F_OK) == -1) {
110         g_westonTrace = new WestonTraceNoop();
111     } else {
112         g_westonTrace = new WestonTraceImpl();
113     }
114 }
115 
log_printf(Cstr label,Cstr func,int32_t line,Cstr color,Cstr fmt,...)116 void log_printf(Cstr label, Cstr func, int32_t line, Cstr color, Cstr fmt, ...)
117 {
118     char str[4096];
119     va_list ap;
120     va_start(ap, fmt);
121     vsnprintf(str, sizeof(str), fmt, ap);
122     va_end(ap);
123     g_westonTrace->Output(label, func, line, color, str);
124 }
125 
log_level_inc()126 void log_level_inc()
127 {
128     g_westonTrace->LevelInc();
129 }
130 
log_level_dec()131 void log_level_dec()
132 {
133     g_westonTrace->LevelDec();
134 }
135 
ScopedLog(Cstr label,Cstr func,int32_t line,Cstr str)136 ScopedLog::ScopedLog(Cstr label, Cstr func, int32_t line, Cstr str)
137 {
138     label_ = label;
139     func_ = func;
140     line_ = line;
141     str_ = str;
142     log_printf(label_, func_, line_, "\033[33m", "%s{", str_);
143     log_level_inc();
144 }
145 
~ScopedLog()146 ScopedLog::~ScopedLog()
147 {
148     log_level_dec();
149     log_printf(label_, func_, line_, "\033[33m", "} %s", str_);
150 }
151