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 <climits> 20 #include <cstdint> 21 #include <iostream> 22 #include <optional> 23 #include <vector> 24 #include <functional> 25 26 #define SOCKET_FILE_DIR "/dev/unix/socket/" 27 #define INPUT_SOCKET_NAME "hilogInput" 28 #define OUTPUT_SOCKET_NAME "hilogOutput" 29 #define CONTROL_SOCKET_NAME "hilogControl" 30 #define HILOG_FILE_DIR "/data/log/hilog/" 31 #define MAX_LOG_LEN 1024 /* maximum length of a log, include '\0' */ 32 #define MAX_TAG_LEN 32 /* log tag size, include '\0' */ 33 #define MAX_REGEX_STR_LEN (128) 34 #define MAX_FILE_NAME_LEN (64) 35 #define RET_SUCCESS 0 36 #define RET_FAIL (-1) 37 #define MAX_JOBS (10) 38 constexpr size_t MIN_BUFFER_SIZE = (64 * 1024); 39 constexpr size_t MAX_BUFFER_SIZE = (512 * 1024 * 1024); 40 constexpr uint32_t MAX_PERSISTER_BUFFER_SIZE = 64 * 1024; 41 constexpr uint32_t MIN_LOG_FILE_SIZE = MAX_PERSISTER_BUFFER_SIZE; 42 constexpr uint32_t MAX_LOG_FILE_SIZE = (512 * 1024 * 1024); 43 constexpr int MIN_LOG_FILE_NUM = 2; 44 constexpr int MAX_LOG_FILE_NUM = 1000; 45 constexpr uint32_t DOMAIN_OS_MIN = 0xD000000; 46 constexpr uint32_t DOMAIN_OS_MAX = 0xD0FFFFF; 47 constexpr uint32_t DOMAIN_APP_MIN = 0x0; 48 constexpr uint32_t DOMAIN_APP_MAX = 0xFFFF; 49 constexpr uint32_t JOB_ID_MIN = 10; 50 constexpr uint32_t JOB_ID_MAX = UINT_MAX; 51 constexpr uint32_t WAITING_DATA_MS = 5000; 52 53 /* 54 * header of log message from libhilog to hilogd 55 */ 56 using HilogMsg = struct __attribute__((__packed__)) { 57 uint16_t len; 58 uint16_t version : 3; 59 uint16_t type : 4; /* APP,CORE,INIT,SEC etc */ 60 uint16_t level : 3; 61 uint16_t tag_len : 6; /* include '\0' */ 62 uint32_t tv_sec; 63 uint32_t tv_nsec; 64 uint32_t mono_sec; 65 uint32_t pid; 66 uint32_t tid; 67 uint32_t domain; 68 char tag[]; /* shall be end with '\0' */ 69 }; 70 71 template <typename T> 72 using OptRef = std::optional<std::reference_wrapper<T>>; 73 74 template <typename T> 75 using OptCRef = std::optional<std::reference_wrapper<const T>>; 76 77 #define CONTENT_LEN(pMsg) ((pMsg)->len - sizeof(HilogMsg) - (pMsg)->tag_len) /* include '\0' */ 78 #define CONTENT_PTR(pMsg) ((pMsg)->tag + (pMsg)->tag_len) 79 80 #define likely(x) __builtin_expect(!!(x), 1) 81 #define unlikely(x) __builtin_expect(!!(x), 0) 82 83 #if defined(__GNUC__) && (__GNUC__ >= 4) 84 #define HILOG_PUBLIC_API __attribute__((visibility ("default"))) 85 #define HILOG_LOCAL_API __attribute__((visibility("hidden"))) 86 #else 87 #define HILOG_PUBLIC_API 88 #define HILOG_LOCAL_API 89 #endif 90 91 /* 92 * ******************************************** 93 * Error codes list 94 * Error codes _values_ are pinned down. 95 * ******************************************** 96 */ 97 typedef enum { 98 SUCCESS_CONTINUE = 1, 99 ERR_LOG_LEVEL_INVALID = -2, 100 ERR_LOG_TYPE_INVALID = -3, 101 ERR_INVALID_RQST_CMD = -4, 102 ERR_INVALID_DOMAIN_STR = -5, 103 ERR_QUERY_TYPE_INVALID = -8, 104 ERR_LOG_PERSIST_FILE_SIZE_INVALID = -11, 105 ERR_LOG_PERSIST_FILE_NAME_INVALID = -12, 106 ERR_LOG_PERSIST_COMPRESS_BUFFER_EXP = -13, 107 ERR_LOG_PERSIST_DIR_OPEN_FAIL = -14, 108 ERR_LOG_PERSIST_COMPRESS_INIT_FAIL = -15, 109 ERR_LOG_PERSIST_FILE_OPEN_FAIL = -16, 110 ERR_LOG_PERSIST_JOBID_FAIL = -18, 111 ERR_DOMAIN_INVALID = -19, 112 ERR_MSG_LEN_INVALID = -21, 113 ERR_LOG_PERSIST_FILE_PATH_INVALID = -25, 114 ERR_LOG_PERSIST_JOBID_INVALID = -28, 115 ERR_BUFF_SIZE_INVALID = -30, 116 ERR_COMMAND_INVALID = -31, 117 ERR_LOG_PERSIST_TASK_EXISTED = -32, 118 ERR_LOG_FILE_NUM_INVALID = -34, 119 ERR_NOT_NUMBER_STR = -35, 120 ERR_TOO_MANY_ARGUMENTS = -36, 121 ERR_DUPLICATE_OPTION = -37, 122 ERR_INVALID_ARGUMENT = -38, 123 ERR_TOO_MANY_DOMAINS = -39, 124 ERR_INVALID_SIZE_STR = -40, 125 ERR_TOO_MANY_PIDS = -41, 126 ERR_TOO_MANY_TAGS = -42, 127 ERR_TAG_STR_TOO_LONG = -43, 128 ERR_REGEX_STR_TOO_LONG = -44, 129 ERR_FILE_NAME_TOO_LONG = -45, 130 ERR_SOCKET_CLIENT_INIT_FAIL = -46, 131 ERR_SOCKET_WRITE_MSG_HEADER_FAIL = -47, 132 ERR_SOCKET_WRITE_CMD_FAIL = -48, 133 ERR_SOCKET_RECEIVE_RSP = -49, 134 ERR_PERSIST_TASK_EMPTY = -50, 135 ERR_JOBID_NOT_EXSIST = -60, 136 ERR_TOO_MANY_JOBS = -61, 137 ERR_STATS_NOT_ENABLE = -62, 138 ERR_NO_RUNNING_TASK = -63, 139 ERR_NO_PID_PERMISSION = -64, 140 } ErrorCode; 141 142 #endif /* HILOG_COMMON_H */ 143