1 /*
2 * Copyright (c) 2025 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 #ifndef COMMON_H
17 #define COMMON_H
18
19 #include <atomic>
20 #include <cstdint>
21 #include <stdarg.h>
22 #include <stdio.h>
23
24 #include <linux/input.h>
25
26 #include "mmi_log.h"
27
28 namespace OHOS {
29 namespace MMI {
30
31 struct EventRecord {
32 uint32_t deviceId;
33 input_event event;
34 };
35
36 // Global shutdown flag for event recorder tool use
37 extern std::atomic<bool> g_shutdown;
38
39 // Print functions in place of Logger
PrintWithPrefix(const char * prefix,const char * format,va_list args)40 inline void PrintWithPrefix(const char* prefix, const char* format, va_list args)
41 {
42 if (prefix && *prefix) {
43 printf("%s", prefix); // Only print prefix if it's not empty
44 }
45 vprintf(format, args);
46 printf("\n");
47 }
48
PrintDebug(const char * format,...)49 inline void PrintDebug(const char* format, ...)
50 {
51 va_list args;
52 va_start(args, format);
53 PrintWithPrefix("", format, args);
54 va_end(args);
55 }
56
PrintInfo(const char * format,...)57 inline void PrintInfo(const char* format, ...)
58 {
59 va_list args;
60 va_start(args, format);
61 PrintWithPrefix("", format, args);
62 va_end(args);
63 }
64
PrintWarning(const char * format,...)65 inline void PrintWarning(const char* format, ...)
66 {
67 va_list args;
68 va_start(args, format);
69 PrintWithPrefix("[WARNING] ", format, args);
70 va_end(args);
71 }
72
PrintError(const char * format,...)73 inline void PrintError(const char* format, ...)
74 {
75 va_list args;
76 va_start(args, format);
77 PrintWithPrefix("[ERROR] ", format, args);
78 va_end(args);
79 }
80
TrimString(std::string & str)81 inline void TrimString(std::string& str)
82 {
83 size_t start = str.find_first_not_of(" \t");
84 if (start == std::string::npos) {
85 str.clear();
86 return;
87 }
88 size_t end = str.find_last_not_of(" \t");
89 str = str.substr(start, end - start + 1);
90 }
91
RemovePrefix(std::string & str,const std::string & prefix)92 inline bool RemovePrefix(std::string& str, const std::string& prefix)
93 {
94 if (str.find(prefix) == 0) {
95 str.erase(0, prefix.length());
96 return true;
97 }
98 return false;
99 }
100 } // namespace MMI
101 } // namespace OHOS
102 #endif // COMMON_H