liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <sys/types.h> |
| 3 | #include <sys/stat.h> |
| 4 | #include <sys/socket.h> |
| 5 | #include <netinet/in.h> |
| 6 | #include <netdb.h> |
| 7 | #include <arpa/inet.h> |
| 8 | #include <fcntl.h> |
| 9 | #include <time.h> |
| 10 | #include <unistd.h> |
| 11 | #include <errno.h> |
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 12 | #include <string.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <pthread.h> |
| 15 | |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 16 | #include "log_config.h" |
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 17 | #include "mbtk_utils.h" |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 18 | |
| 19 | #define ALOG_DEV "/dev/log_radio" |
xf.li | 4364377 | 2024-03-04 19:39:53 -0800 | [diff] [blame] | 20 | #define LOG_CONFIG_LEN 50 |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 21 | |
l.yang | 67782e6 | 2024-11-05 01:28:10 -0800 | [diff] [blame] | 22 | #define RADIOLOG_BUFF_SIZE (4*1024) |
| 23 | #define MAX_BUFFER_SIZE (8*1024) |
| 24 | |
l.yang | 250444d | 2024-11-12 00:35:10 -0800 | [diff] [blame] | 25 | #define LOGGER_ENTRY_MAX_LEN (5*1024) |
| 26 | |
| 27 | |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 28 | typedef enum android_LogPriority { |
| 29 | ANDROID_LOG_UNKNOWN = 0, |
| 30 | ANDROID_LOG_DEFAULT, /* only for SetMinPriority() */ |
| 31 | ANDROID_LOG_VERBOSE, |
| 32 | ANDROID_LOG_DEBUG, |
| 33 | ANDROID_LOG_INFO, |
| 34 | ANDROID_LOG_WARN, |
| 35 | ANDROID_LOG_ERROR, |
| 36 | ANDROID_LOG_FATAL, |
| 37 | ANDROID_LOG_SILENT, /* only for SetMinPriority(); must be last */ |
| 38 | } android_LogPriority; |
| 39 | |
| 40 | typedef struct AndroidLogEntry_t { |
| 41 | time_t tv_sec; |
| 42 | long tv_nsec; |
| 43 | android_LogPriority priority; |
| 44 | int32_t pid; |
| 45 | int32_t tid; |
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 46 | char * tag; |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 47 | size_t messageLen; |
| 48 | const char * message; |
| 49 | } AndroidLogEntry; |
| 50 | |
xf.li | 4364377 | 2024-03-04 19:39:53 -0800 | [diff] [blame] | 51 | //static const char *log_file, *log_ip, *log_port, *log_prefix, *pid_file, *hostname; |
| 52 | static char log_file[LOG_CONFIG_LEN], log_ip[LOG_CONFIG_LEN], log_port[LOG_CONFIG_LEN], log_prefix[LOG_CONFIG_LEN], pid_file[LOG_CONFIG_LEN], hostname[LOG_CONFIG_LEN]; |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 53 | static int log_size = 1 * 1024 * 1024; |
| 54 | |
| 55 | static log_config_entry *config = NULL; |
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 56 | static char tmp_log[1024] = {0}; |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 57 | |
l.yang | b7972c7 | 2024-10-15 22:34:51 -0700 | [diff] [blame] | 58 | static int fd_radio = 0; |
| 59 | |
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 60 | int tcp_connect(char* ip, int port); |
l.yang | 67782e6 | 2024-11-05 01:28:10 -0800 | [diff] [blame] | 61 | |
l.yang | 250444d | 2024-11-12 00:35:10 -0800 | [diff] [blame] | 62 | extern int tmp_rd_fd; |
| 63 | extern char radiolog_buff[MAX_BUFFER_SIZE]; |
| 64 | |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 65 | void hex_print(char* buf, int len) |
| 66 | { |
| 67 | int i; |
| 68 | |
| 69 | for (i = 0; i < len; i++) { |
| 70 | printf("%x ", buf[i]); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | static char filterPriToChar(android_LogPriority pri) |
| 75 | { |
| 76 | switch (pri) { |
| 77 | case ANDROID_LOG_VERBOSE: |
| 78 | return 'V'; |
| 79 | case ANDROID_LOG_DEBUG: |
| 80 | return 'D'; |
| 81 | case ANDROID_LOG_INFO: |
| 82 | return 'I'; |
| 83 | case ANDROID_LOG_WARN: |
| 84 | return 'W'; |
| 85 | case ANDROID_LOG_ERROR: |
| 86 | return 'E'; |
| 87 | case ANDROID_LOG_FATAL: |
| 88 | return 'F'; |
| 89 | case ANDROID_LOG_SILENT: |
| 90 | return 'S'; |
| 91 | |
| 92 | case ANDROID_LOG_DEFAULT: |
| 93 | case ANDROID_LOG_UNKNOWN: |
| 94 | default: |
| 95 | return '?'; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | static android_LogPriority filterCharToPri(char c) |
| 100 | { |
| 101 | switch (c) { |
| 102 | case 'v': |
| 103 | return ANDROID_LOG_VERBOSE; |
| 104 | case 'd': |
| 105 | return ANDROID_LOG_DEBUG; |
| 106 | case 'i': |
| 107 | return ANDROID_LOG_INFO; |
| 108 | case 'w': |
| 109 | return ANDROID_LOG_WARN; |
| 110 | case 'e': |
| 111 | return ANDROID_LOG_ERROR; |
| 112 | case 'f': |
| 113 | return ANDROID_LOG_FATAL; |
| 114 | case 's': |
| 115 | return ANDROID_LOG_SILENT; |
| 116 | case '*': |
| 117 | default: |
| 118 | return ANDROID_LOG_VERBOSE; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | int fileter_log(int pri, char *tag, struct filter_list_t *filter) |
| 123 | { |
| 124 | struct filter_list_t *_filter = filter; |
| 125 | |
| 126 | while(_filter) |
| 127 | { |
| 128 | int p = filterCharToPri(_filter->priority); |
| 129 | if(_filter->tag) |
| 130 | { |
| 131 | int len = strlen(_filter->tag); |
| 132 | // tag and priority |
| 133 | if(0 == memcmp(_filter->tag, tag, len) && ((pri > p) || (pri == p))) |
| 134 | return 0; |
| 135 | }else{ // have no tag |
| 136 | if(pri < p) |
| 137 | return -1; |
| 138 | else |
| 139 | return 0; |
| 140 | } |
| 141 | _filter = _filter->next; |
| 142 | } |
| 143 | |
| 144 | return -1; |
| 145 | } |
| 146 | |
| 147 | int alog_process(char* data, int len, AndroidLogEntry *entry) |
| 148 | { |
| 149 | int i, n = 0; |
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 150 | // int tmp_len; |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 151 | |
| 152 | for (i = 0; i < len;) { |
| 153 | if (data[i] == '\0') { |
| 154 | i += 1; |
| 155 | } else { |
| 156 | switch (n) { |
| 157 | case 0: { |
| 158 | // printf("%d - %d: %x\n", i, tmp_len, data[i]); |
| 159 | i++; |
| 160 | break; |
| 161 | } |
| 162 | case 1: { |
| 163 | int* pid = (int*)&data[i]; |
| 164 | entry->pid = *pid; |
| 165 | // printf("%d pid: %d\n", i, entry->pid); |
| 166 | i += 4; |
| 167 | break; |
| 168 | } |
| 169 | case 2: { |
| 170 | int* tid = (int*)&data[i]; |
| 171 | entry->tid = *tid; |
| 172 | // printf("%d tid: %d\n", i, entry->tid); |
| 173 | i += 4; |
| 174 | break; |
| 175 | } |
| 176 | case 3: { |
| 177 | // printf("%d - %d: %x %x %x %x\n", i, tmp_len, data[i], data[i + 1], data[i + 2], data[i + 3]); |
| 178 | time_t* _t = (time_t*)&data[i]; |
| 179 | entry->tv_sec = *_t; |
| 180 | i += 8; |
| 181 | break; |
| 182 | } |
| 183 | case 4: { |
| 184 | entry->priority = data[i]; |
| 185 | entry->tag = &data[i + 1]; |
| 186 | i += strlen(&data[i]); |
| 187 | break; |
| 188 | } |
| 189 | //* format: <priority:1><tag:N>\0<message:N>\0 |
| 190 | case 5: { |
| 191 | entry->message = &data[i]; |
| 192 | entry->messageLen = strlen(&data[i]); |
| 193 | i += entry->messageLen; |
| 194 | break; |
| 195 | } |
| 196 | default: |
| 197 | printf("process error \n"); |
l.yang | f08c4a0 | 2024-11-24 21:18:22 -0800 | [diff] [blame^] | 198 | return -1; |
| 199 | //break; |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 200 | } |
| 201 | n++; |
| 202 | } |
| 203 | } |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | int android_log_printLogLine( |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 208 | struct file_list_t *_file_list, |
| 209 | const AndroidLogEntry *entry) |
| 210 | { |
| 211 | char priChar; |
| 212 | char timeBuf[32]; |
l.yang | 250444d | 2024-11-12 00:35:10 -0800 | [diff] [blame] | 213 | char defaultBuffer[LOGGER_ENTRY_MAX_LEN] = {0}; |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 214 | size_t totalLen; |
xf.li | 01e3982 | 2024-10-30 16:00:32 +0800 | [diff] [blame] | 215 | int index = 0; |
| 216 | int len = 0; |
l.yang | b7972c7 | 2024-10-15 22:34:51 -0700 | [diff] [blame] | 217 | struct stat s; |
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 218 | |
l.yang | 67782e6 | 2024-11-05 01:28:10 -0800 | [diff] [blame] | 219 | |
| 220 | static char buffer[MAX_BUFFER_SIZE] = {0}; |
| 221 | static int buffer_index = 0; |
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 222 | |
l.yang | 250444d | 2024-11-12 00:35:10 -0800 | [diff] [blame] | 223 | |
| 224 | if(fcntl(fd_radio, F_GETFL) == -1 || access(tmp_log, W_OK) != 0) |
l.yang | b7972c7 | 2024-10-15 22:34:51 -0700 | [diff] [blame] | 225 | { |
| 226 | fd_radio = open(tmp_log, O_CREAT | O_WRONLY| O_APPEND, 0600); |
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 227 | if (fd_radio < 0) |
l.yang | b7972c7 | 2024-10-15 22:34:51 -0700 | [diff] [blame] | 228 | { |
| 229 | fprintf(stderr, "failed to open %s: %s\n", tmp_log, strerror(errno)); |
| 230 | exit(-1); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 231 | } |
l.yang | 250444d | 2024-11-12 00:35:10 -0800 | [diff] [blame] | 232 | tmp_rd_fd = fd_radio; |
l.yang | b7972c7 | 2024-10-15 22:34:51 -0700 | [diff] [blame] | 233 | } |
l.yang | 67782e6 | 2024-11-05 01:28:10 -0800 | [diff] [blame] | 234 | |
| 235 | |
l.yang | b7972c7 | 2024-10-15 22:34:51 -0700 | [diff] [blame] | 236 | if (log_size && (!stat(tmp_log, &s)) && (s.st_size > log_size)) |
| 237 | { |
| 238 | fd_radio = get_rotate_file(fd_radio, log_file, _file_list); |
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 239 | if (fd_radio < 0) |
l.yang | b7972c7 | 2024-10-15 22:34:51 -0700 | [diff] [blame] | 240 | { |
| 241 | fprintf(stderr, "failed to open %s: %s\n", log_file, strerror(errno)); |
| 242 | exit(-1); |
| 243 | } |
l.yang | 250444d | 2024-11-12 00:35:10 -0800 | [diff] [blame] | 244 | tmp_rd_fd = fd_radio; |
| 245 | |
l.yang | b7972c7 | 2024-10-15 22:34:51 -0700 | [diff] [blame] | 246 | } |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 247 | |
| 248 | if(fileter_log(entry->priority, entry->tag, config->filter_list)) |
| 249 | { |
| 250 | // printf("%s %d: fileter pri:%d tag:%s!\n", __FUNCTION__, __LINE__, entry->priority, entry->tag); |
| 251 | return -1; |
| 252 | } |
| 253 | |
| 254 | priChar = filterPriToChar(entry->priority); |
| 255 | struct tm* ptm = localtime(&entry->tv_sec); |
| 256 | strftime(timeBuf, sizeof(timeBuf), "%Y-%m-%d %H:%M:%S", ptm); |
| 257 | |
| 258 | totalLen = snprintf(defaultBuffer, sizeof(defaultBuffer), |
| 259 | "%s %c/%s (%d): %s\n", timeBuf, priChar, entry->tag, entry->pid, entry->message); |
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 260 | |
xf.li | 01e3982 | 2024-10-30 16:00:32 +0800 | [diff] [blame] | 261 | len = strlen(defaultBuffer); |
| 262 | if(access("/etc/syslog_encrypt_flag", F_OK) == 0) |
| 263 | { |
| 264 | for(index = 0; index < len; index++) |
| 265 | { |
| 266 | defaultBuffer[index] ^= 1; |
| 267 | } |
| 268 | } |
l.yang | 250444d | 2024-11-12 00:35:10 -0800 | [diff] [blame] | 269 | |
| 270 | if(buffer_index + totalLen >= RADIOLOG_BUFF_SIZE && buffer_index < RADIOLOG_BUFF_SIZE) |
l.yang | 67782e6 | 2024-11-05 01:28:10 -0800 | [diff] [blame] | 271 | { |
l.yang | 250444d | 2024-11-12 00:35:10 -0800 | [diff] [blame] | 272 | memcpy(buffer + buffer_index, defaultBuffer, totalLen); |
| 273 | buffer_index += totalLen; |
| 274 | if (write(fd_radio, buffer, buffer_index) < 0) |
l.yang | 67782e6 | 2024-11-05 01:28:10 -0800 | [diff] [blame] | 275 | { |
| 276 | perror("write error"); |
l.yang | 250444d | 2024-11-12 00:35:10 -0800 | [diff] [blame] | 277 | close(fd_radio); |
l.yang | 67782e6 | 2024-11-05 01:28:10 -0800 | [diff] [blame] | 278 | return -1; |
| 279 | } |
| 280 | buffer_index = 0; // Reset buffer index after flushing |
| 281 | } |
l.yang | 67782e6 | 2024-11-05 01:28:10 -0800 | [diff] [blame] | 282 | else |
| 283 | { |
l.yang | 250444d | 2024-11-12 00:35:10 -0800 | [diff] [blame] | 284 | memcpy(buffer + buffer_index, defaultBuffer, totalLen); |
| 285 | buffer_index += totalLen; |
| 286 | memcpy(radiolog_buff,buffer,buffer_index); |
l.yang | 67782e6 | 2024-11-05 01:28:10 -0800 | [diff] [blame] | 287 | } |
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 288 | |
| 289 | |
l.yang | 67782e6 | 2024-11-05 01:28:10 -0800 | [diff] [blame] | 290 | return 0; |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | void* alog_thread(void* argv) |
| 294 | { |
| 295 | int dev_fd, ret; |
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 296 | // int log_fd; |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 297 | AndroidLogEntry entry; |
l.yang | 250444d | 2024-11-12 00:35:10 -0800 | [diff] [blame] | 298 | char buf[LOGGER_ENTRY_MAX_LEN] = {0}; |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 299 | static struct file_list_t file_list; |
| 300 | config = (log_config_entry *)argv; |
| 301 | |
| 302 | pthread_detach(pthread_self()); |
| 303 | if (NULL == argv) |
| 304 | return NULL; |
| 305 | |
| 306 | dev_fd = open(ALOG_DEV, O_RDONLY, 0600); |
| 307 | if (dev_fd < 0) { |
| 308 | fprintf(stderr, "failed to open %s: %s\n", ALOG_DEV, strerror(errno)); |
| 309 | exit(-1); |
| 310 | } |
| 311 | |
| 312 | memset(&file_list, 0, sizeof(struct file_list_t)); |
| 313 | file_list.total = config->rotate_file_count; |
xf.li | 4364377 | 2024-03-04 19:39:53 -0800 | [diff] [blame] | 314 | //log_file = config->out_path; |
| 315 | memset(log_file, 0, sizeof(log_file)); |
| 316 | memset(log_ip, 0, sizeof(log_ip)); |
| 317 | memset(log_port, 0, sizeof(log_port)); |
| 318 | memset(log_prefix, 0, sizeof(log_prefix)); |
| 319 | memset(pid_file, 0, sizeof(pid_file)); |
| 320 | memset(hostname, 0, sizeof(hostname)); |
| 321 | if(config->out_path != NULL) |
| 322 | { |
| 323 | strncpy(log_file, config->out_path, LOG_CONFIG_LEN - 1); |
| 324 | } |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 325 | |
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 326 | if (config->ip && config->port) |
l.yang | b7972c7 | 2024-10-15 22:34:51 -0700 | [diff] [blame] | 327 | { |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 328 | int port = atoi(config->port); |
| 329 | printf("%s %d : %s:%s\n", __FUNCTION__, __LINE__, config->ip, config->port); |
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 330 | tcp_connect(config->ip, port); |
| 331 | } |
| 332 | else if (strlen(log_file) > 0) |
l.yang | b7972c7 | 2024-10-15 22:34:51 -0700 | [diff] [blame] | 333 | { |
| 334 | //sprintf(tmp_log, "/tmp/log%s", strstr_tail(log_file, "/")); |
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 335 | |
l.yang | b7972c7 | 2024-10-15 22:34:51 -0700 | [diff] [blame] | 336 | snprintf(tmp_log,sizeof(tmp_log), "%s", log_file); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 337 | // 先将文件保存到 /tmp/log/ 目录下,后面到达 rotate_file_size 后,转移到out_path |
l.yang | 250444d | 2024-11-12 00:35:10 -0800 | [diff] [blame] | 338 | |
| 339 | fd_radio = open(tmp_log, O_CREAT | O_WRONLY| O_APPEND, 0600); |
| 340 | if (fd_radio < 0) |
| 341 | { |
| 342 | fprintf(stderr, "failed to open %s: %s\n", tmp_log, strerror(errno)); |
| 343 | exit(-1); |
| 344 | } |
| 345 | tmp_rd_fd = fd_radio; |
| 346 | } |
| 347 | else |
l.yang | b7972c7 | 2024-10-15 22:34:51 -0700 | [diff] [blame] | 348 | { |
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 349 | //log_fd = STDOUT_FILENO; |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 350 | } |
| 351 | if(config->rotate_file_size) |
l.yang | b7972c7 | 2024-10-15 22:34:51 -0700 | [diff] [blame] | 352 | { |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 353 | log_size = config->rotate_file_size; |
l.yang | b7972c7 | 2024-10-15 22:34:51 -0700 | [diff] [blame] | 354 | } |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 355 | printf("android log start...\n"); |
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 356 | while (1) |
l.yang | b7972c7 | 2024-10-15 22:34:51 -0700 | [diff] [blame] | 357 | { |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 358 | ret = read(dev_fd, buf, sizeof(buf)); |
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 359 | if (ret < 0) |
l.yang | b7972c7 | 2024-10-15 22:34:51 -0700 | [diff] [blame] | 360 | { |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 361 | printf("read error\n"); |
| 362 | break; |
| 363 | } |
| 364 | alog_process(buf, ret, &entry); |
l.yang | b7972c7 | 2024-10-15 22:34:51 -0700 | [diff] [blame] | 365 | android_log_printLogLine(&file_list, &entry); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 366 | memset(buf, 0, sizeof(buf)); |
| 367 | } |
| 368 | close(dev_fd); |
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 369 | |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 370 | printf("%s exit \n", __FUNCTION__); |
| 371 | pthread_exit(NULL); |
| 372 | return NULL; |
| 373 | } |