blob: 68eda603e8299a564380c5d5bb0e08e25cf7696c [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.liu0f7ffad2024-11-06 19:57:27 +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.liu0f7ffad2024-11-06 19:57:27 +080017#include "mbtk_utils.h"
liubin281ac462023-07-19 14:22:54 +080018
19#define ALOG_DEV "/dev/log_radio"
20
b.liu0f7ffad2024-11-06 19:57:27 +080021static char *log_file;
liubin281ac462023-07-19 14:22:54 +080022static int log_size = 1 * 1024 * 1024;
23
24static log_config_entry *config = NULL;
25static char tmp_log[48] = {0};
26
b.liu0f7ffad2024-11-06 19:57:27 +080027char *strstr_tail(const char *dst, const char *src);
28int tcp_connect(char* ip, int port);
liubin281ac462023-07-19 14:22:54 +080029
b.liu0f7ffad2024-11-06 19:57:27 +080030#if 0
liubin281ac462023-07-19 14:22:54 +080031static int fileter_log(int pri, char *tag, struct filter_list_t *filter)
32{
33 struct filter_list_t *_filter = filter;
34
35 while(_filter)
36 {
37 // _filter->priority
38 // 获取 筛选的等级 p
39 int p = 0;
40 if(_filter->tag)
41 {
42 int len = strlen(_filter->tag);
43 // tag and priority
44 if(0 == memcmp(_filter->tag, tag, len) && ((pri > p) || (pri == p)))
45 return 0;
46 }else{ // have no tag
47 if(pri < p)
48 return -1;
49 else
50 return 0;
51 }
52 _filter = _filter->next;
53 }
54
55 return -1;
56}
b.liu0f7ffad2024-11-06 19:57:27 +080057#endif
liubin281ac462023-07-19 14:22:54 +080058
59int common_log_print(
60 int fd,
61 struct file_list_t *_file_list,
62 char *entry)
63{
b.liu0f7ffad2024-11-06 19:57:27 +080064// char priChar;
liubin281ac462023-07-19 14:22:54 +080065 char timeBuf[32];
66 char defaultBuffer[512];
67 size_t totalLen;
68 int fd_new = fd;
69 struct stat s;
70 time_t timetemp; // 定义一个时间结构体变量
b.liu0f7ffad2024-11-06 19:57:27 +080071 int ret = 0;
liubin281ac462023-07-19 14:22:54 +080072
73 if (log_size && (!stat(tmp_log, &s)) && (s.st_size > log_size)) {
74 fd_new = get_rotate_file(fd_new, log_file, _file_list);
75 if (fd_new < 0) {
76 fprintf(stderr, "failed to open %s: %s\n", log_file, strerror(errno));
77 exit(-1);
78 }
79 }
80
81 // if(fileter_log(entry->priority, entry->tag, config->filter_list))
82 // {
83 // printf("%s %d: fileter pri:%d tag:%s!\n", __FUNCTION__, __LINE__, entry->priority, entry->tag);
84 // return -1;
85 // }
86 time(&timetemp); // 获得时间参数
87 struct tm* ptm = localtime(&timetemp);
88 strftime(timeBuf, sizeof(timeBuf), "%Y-%m-%d %H:%M:%S", ptm);
89
90 totalLen = snprintf(defaultBuffer, sizeof(defaultBuffer),
91 "%s : %s\n", timeBuf, entry);
92
93 ret = write(fd_new, defaultBuffer, totalLen);
94
95 return ret;
96}
97
98void* common_log_thread(void* argv)
99{
100 int dev_fd, ret;
101 int log_fd;
102 char buf[512] = {0};
103 static struct file_list_t file_list;
104 config = (log_config_entry *)argv;
105
106 pthread_detach(pthread_self());
107 if (NULL == argv || NULL == config->name)
108 return NULL;
109
110 dev_fd = open(config->name, O_RDONLY, 0600);
111 if (dev_fd < 0) {
112 fprintf(stderr, "failed to open %s: %s\n", config->name, strerror(errno));
113 exit(-1);
114 }
115
116 memset(&file_list, 0, sizeof(struct file_list_t));
117 file_list.total = config->rotate_file_count;
118 log_file = config->out_path;
119
120 if (config->ip && config->port) {
121 int port = atoi(config->port);
122 printf("%s %d : %s:%s\n", __FUNCTION__, __LINE__, config->ip, config->port);
123 log_fd = tcp_connect(config->ip, port);
124 } else if (log_file) {
125 sprintf(tmp_log, "/tmp/log%s", strstr_tail(log_file, "/"));
126 // 先将文件保存到 /tmp/log/ 目录下,后面到达 rotate_file_size 后,转移到out_path
127 log_fd = open(tmp_log, O_CREAT | O_WRONLY| O_APPEND, 0600);
128 if (log_fd < 0) {
129 fprintf(stderr, "failed to open %s: %s\n", tmp_log, strerror(errno));
130 exit(-1);
131 }
132 } else {
133 log_fd = STDOUT_FILENO;
134 }
135 if(config->rotate_file_size)
136 log_size = config->rotate_file_size;
137
138 printf("uart log start...\n");
139 while (1) {
140 ret = read(dev_fd, buf, sizeof(buf));
141 if (ret < 0) {
142 printf("read error\n");
143 break;
144 }
145 // common_log_process(buf, ret, &entry);
146 common_log_print(log_fd, &file_list, buf);
147 memset(buf, 0, sizeof(buf));
148 }
149 close(dev_fd);
150 close(log_fd);
151
152 printf("%s exit \n", __FUNCTION__);
153 pthread_exit(NULL);
154 return NULL;
155}