blob: 161c5ab5833c3d70c4002bf34312df19d9838e3c [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;
63extern char radiolog_buff[MAX_BUFFER_SIZE];
64
liubin281ac462023-07-19 14:22:54 +080065void 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
74static 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
99static 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
122int 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
147int alog_process(char* data, int len, AndroidLogEntry *entry)
148{
149 int i, n = 0;
b.liu9e8584b2024-11-06 19:21:28 +0800150// int tmp_len;
liubin281ac462023-07-19 14:22:54 +0800151
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.yangf08c4a02024-11-24 21:18:22 -0800198 return -1;
199 //break;
liubin281ac462023-07-19 14:22:54 +0800200 }
201 n++;
202 }
203 }
204 return 0;
205}
206
207int android_log_printLogLine(
liubin281ac462023-07-19 14:22:54 +0800208 struct file_list_t *_file_list,
209 const AndroidLogEntry *entry)
210{
211 char priChar;
212 char timeBuf[32];
l.yang250444d2024-11-12 00:35:10 -0800213 char defaultBuffer[LOGGER_ENTRY_MAX_LEN] = {0};
liubin281ac462023-07-19 14:22:54 +0800214 size_t totalLen;
xf.li01e39822024-10-30 16:00:32 +0800215 int index = 0;
216 int len = 0;
l.yangb7972c72024-10-15 22:34:51 -0700217 struct stat s;
b.liu9e8584b2024-11-06 19:21:28 +0800218
l.yang67782e62024-11-05 01:28:10 -0800219
220 static char buffer[MAX_BUFFER_SIZE] = {0};
221 static int buffer_index = 0;
b.liu9e8584b2024-11-06 19:21:28 +0800222
l.yang250444d2024-11-12 00:35:10 -0800223
224 if(fcntl(fd_radio, F_GETFL) == -1 || access(tmp_log, W_OK) != 0)
l.yangb7972c72024-10-15 22:34:51 -0700225 {
226 fd_radio = open(tmp_log, O_CREAT | O_WRONLY| O_APPEND, 0600);
b.liu9e8584b2024-11-06 19:21:28 +0800227 if (fd_radio < 0)
l.yangb7972c72024-10-15 22:34:51 -0700228 {
229 fprintf(stderr, "failed to open %s: %s\n", tmp_log, strerror(errno));
230 exit(-1);
liubin281ac462023-07-19 14:22:54 +0800231 }
l.yang250444d2024-11-12 00:35:10 -0800232 tmp_rd_fd = fd_radio;
l.yangb7972c72024-10-15 22:34:51 -0700233 }
l.yang67782e62024-11-05 01:28:10 -0800234
235
l.yangb7972c72024-10-15 22:34:51 -0700236 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.liu9e8584b2024-11-06 19:21:28 +0800239 if (fd_radio < 0)
l.yangb7972c72024-10-15 22:34:51 -0700240 {
241 fprintf(stderr, "failed to open %s: %s\n", log_file, strerror(errno));
242 exit(-1);
243 }
l.yang250444d2024-11-12 00:35:10 -0800244 tmp_rd_fd = fd_radio;
245
l.yangb7972c72024-10-15 22:34:51 -0700246 }
liubin281ac462023-07-19 14:22:54 +0800247
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.liu9e8584b2024-11-06 19:21:28 +0800260
xf.li01e39822024-10-30 16:00:32 +0800261 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.yang250444d2024-11-12 00:35:10 -0800269
270 if(buffer_index + totalLen >= RADIOLOG_BUFF_SIZE && buffer_index < RADIOLOG_BUFF_SIZE)
l.yang67782e62024-11-05 01:28:10 -0800271 {
l.yang250444d2024-11-12 00:35:10 -0800272 memcpy(buffer + buffer_index, defaultBuffer, totalLen);
273 buffer_index += totalLen;
274 if (write(fd_radio, buffer, buffer_index) < 0)
l.yang67782e62024-11-05 01:28:10 -0800275 {
276 perror("write error");
l.yang250444d2024-11-12 00:35:10 -0800277 close(fd_radio);
l.yang67782e62024-11-05 01:28:10 -0800278 return -1;
279 }
280 buffer_index = 0; // Reset buffer index after flushing
281 }
l.yang67782e62024-11-05 01:28:10 -0800282 else
283 {
l.yang250444d2024-11-12 00:35:10 -0800284 memcpy(buffer + buffer_index, defaultBuffer, totalLen);
285 buffer_index += totalLen;
286 memcpy(radiolog_buff,buffer,buffer_index);
l.yang67782e62024-11-05 01:28:10 -0800287 }
b.liu9e8584b2024-11-06 19:21:28 +0800288
289
l.yang67782e62024-11-05 01:28:10 -0800290 return 0;
liubin281ac462023-07-19 14:22:54 +0800291}
292
293void* alog_thread(void* argv)
294{
295 int dev_fd, ret;
b.liu9e8584b2024-11-06 19:21:28 +0800296// int log_fd;
liubin281ac462023-07-19 14:22:54 +0800297 AndroidLogEntry entry;
l.yang250444d2024-11-12 00:35:10 -0800298 char buf[LOGGER_ENTRY_MAX_LEN] = {0};
liubin281ac462023-07-19 14:22:54 +0800299 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.li43643772024-03-04 19:39:53 -0800314 //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 }
liubin281ac462023-07-19 14:22:54 +0800325
b.liu9e8584b2024-11-06 19:21:28 +0800326 if (config->ip && config->port)
l.yangb7972c72024-10-15 22:34:51 -0700327 {
liubin281ac462023-07-19 14:22:54 +0800328 int port = atoi(config->port);
329 printf("%s %d : %s:%s\n", __FUNCTION__, __LINE__, config->ip, config->port);
b.liu9e8584b2024-11-06 19:21:28 +0800330 tcp_connect(config->ip, port);
331 }
332 else if (strlen(log_file) > 0)
l.yangb7972c72024-10-15 22:34:51 -0700333 {
334 //sprintf(tmp_log, "/tmp/log%s", strstr_tail(log_file, "/"));
b.liu9e8584b2024-11-06 19:21:28 +0800335
l.yangb7972c72024-10-15 22:34:51 -0700336 snprintf(tmp_log,sizeof(tmp_log), "%s", log_file);
liubin281ac462023-07-19 14:22:54 +0800337 // 先将文件保存到 /tmp/log/ 目录下,后面到达 rotate_file_size 后,转移到out_path
l.yang250444d2024-11-12 00:35:10 -0800338
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.yangb7972c72024-10-15 22:34:51 -0700348 {
b.liu9e8584b2024-11-06 19:21:28 +0800349 //log_fd = STDOUT_FILENO;
liubin281ac462023-07-19 14:22:54 +0800350 }
351 if(config->rotate_file_size)
l.yangb7972c72024-10-15 22:34:51 -0700352 {
liubin281ac462023-07-19 14:22:54 +0800353 log_size = config->rotate_file_size;
l.yangb7972c72024-10-15 22:34:51 -0700354 }
liubin281ac462023-07-19 14:22:54 +0800355 printf("android log start...\n");
b.liu9e8584b2024-11-06 19:21:28 +0800356 while (1)
l.yangb7972c72024-10-15 22:34:51 -0700357 {
liubin281ac462023-07-19 14:22:54 +0800358 ret = read(dev_fd, buf, sizeof(buf));
b.liu9e8584b2024-11-06 19:21:28 +0800359 if (ret < 0)
l.yangb7972c72024-10-15 22:34:51 -0700360 {
liubin281ac462023-07-19 14:22:54 +0800361 printf("read error\n");
362 break;
363 }
364 alog_process(buf, ret, &entry);
l.yangb7972c72024-10-15 22:34:51 -0700365 android_log_printLogLine(&file_list, &entry);
liubin281ac462023-07-19 14:22:54 +0800366 memset(buf, 0, sizeof(buf));
367 }
368 close(dev_fd);
b.liu9e8584b2024-11-06 19:21:28 +0800369
liubin281ac462023-07-19 14:22:54 +0800370 printf("%s exit \n", __FUNCTION__);
371 pthread_exit(NULL);
372 return NULL;
373}