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