blob: 5e861384b5a395a5677804c25f9a6efe8a80fe4a [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;
66
l.yang250444d2024-11-12 00:35:10 -080067
liubin281ac462023-07-19 14:22:54 +080068void hex_print(char* buf, int len)
69{
70 int i;
71
72 for (i = 0; i < len; i++) {
73 printf("%x ", buf[i]);
74 }
75}
76
77static char filterPriToChar(android_LogPriority pri)
78{
79 switch (pri) {
80 case ANDROID_LOG_VERBOSE:
81 return 'V';
82 case ANDROID_LOG_DEBUG:
83 return 'D';
84 case ANDROID_LOG_INFO:
85 return 'I';
86 case ANDROID_LOG_WARN:
87 return 'W';
88 case ANDROID_LOG_ERROR:
89 return 'E';
90 case ANDROID_LOG_FATAL:
91 return 'F';
92 case ANDROID_LOG_SILENT:
93 return 'S';
94
95 case ANDROID_LOG_DEFAULT:
96 case ANDROID_LOG_UNKNOWN:
97 default:
98 return '?';
99 }
100}
101
102static android_LogPriority filterCharToPri(char c)
103{
104 switch (c) {
105 case 'v':
106 return ANDROID_LOG_VERBOSE;
107 case 'd':
108 return ANDROID_LOG_DEBUG;
109 case 'i':
110 return ANDROID_LOG_INFO;
111 case 'w':
112 return ANDROID_LOG_WARN;
113 case 'e':
114 return ANDROID_LOG_ERROR;
115 case 'f':
116 return ANDROID_LOG_FATAL;
117 case 's':
118 return ANDROID_LOG_SILENT;
119 case '*':
120 default:
121 return ANDROID_LOG_VERBOSE;
122 }
123}
124
125int fileter_log(int pri, char *tag, struct filter_list_t *filter)
126{
127 struct filter_list_t *_filter = filter;
128
129 while(_filter)
130 {
131 int p = filterCharToPri(_filter->priority);
132 if(_filter->tag)
133 {
134 int len = strlen(_filter->tag);
135 // tag and priority
136 if(0 == memcmp(_filter->tag, tag, len) && ((pri > p) || (pri == p)))
137 return 0;
138 }else{ // have no tag
139 if(pri < p)
140 return -1;
141 else
142 return 0;
143 }
144 _filter = _filter->next;
145 }
146
147 return -1;
148}
149
150int alog_process(char* data, int len, AndroidLogEntry *entry)
151{
152 int i, n = 0;
b.liu9e8584b2024-11-06 19:21:28 +0800153// int tmp_len;
liubin281ac462023-07-19 14:22:54 +0800154
155 for (i = 0; i < len;) {
156 if (data[i] == '\0') {
157 i += 1;
158 } else {
159 switch (n) {
160 case 0: {
161 // printf("%d - %d: %x\n", i, tmp_len, data[i]);
162 i++;
163 break;
164 }
165 case 1: {
166 int* pid = (int*)&data[i];
167 entry->pid = *pid;
168 // printf("%d pid: %d\n", i, entry->pid);
169 i += 4;
170 break;
171 }
172 case 2: {
173 int* tid = (int*)&data[i];
174 entry->tid = *tid;
175 // printf("%d tid: %d\n", i, entry->tid);
176 i += 4;
177 break;
178 }
179 case 3: {
180 // printf("%d - %d: %x %x %x %x\n", i, tmp_len, data[i], data[i + 1], data[i + 2], data[i + 3]);
181 time_t* _t = (time_t*)&data[i];
182 entry->tv_sec = *_t;
183 i += 8;
184 break;
185 }
186 case 4: {
187 entry->priority = data[i];
188 entry->tag = &data[i + 1];
189 i += strlen(&data[i]);
190 break;
191 }
192 //* format: <priority:1><tag:N>\0<message:N>\0
193 case 5: {
194 entry->message = &data[i];
195 entry->messageLen = strlen(&data[i]);
196 i += entry->messageLen;
197 break;
198 }
199 default:
200 printf("process error \n");
l.yangf08c4a02024-11-24 21:18:22 -0800201 return -1;
202 //break;
liubin281ac462023-07-19 14:22:54 +0800203 }
204 n++;
205 }
206 }
207 return 0;
208}
209
210int android_log_printLogLine(
liubin281ac462023-07-19 14:22:54 +0800211 struct file_list_t *_file_list,
212 const AndroidLogEntry *entry)
213{
214 char priChar;
215 char timeBuf[32];
l.yang250444d2024-11-12 00:35:10 -0800216 char defaultBuffer[LOGGER_ENTRY_MAX_LEN] = {0};
liubin281ac462023-07-19 14:22:54 +0800217 size_t totalLen;
xf.li01e39822024-10-30 16:00:32 +0800218 int index = 0;
219 int len = 0;
l.yangb7972c72024-10-15 22:34:51 -0700220 struct stat s;
b.liu9e8584b2024-11-06 19:21:28 +0800221
l.yang67782e62024-11-05 01:28:10 -0800222
223 static char buffer[MAX_BUFFER_SIZE] = {0};
224 static int buffer_index = 0;
l.yangfa05af32024-11-27 00:00:56 -0800225 radio_globalPtr = buffer;
l.yang7ec80982024-12-15 23:34:35 -0800226
227 if(access("/etc/syslog_close", F_OK) == 0)
228 {
229 return 0;
230 }
231
l.yang250444d2024-11-12 00:35:10 -0800232 if(fcntl(fd_radio, F_GETFL) == -1 || access(tmp_log, W_OK) != 0)
l.yangb7972c72024-10-15 22:34:51 -0700233 {
234 fd_radio = open(tmp_log, O_CREAT | O_WRONLY| O_APPEND, 0600);
b.liu9e8584b2024-11-06 19:21:28 +0800235 if (fd_radio < 0)
l.yangb7972c72024-10-15 22:34:51 -0700236 {
237 fprintf(stderr, "failed to open %s: %s\n", tmp_log, strerror(errno));
238 exit(-1);
liubin281ac462023-07-19 14:22:54 +0800239 }
l.yang250444d2024-11-12 00:35:10 -0800240 tmp_rd_fd = fd_radio;
l.yangb7972c72024-10-15 22:34:51 -0700241 }
l.yang67782e62024-11-05 01:28:10 -0800242
243
l.yangb7972c72024-10-15 22:34:51 -0700244 if (log_size && (!stat(tmp_log, &s)) && (s.st_size > log_size))
245 {
246 fd_radio = get_rotate_file(fd_radio, log_file, _file_list);
b.liu9e8584b2024-11-06 19:21:28 +0800247 if (fd_radio < 0)
l.yangb7972c72024-10-15 22:34:51 -0700248 {
249 fprintf(stderr, "failed to open %s: %s\n", log_file, strerror(errno));
250 exit(-1);
251 }
l.yang250444d2024-11-12 00:35:10 -0800252 tmp_rd_fd = fd_radio;
253
l.yangb7972c72024-10-15 22:34:51 -0700254 }
liubin281ac462023-07-19 14:22:54 +0800255
256 if(fileter_log(entry->priority, entry->tag, config->filter_list))
257 {
258 // printf("%s %d: fileter pri:%d tag:%s!\n", __FUNCTION__, __LINE__, entry->priority, entry->tag);
259 return -1;
260 }
261
262 priChar = filterPriToChar(entry->priority);
263 struct tm* ptm = localtime(&entry->tv_sec);
264 strftime(timeBuf, sizeof(timeBuf), "%Y-%m-%d %H:%M:%S", ptm);
265
266 totalLen = snprintf(defaultBuffer, sizeof(defaultBuffer),
267 "%s %c/%s (%d): %s\n", timeBuf, priChar, entry->tag, entry->pid, entry->message);
b.liu9e8584b2024-11-06 19:21:28 +0800268
xf.li01e39822024-10-30 16:00:32 +0800269 len = strlen(defaultBuffer);
270 if(access("/etc/syslog_encrypt_flag", F_OK) == 0)
271 {
272 for(index = 0; index < len; index++)
273 {
274 defaultBuffer[index] ^= 1;
275 }
276 }
l.yang250444d2024-11-12 00:35:10 -0800277
278 if(buffer_index + totalLen >= RADIOLOG_BUFF_SIZE && buffer_index < RADIOLOG_BUFF_SIZE)
l.yang67782e62024-11-05 01:28:10 -0800279 {
l.yang250444d2024-11-12 00:35:10 -0800280 memcpy(buffer + buffer_index, defaultBuffer, totalLen);
281 buffer_index += totalLen;
282 if (write(fd_radio, buffer, buffer_index) < 0)
l.yang67782e62024-11-05 01:28:10 -0800283 {
284 perror("write error");
l.yang250444d2024-11-12 00:35:10 -0800285 close(fd_radio);
l.yang67782e62024-11-05 01:28:10 -0800286 return -1;
287 }
288 buffer_index = 0; // Reset buffer index after flushing
289 }
l.yang67782e62024-11-05 01:28:10 -0800290 else
291 {
l.yang250444d2024-11-12 00:35:10 -0800292 memcpy(buffer + buffer_index, defaultBuffer, totalLen);
293 buffer_index += totalLen;
l.yangfa05af32024-11-27 00:00:56 -0800294 //memcpy(radiolog_buff,buffer,buffer_index);
l.yang67782e62024-11-05 01:28:10 -0800295 }
b.liu9e8584b2024-11-06 19:21:28 +0800296
297
l.yang67782e62024-11-05 01:28:10 -0800298 return 0;
liubin281ac462023-07-19 14:22:54 +0800299}
300
301void* alog_thread(void* argv)
302{
303 int dev_fd, ret;
b.liu9e8584b2024-11-06 19:21:28 +0800304// int log_fd;
liubin281ac462023-07-19 14:22:54 +0800305 AndroidLogEntry entry;
l.yang250444d2024-11-12 00:35:10 -0800306 char buf[LOGGER_ENTRY_MAX_LEN] = {0};
liubin281ac462023-07-19 14:22:54 +0800307 static struct file_list_t file_list;
308 config = (log_config_entry *)argv;
309
310 pthread_detach(pthread_self());
311 if (NULL == argv)
312 return NULL;
313
314 dev_fd = open(ALOG_DEV, O_RDONLY, 0600);
315 if (dev_fd < 0) {
316 fprintf(stderr, "failed to open %s: %s\n", ALOG_DEV, strerror(errno));
317 exit(-1);
318 }
319
320 memset(&file_list, 0, sizeof(struct file_list_t));
321 file_list.total = config->rotate_file_count;
xf.li43643772024-03-04 19:39:53 -0800322 //log_file = config->out_path;
323 memset(log_file, 0, sizeof(log_file));
324 memset(log_ip, 0, sizeof(log_ip));
325 memset(log_port, 0, sizeof(log_port));
326 memset(log_prefix, 0, sizeof(log_prefix));
327 memset(pid_file, 0, sizeof(pid_file));
328 memset(hostname, 0, sizeof(hostname));
329 if(config->out_path != NULL)
330 {
331 strncpy(log_file, config->out_path, LOG_CONFIG_LEN - 1);
332 }
liubin281ac462023-07-19 14:22:54 +0800333
b.liu9e8584b2024-11-06 19:21:28 +0800334 if (config->ip && config->port)
l.yangb7972c72024-10-15 22:34:51 -0700335 {
liubin281ac462023-07-19 14:22:54 +0800336 int port = atoi(config->port);
337 printf("%s %d : %s:%s\n", __FUNCTION__, __LINE__, config->ip, config->port);
b.liu9e8584b2024-11-06 19:21:28 +0800338 tcp_connect(config->ip, port);
339 }
340 else if (strlen(log_file) > 0)
l.yangb7972c72024-10-15 22:34:51 -0700341 {
342 //sprintf(tmp_log, "/tmp/log%s", strstr_tail(log_file, "/"));
b.liu9e8584b2024-11-06 19:21:28 +0800343
l.yangb7972c72024-10-15 22:34:51 -0700344 snprintf(tmp_log,sizeof(tmp_log), "%s", log_file);
liubin281ac462023-07-19 14:22:54 +0800345 // 先将文件保存到 /tmp/log/ 目录下,后面到达 rotate_file_size 后,转移到out_path
l.yang250444d2024-11-12 00:35:10 -0800346
347 fd_radio = open(tmp_log, O_CREAT | O_WRONLY| O_APPEND, 0600);
348 if (fd_radio < 0)
349 {
350 fprintf(stderr, "failed to open %s: %s\n", tmp_log, strerror(errno));
351 exit(-1);
352 }
353 tmp_rd_fd = fd_radio;
354 }
355 else
l.yangb7972c72024-10-15 22:34:51 -0700356 {
b.liu9e8584b2024-11-06 19:21:28 +0800357 //log_fd = STDOUT_FILENO;
liubin281ac462023-07-19 14:22:54 +0800358 }
359 if(config->rotate_file_size)
l.yangb7972c72024-10-15 22:34:51 -0700360 {
liubin281ac462023-07-19 14:22:54 +0800361 log_size = config->rotate_file_size;
l.yangb7972c72024-10-15 22:34:51 -0700362 }
liubin281ac462023-07-19 14:22:54 +0800363 printf("android log start...\n");
b.liu9e8584b2024-11-06 19:21:28 +0800364 while (1)
l.yangb7972c72024-10-15 22:34:51 -0700365 {
liubin281ac462023-07-19 14:22:54 +0800366 ret = read(dev_fd, buf, sizeof(buf));
b.liu9e8584b2024-11-06 19:21:28 +0800367 if (ret < 0)
l.yangb7972c72024-10-15 22:34:51 -0700368 {
liubin281ac462023-07-19 14:22:54 +0800369 printf("read error\n");
370 break;
371 }
372 alog_process(buf, ret, &entry);
l.yangb7972c72024-10-15 22:34:51 -0700373 android_log_printLogLine(&file_list, &entry);
liubin281ac462023-07-19 14:22:54 +0800374 memset(buf, 0, sizeof(buf));
375 }
376 close(dev_fd);
b.liu9e8584b2024-11-06 19:21:28 +0800377
liubin281ac462023-07-19 14:22:54 +0800378 printf("%s exit \n", __FUNCTION__);
379 pthread_exit(NULL);
380 return NULL;
381}