• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 HILOG_COMMON_H
17 #define HILOG_COMMON_H
18 
19 #include <cstdint>
20 #include <iostream>
21 #include <optional>
22 #include <vector>
23 
24 #define SOCKET_FILE_DIR "/dev/unix/socket/"
25 #define INPUT_SOCKET_NAME "hilogInput"
26 #define INPUT_SOCKET SOCKET_FILE_DIR INPUT_SOCKET_NAME
27 #define CONTROL_SOCKET_NAME "hilogControl"
28 #define CONTROL_SOCKET SOCKET_FILE_DIR CONTROL_SOCKET_NAME
29 #define HILOG_FILE_DIR "/data/log/hilog/"
30 #define SYSTEM_BG_STUNE "/dev/stune/system-background/cgroup.procs"
31 #define SYSTEM_BG_CPUSET "/dev/cpuset/system-background/cgroup.procs"
32 #define SYSTEM_BG_BLKIO "/dev/blkio/system-background/cgroup.procs"
33 
34 #define SENDIDN 0    // hilogd: reached end of log;  hilogtool: exit log reading
35 #define SENDIDA 1    // hilogd & hilogtool: normal log reading
36 #define SENDIDS 2    // hilogd: notify for new data; hilogtool: block and wait for new data
37 #define MAX_LOG_LEN 1024  /* maximum length of a log, include '\0' */
38 #define MAX_TAG_LEN 32  /* log tag size, include '\0' */
39 #define MAX_DOMAINS 5
40 #define MAX_TAGS 10
41 #define MAX_PIDS 5
42 #define RET_SUCCESS 0
43 #define RET_FAIL (-1)
44 #define IS_ONE(number, n) ((number>>n)&0x01)
45 #define ONE_KB (1UL<<10)
46 #define ONE_MB (1UL<<20)
47 #define ONE_GB (1UL<<30)
48 #define ONE_TB (1ULL<<40)
49 const long long NSEC = 1000000000LL;
50 const long long US = 1000000LL;
51 const long long NS2US = 1000LL;
52 const long long NS2MS = 1000000LL;
53 const uint32_t MAX_BUFFER_SIZE = 1UL<<30;
54 const uint32_t MAX_PERSISTER_BUFFER_SIZE = 64 * 1024;
55 constexpr int MIN_LOG_FILE_NUM = 2;
56 constexpr int MAX_LOG_FILE_NUM = 1000;
57 const int MSG_MAX_LEN = 2048;
58 constexpr uint64_t PRIME = 0x100000001B3ull;
59 constexpr uint64_t BASIS = 0xCBF29CE484222325ull;
60 /*
61  * header of log message from libhilog to hilogd
62  */
63 using HilogMsg = struct __attribute__((__packed__)) {
64     uint16_t len;
65     uint16_t version : 3;
66     uint16_t type : 4;  /* APP,CORE,INIT,SEC etc */
67     uint16_t level : 3;
68     uint16_t tag_len : 6; /* include '\0' */
69     uint32_t tv_sec;
70     uint32_t tv_nsec;
71     uint32_t pid;
72     uint32_t tid;
73     uint32_t domain;
74     char tag[]; /* shall be end with '\0' */
75 };
76 
77 using HilogShowFormatBuffer = struct {
78     uint16_t length;
79     uint16_t level;
80     uint16_t type;
81     uint16_t tag_len;
82     uint32_t pid;
83     uint32_t tid;
84     uint32_t domain;
85     uint32_t tv_sec;
86     uint32_t tv_nsec;
87     const char* data;
88 };
89 
90 template <typename T>
91 using OptRef = std::optional<std::reference_wrapper<T>>;
92 
93 template <typename T>
94 using OptCRef = std::optional<std::reference_wrapper<const T>>;
95 
96 #define CONTENT_LEN(pMsg) (pMsg->len - sizeof(HilogMsg) - pMsg->tag_len) /* include '\0' */
97 #define CONTENT_PTR(pMsg) (pMsg->tag + pMsg->tag_len)
98 
99 #define likely(x)      __builtin_expect(!!(x), 1)
100 #define unlikely(x)    __builtin_expect(!!(x), 0)
101 
102 #if defined(__GNUC__) && (__GNUC__ >= 4)
103     #define HILOG_PUBLIC_API __attribute__((visibility ("default")))
104     #define HILOG_LOCAL_API __attribute__((visibility("hidden")))
105 #else
106     #define HILOG_PUBLIC_API
107     #define HILOG_LOCAL_API
108 #endif
109 
110 /*
111  * ********************************************
112  *  Error codes list
113  *  Error codes _values_ are pinned down.
114  * ********************************************
115 */
116 typedef enum {
117     ERR_LOG_LEVEL_INVALID  = -2,
118     ERR_LOG_TYPE_INVALID = -3,
119     ERR_QUERY_LEVEL_INVALID = -4,
120     ERR_QUERY_DOMAIN_INVALID = -5,
121     ERR_QUERY_TAG_INVALID = -6,
122     ERR_QUERY_PID_INVALID = -7,
123     ERR_QUERY_TYPE_INVALID = -8,
124     ERR_BUFF_SIZE_EXP = -9,
125     ERR_LOG_CONTENT_NULL = -10,
126     ERR_LOG_PERSIST_FILE_SIZE_INVALID = -11,
127     ERR_LOG_PERSIST_FILE_NAME_INVALID = -12,
128     ERR_LOG_PERSIST_COMPRESS_BUFFER_EXP = -13,
129     ERR_LOG_PERSIST_DIR_OPEN_FAIL = -14,
130     ERR_LOG_PERSIST_COMPRESS_INIT_FAIL = -15,
131     ERR_LOG_PERSIST_FILE_OPEN_FAIL = -16,
132     ERR_LOG_PERSIST_MMAP_FAIL = -17,
133     ERR_LOG_PERSIST_JOBID_FAIL = -18,
134     ERR_DOMAIN_INVALID = -19,
135     ERR_MEM_ALLOC_FAIL = -20,
136     ERR_MSG_LEN_INVALID = -21,
137     ERR_PRIVATE_SWITCH_VALUE_INVALID = -22,
138     ERR_COMMAND_NOT_FOUND = -23,
139     ERR_FORMAT_INVALID = -24,
140     ERR_LOG_PERSIST_FILE_PATH_INVALID = -25,
141     ERR_PERSIST_INFO_OPEN_FAIL = -26,
142     ERR_FLOWCONTROL_CONF_OPEN_FAIL = -27,
143     ERR_LOG_PERSIST_JOBID_INVALID = -28,
144     ERR_FLOWCTRL_SWITCH_VALUE_INVALID = -29,
145     ERR_BUFF_SIZE_INVALID = -30,
146     ERR_COMMAND_INVALID = -31,
147     ERR_LOG_PERSIST_TASK_FAIL = -32,
148     ERR_KMSG_SWITCH_VALUE_INVALID = -33,
149     ERR_LOG_FILE_NUM_INVALID = -34,
150 } ErrorCode;
151 
HilogPrintError(int errnum)152 inline void HilogPrintError(int errnum)
153 {
154     constexpr int bufSize = 1024;
155     char buf[bufSize] = { 0 };
156     strerror_r(errnum, buf, bufSize);
157     std::cerr << "Errno: " << errnum << ", " << buf << "\n";
158 }
159 
160 #endif /* HILOG_COMMON_H */
161