blob: d05da170c7e03ca4b88845230f1da30b1e4fb1d2 [file] [log] [blame]
liubin281ac462023-07-19 14:22:54 +08001#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.liu9e8584b2024-11-06 19:21:28 +080012#include <string.h>
13#include <stdlib.h>
14#include <pthread.h>
15
liubin281ac462023-07-19 14:22:54 +080016#include "log_config.h"
b.liu9e8584b2024-11-06 19:21:28 +080017#include "mbtk_utils.h"
liubin281ac462023-07-19 14:22:54 +080018
19#define ALOG_DEV "/dev/log_radio"
xf.li43643772024-03-04 19:39:53 -080020#define LOG_CONFIG_LEN 50
liubin281ac462023-07-19 14:22:54 +080021
l.yang67782e62024-11-05 01:28:10 -080022#define RADIOLOG_BUFF_SIZE (4*1024)
23#define MAX_BUFFER_SIZE (8*1024)
24
l.yang250444d2024-11-12 00:35:10 -080025#define LOGGER_ENTRY_MAX_LEN (5*1024)
26
27
liubin281ac462023-07-19 14:22:54 +080028typedef 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
40typedef 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.liu9e8584b2024-11-06 19:21:28 +080046 char * tag;
liubin281ac462023-07-19 14:22:54 +080047 size_t messageLen;
48 const char * message;
49} AndroidLogEntry;
50
xf.li43643772024-03-04 19:39:53 -080051//static const char *log_file, *log_ip, *log_port, *log_prefix, *pid_file, *hostname;
52static 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];
liubin281ac462023-07-19 14:22:54 +080053static int log_size = 1 * 1024 * 1024;
54
55static log_config_entry *config = NULL;
b.liu9e8584b2024-11-06 19:21:28 +080056static char tmp_log[1024] = {0};
liubin281ac462023-07-19 14:22:54 +080057
l.yangb7972c72024-10-15 22:34:51 -070058static int fd_radio = 0;
59
b.liu9e8584b2024-11-06 19:21:28 +080060int tcp_connect(char* ip, int port);
l.yang67782e62024-11-05 01:28:10 -080061
l.yang250444d2024-11-12 00:35:10 -080062extern int tmp_rd_fd;
l.yangfa05af32024-11-27 00:00:56 -080063//extern char radiolog_buff[MAX_BUFFER_SIZE];
64extern char *radio_globalPtr;
65extern int radio_len;
l.yangd2c71bc2024-12-16 19:24:24 -080066extern int emmc_pro;
l.yangfa05af32024-11-27 00:00:56 -080067
l.yang250444d2024-11-12 00:35:10 -080068
liubin281ac462023-07-19 14:22:54 +080069void hex_print(char* buf, int len)
70{
71 int i;
72
73 for (i = 0; i < len; i++) {
74 printf("%x ", buf[i]);
75 }
76}
77
78static char filterPriToChar(android_LogPriority pri)
79{
80 switch (pri) {
81 case ANDROID_LOG_VERBOSE:
82 return 'V';
83 case ANDROID_LOG_DEBUG:
84 return 'D';
85 case ANDROID_LOG_INFO:
86 return 'I';
87 case ANDROID_LOG_WARN:
88 return 'W';
89 case ANDROID_LOG_ERROR:
90 return 'E';
91 case ANDROID_LOG_FATAL:
92 return 'F';
93 case ANDROID_LOG_SILENT:
94 return 'S';
95
96 case ANDROID_LOG_DEFAULT:
97 case ANDROID_LOG_UNKNOWN:
98 default:
99 return '?';
100 }
101}
102
103static android_LogPriority filterCharToPri(char c)
104{
105 switch (c) {
106 case 'v':
107 return ANDROID_LOG_VERBOSE;
108 case 'd':
109 return ANDROID_LOG_DEBUG;
110 case 'i':
111 return ANDROID_LOG_INFO;
112 case 'w':
113 return ANDROID_LOG_WARN;
114 case 'e':
115 return ANDROID_LOG_ERROR;
116 case 'f':
117 return ANDROID_LOG_FATAL;
118 case 's':
119 return ANDROID_LOG_SILENT;
120 case '*':
121 default:
122 return ANDROID_LOG_VERBOSE;
123 }
124}
125
126int fileter_log(int pri, char *tag, struct filter_list_t *filter)
127{
128 struct filter_list_t *_filter = filter;
129
130 while(_filter)
131 {
132 int p = filterCharToPri(_filter->priority);
133 if(_filter->tag)
134 {
135 int len = strlen(_filter->tag);
136 // tag and priority
137 if(0 == memcmp(_filter->tag, tag, len) && ((pri > p) || (pri == p)))
138 return 0;
139 }else{ // have no tag
140 if(pri < p)
141 return -1;
142 else
143 return 0;
144 }
145 _filter = _filter->next;
146 }
147
148 return -1;
149}
150
151int alog_process(char* data, int len, AndroidLogEntry *entry)
152{
153 int i, n = 0;
b.liu9e8584b2024-11-06 19:21:28 +0800154// int tmp_len;
liubin281ac462023-07-19 14:22:54 +0800155
156 for (i = 0; i < len;) {
157 if (data[i] == '\0') {
158 i += 1;
159 } else {
160 switch (n) {
161 case 0: {
162 // printf("%d - %d: %x\n", i, tmp_len, data[i]);
163 i++;
164 break;
165 }
166 case 1: {
167 int* pid = (int*)&data[i];
168 entry->pid = *pid;
169 // printf("%d pid: %d\n", i, entry->pid);
170 i += 4;
171 break;
172 }
173 case 2: {
174 int* tid = (int*)&data[i];
175 entry->tid = *tid;
176 // printf("%d tid: %d\n", i, entry->tid);
177 i += 4;
178 break;
179 }
180 case 3: {
181 // printf("%d - %d: %x %x %x %x\n", i, tmp_len, data[i], data[i + 1], data[i + 2], data[i + 3]);
182 time_t* _t = (time_t*)&data[i];
183 entry->tv_sec = *_t;
184 i += 8;
185 break;
186 }
187 case 4: {
188 entry->priority = data[i];
189 entry->tag = &data[i + 1];
190 i += strlen(&data[i]);
191 break;
192 }
193 //* format: <priority:1><tag:N>\0<message:N>\0
194 case 5: {
195 entry->message = &data[i];
196 entry->messageLen = strlen(&data[i]);
197 i += entry->messageLen;
198 break;
199 }
200 default:
201 printf("process error \n");
l.yangf08c4a02024-11-24 21:18:22 -0800202 return -1;
203 //break;
liubin281ac462023-07-19 14:22:54 +0800204 }
205 n++;
206 }
207 }
208 return 0;
209}
210
211int android_log_printLogLine(
liubin281ac462023-07-19 14:22:54 +0800212 struct file_list_t *_file_list,
213 const AndroidLogEntry *entry)
214{
215 char priChar;
216 char timeBuf[32];
l.yang250444d2024-11-12 00:35:10 -0800217 char defaultBuffer[LOGGER_ENTRY_MAX_LEN] = {0};
liubin281ac462023-07-19 14:22:54 +0800218 size_t totalLen;
xf.li01e39822024-10-30 16:00:32 +0800219 int index = 0;
220 int len = 0;
l.yangb7972c72024-10-15 22:34:51 -0700221 struct stat s;
b.liu9e8584b2024-11-06 19:21:28 +0800222
l.yang67782e62024-11-05 01:28:10 -0800223
224 static char buffer[MAX_BUFFER_SIZE] = {0};
225 static int buffer_index = 0;
l.yangfa05af32024-11-27 00:00:56 -0800226 radio_globalPtr = buffer;
l.yang7ec80982024-12-15 23:34:35 -0800227
228 if(access("/etc/syslog_close", F_OK) == 0)
229 {
230 return 0;
231 }
232
l.yang250444d2024-11-12 00:35:10 -0800233 if(fcntl(fd_radio, F_GETFL) == -1 || access(tmp_log, W_OK) != 0)
l.yangb7972c72024-10-15 22:34:51 -0700234 {
235 fd_radio = open(tmp_log, O_CREAT | O_WRONLY| O_APPEND, 0600);
b.liu9e8584b2024-11-06 19:21:28 +0800236 if (fd_radio < 0)
l.yangb7972c72024-10-15 22:34:51 -0700237 {
238 fprintf(stderr, "failed to open %s: %s\n", tmp_log, strerror(errno));
239 exit(-1);
liubin281ac462023-07-19 14:22:54 +0800240 }
l.yang250444d2024-11-12 00:35:10 -0800241 tmp_rd_fd = fd_radio;
l.yangb7972c72024-10-15 22:34:51 -0700242 }
l.yang67782e62024-11-05 01:28:10 -0800243
244
l.yangb7972c72024-10-15 22:34:51 -0700245 if (log_size && (!stat(tmp_log, &s)) && (s.st_size > log_size))
246 {
247 fd_radio = get_rotate_file(fd_radio, log_file, _file_list);
b.liu9e8584b2024-11-06 19:21:28 +0800248 if (fd_radio < 0)
l.yangb7972c72024-10-15 22:34:51 -0700249 {
250 fprintf(stderr, "failed to open %s: %s\n", log_file, strerror(errno));
251 exit(-1);
252 }
l.yang250444d2024-11-12 00:35:10 -0800253 tmp_rd_fd = fd_radio;
254
l.yangb7972c72024-10-15 22:34:51 -0700255 }
liubin281ac462023-07-19 14:22:54 +0800256
257 if(fileter_log(entry->priority, entry->tag, config->filter_list))
258 {
259 // printf("%s %d: fileter pri:%d tag:%s!\n", __FUNCTION__, __LINE__, entry->priority, entry->tag);
260 return -1;
261 }
262
263 priChar = filterPriToChar(entry->priority);
264 struct tm* ptm = localtime(&entry->tv_sec);
265 strftime(timeBuf, sizeof(timeBuf), "%Y-%m-%d %H:%M:%S", ptm);
266
267 totalLen = snprintf(defaultBuffer, sizeof(defaultBuffer),
268 "%s %c/%s (%d): %s\n", timeBuf, priChar, entry->tag, entry->pid, entry->message);
b.liu9e8584b2024-11-06 19:21:28 +0800269
xf.li01e39822024-10-30 16:00:32 +0800270 len = strlen(defaultBuffer);
271 if(access("/etc/syslog_encrypt_flag", F_OK) == 0)
272 {
273 for(index = 0; index < len; index++)
274 {
275 defaultBuffer[index] ^= 1;
276 }
277 }
l.yangd2c71bc2024-12-16 19:24:24 -0800278 if(emmc_pro)
l.yang67782e62024-11-05 01:28:10 -0800279 {
l.yangd2c71bc2024-12-16 19:24:24 -0800280 if(buffer_index + totalLen >= RADIOLOG_BUFF_SIZE && buffer_index < RADIOLOG_BUFF_SIZE)
281 {
282 memcpy(buffer + buffer_index, defaultBuffer, totalLen);
283 buffer_index += totalLen;
284 if (write(fd_radio, buffer, buffer_index) < 0)
285 {
286 perror("write error");
287 close(fd_radio);
288 return -1;
289 }
290 buffer_index = 0; // Reset buffer index after flushing
291 }
292 else
293 {
294 memcpy(buffer + buffer_index, defaultBuffer, totalLen);
295 buffer_index += totalLen;
296 //memcpy(radiolog_buff,buffer,buffer_index);
297 }
298 }
299 else
300 {
301 if (write(fd_radio, defaultBuffer, totalLen) < 0)
l.yang67782e62024-11-05 01:28:10 -0800302 {
303 perror("write error");
l.yang250444d2024-11-12 00:35:10 -0800304 close(fd_radio);
l.yang67782e62024-11-05 01:28:10 -0800305 return -1;
306 }
l.yang67782e62024-11-05 01:28:10 -0800307 }
b.liu9e8584b2024-11-06 19:21:28 +0800308
309
l.yang67782e62024-11-05 01:28:10 -0800310 return 0;
liubin281ac462023-07-19 14:22:54 +0800311}
312
313void* alog_thread(void* argv)
314{
315 int dev_fd, ret;
b.liu9e8584b2024-11-06 19:21:28 +0800316// int log_fd;
liubin281ac462023-07-19 14:22:54 +0800317 AndroidLogEntry entry;
l.yang250444d2024-11-12 00:35:10 -0800318 char buf[LOGGER_ENTRY_MAX_LEN] = {0};
liubin281ac462023-07-19 14:22:54 +0800319 static struct file_list_t file_list;
320 config = (log_config_entry *)argv;
321
322 pthread_detach(pthread_self());
323 if (NULL == argv)
324 return NULL;
325
326 dev_fd = open(ALOG_DEV, O_RDONLY, 0600);
327 if (dev_fd < 0) {
328 fprintf(stderr, "failed to open %s: %s\n", ALOG_DEV, strerror(errno));
329 exit(-1);
330 }
331
332 memset(&file_list, 0, sizeof(struct file_list_t));
333 file_list.total = config->rotate_file_count;
xf.li43643772024-03-04 19:39:53 -0800334 //log_file = config->out_path;
335 memset(log_file, 0, sizeof(log_file));
336 memset(log_ip, 0, sizeof(log_ip));
337 memset(log_port, 0, sizeof(log_port));
338 memset(log_prefix, 0, sizeof(log_prefix));
339 memset(pid_file, 0, sizeof(pid_file));
340 memset(hostname, 0, sizeof(hostname));
341 if(config->out_path != NULL)
342 {
343 strncpy(log_file, config->out_path, LOG_CONFIG_LEN - 1);
344 }
liubin281ac462023-07-19 14:22:54 +0800345
b.liu9e8584b2024-11-06 19:21:28 +0800346 if (config->ip && config->port)
l.yangb7972c72024-10-15 22:34:51 -0700347 {
liubin281ac462023-07-19 14:22:54 +0800348 int port = atoi(config->port);
349 printf("%s %d : %s:%s\n", __FUNCTION__, __LINE__, config->ip, config->port);
b.liu9e8584b2024-11-06 19:21:28 +0800350 tcp_connect(config->ip, port);
351 }
352 else if (strlen(log_file) > 0)
l.yangb7972c72024-10-15 22:34:51 -0700353 {
354 //sprintf(tmp_log, "/tmp/log%s", strstr_tail(log_file, "/"));
b.liu9e8584b2024-11-06 19:21:28 +0800355
l.yangb7972c72024-10-15 22:34:51 -0700356 snprintf(tmp_log,sizeof(tmp_log), "%s", log_file);
liubin281ac462023-07-19 14:22:54 +0800357 // 先将文件保存到 /tmp/log/ 目录下,后面到达 rotate_file_size 后,转移到out_path
l.yang250444d2024-11-12 00:35:10 -0800358
359 fd_radio = open(tmp_log, O_CREAT | O_WRONLY| O_APPEND, 0600);
360 if (fd_radio < 0)
361 {
362 fprintf(stderr, "failed to open %s: %s\n", tmp_log, strerror(errno));
363 exit(-1);
364 }
365 tmp_rd_fd = fd_radio;
366 }
367 else
l.yangb7972c72024-10-15 22:34:51 -0700368 {
b.liu9e8584b2024-11-06 19:21:28 +0800369 //log_fd = STDOUT_FILENO;
liubin281ac462023-07-19 14:22:54 +0800370 }
371 if(config->rotate_file_size)
l.yangb7972c72024-10-15 22:34:51 -0700372 {
liubin281ac462023-07-19 14:22:54 +0800373 log_size = config->rotate_file_size;
l.yangb7972c72024-10-15 22:34:51 -0700374 }
liubin281ac462023-07-19 14:22:54 +0800375 printf("android log start...\n");
b.liu9e8584b2024-11-06 19:21:28 +0800376 while (1)
l.yangb7972c72024-10-15 22:34:51 -0700377 {
liubin281ac462023-07-19 14:22:54 +0800378 ret = read(dev_fd, buf, sizeof(buf));
b.liu9e8584b2024-11-06 19:21:28 +0800379 if (ret < 0)
l.yangb7972c72024-10-15 22:34:51 -0700380 {
liubin281ac462023-07-19 14:22:54 +0800381 printf("read error\n");
hj.shaoccb8e212025-07-07 00:37:37 -0700382 continue;
liubin281ac462023-07-19 14:22:54 +0800383 }
384 alog_process(buf, ret, &entry);
l.yangb7972c72024-10-15 22:34:51 -0700385 android_log_printLogLine(&file_list, &entry);
liubin281ac462023-07-19 14:22:54 +0800386 memset(buf, 0, sizeof(buf));
387 }
388 close(dev_fd);
b.liu9e8584b2024-11-06 19:21:28 +0800389
liubin281ac462023-07-19 14:22:54 +0800390 printf("%s exit \n", __FUNCTION__);
391 pthread_exit(NULL);
392 return NULL;
393}