blob: 7b54051545ef8c222e292a73e5966aa83d73faf2 [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 <sys/un.h>
6#include <netinet/in.h>
7#include <netdb.h>
8#include <arpa/inet.h>
9#include <fcntl.h>
10#include <time.h>
11#include <unistd.h>
12#include <errno.h>
b.liu0f7ffad2024-11-06 19:57:27 +080013#include <stdlib.h>
14#include <pthread.h>
15
liubin281ac462023-07-19 14:22:54 +080016#include "log_config.h"
17
18#define _MOPEN_RILD_CONNET_NUM 5
19#define _MOPEN_RILD_SOCKET "/tmp/logd_socket"
20
21static int client_sockfd[_MOPEN_RILD_CONNET_NUM];
22
b.liu0f7ffad2024-11-06 19:57:27 +080023static char* log_file;
liubin281ac462023-07-19 14:22:54 +080024static int log_size = 1 * 1024 * 1024;
25
26static log_config_entry* config = NULL;
27static char tmp_log[48] = {0};
28
b.liu0f7ffad2024-11-06 19:57:27 +080029int tcp_connect(char* ip, int port);
30char *strstr_tail(const char *dst, const char *src);
31#if 0
liubin281ac462023-07-19 14:22:54 +080032static int fileter_log(int pri, char* tag, struct filter_list_t* filter)
33{
34 struct filter_list_t* _filter = filter;
35
36 while (_filter) {
37 // _filter->priority
38 // 获取 筛选的等级 p
39 int p = 0;
40 if (_filter->tag) {
41 int len = strlen(_filter->tag);
42 // tag and priority
43 if (0 == memcmp(_filter->tag, tag, len) && ((pri > p) || (pri == p))) {
44 return 0;
45 }
46 } else { // have no tag
47 if (pri < p) {
48 return -1;
49 } else {
50 return 0;
51 }
52 }
53 _filter = _filter->next;
54 }
55
56 return -1;
57}
b.liu0f7ffad2024-11-06 19:57:27 +080058#endif
liubin281ac462023-07-19 14:22:54 +080059
60int socket_log_print(
61 int fd,
62 struct file_list_t* _file_list,
63 char* entry)
64{
b.liu0f7ffad2024-11-06 19:57:27 +080065// char priChar;
liubin281ac462023-07-19 14:22:54 +080066 // char timeBuf[32];
67 // time_t timetemp; // 定义一个时间结构体变量
68 char defaultBuffer[512];
69 size_t totalLen;
70 int fd_new = fd;
71 struct stat s;
b.liu0f7ffad2024-11-06 19:57:27 +080072 int ret = 0;
liubin281ac462023-07-19 14:22:54 +080073
74 if (log_size && (!stat(tmp_log, &s)) && (s.st_size > log_size)) {
75 fd_new = get_rotate_file(fd_new, log_file, _file_list);
76 if (fd_new < 0) {
77 fprintf(stderr, "failed to open %s: %s\n", log_file, strerror(errno));
78 exit(-1);
79 }
80 }
81
82 // if(fileter_log(entry->priority, entry->tag, config->filter_list))
83 // {
84 // printf("%s %d: fileter pri:%d tag:%s!\n", __FUNCTION__, __LINE__, entry->priority, entry->tag);
85 // return -1;
86 // }
87 // time(&timetemp); // 获得时间参数
88 // struct tm* ptm = localtime(&timetemp);
89 // strftime(timeBuf, sizeof(timeBuf), "%Y-%m-%d %H:%M:%S", ptm);
90 totalLen = snprintf(defaultBuffer, sizeof(defaultBuffer), "%s", entry);
91
92 ret = write(fd_new, defaultBuffer, totalLen);
93
94 return ret;
95}
96
97static int open_local_socket(char *path)
98{
99 int listen_fd;
100 int ret;
101 struct sockaddr_un srv_addr;
102
103 listen_fd = socket(AF_UNIX, SOCK_STREAM, 0);
104 if (listen_fd < 0) {
105 printf("connect creat communication socket");
106 return -1;
107 }
108
109 unlink(path);
110 srv_addr.sun_family = AF_UNIX;
111 strcpy(srv_addr.sun_path, path);
112
113 ret = bind(listen_fd, (struct sockaddr*)&srv_addr, sizeof(srv_addr));
114 if (ret < 0) {
115 printf("cannot bind server socket");
116 close(listen_fd);
117 unlink(path);
118 return -1;
119 }
120
121 return listen_fd;
122}
123
124void* socket_log_thread(void* argv)
125{
126 int listen_fd;
127 int com_fd, fd_max, fd_num;
128 int ret, log_fd;
129 int i, n, str_len;
130 fd_set reads, cpy_reads;
131 struct timeval timeout;
132 char buf[512] = {0};
133 static struct file_list_t file_list;
134 config = (log_config_entry*)argv;
135
136 pthread_detach(pthread_self());
137 if (NULL == argv || NULL == config->name) {
138 return NULL;
139 }
140
141 memset(&file_list, 0, sizeof(struct file_list_t));
142 file_list.total = config->rotate_file_count;
143 log_file = config->out_path;
144
145 if (config->ip && config->port) {
146 int port = atoi(config->port);
147 printf("%s %d : %s:%s\n", __FUNCTION__, __LINE__, config->ip, config->port);
148 log_fd = tcp_connect(config->ip, port);
149 } else if (log_file) {
150 sprintf(tmp_log, "/tmp/log%s", strstr_tail(log_file, "/"));
151 // 先将文件保存到 /tmp/log/ 目录下,后面到达 rotate_file_size 后,转移到out_path
152 log_fd = open(tmp_log, O_CREAT | O_WRONLY | O_APPEND, 0600);
153 if (log_fd < 0) {
154 fprintf(stderr, "failed to open %s: %s\n", tmp_log, strerror(errno));
155 exit(-1);
156 }
157 } else {
158 log_fd = STDOUT_FILENO;
159 }
160 if (config->rotate_file_size) {
161 log_size = config->rotate_file_size;
162 }
163
164 listen_fd = open_local_socket(_MOPEN_RILD_SOCKET);
165 if (listen_fd < 0 || listen_fd == 0) {
166 return NULL;
167 }
168 ret = listen(listen_fd, 1);
169 if (ret < 0) {
170 printf("cannot listen sockfd");
171 close(listen_fd);
172 unlink(_MOPEN_RILD_SOCKET);
173 return NULL;
174 }
175 FD_ZERO(&reads);
176 FD_SET(listen_fd, &reads);
177 fd_max = listen_fd;
178 printf("socket log start...\n");
179 memset(client_sockfd, 0, sizeof(client_sockfd));
180 for (;;) {
181 cpy_reads = reads;
182 timeout.tv_sec = 5;
183 timeout.tv_usec = 5000;
184
185 if ((fd_num = select(fd_max + 1, &cpy_reads, 0, 0, &timeout)) == -1) {
186 perror("select error");
187 break;
188 }
189 if (fd_num == 0) {
190 continue;
191 }
192
193 for (n = 0; n < fd_max + 1; n++) {
194 if (FD_ISSET(n, &cpy_reads)) {
195 if (n == listen_fd) {
196 com_fd = accept(listen_fd, NULL, NULL);
197 FD_SET(com_fd, &reads);
198 if (fd_max < com_fd) {
199 fd_max = com_fd;
200 }
201 printf("accept accept fd [%d]", com_fd);
202 for (i = 0; i < _MOPEN_RILD_CONNET_NUM; i++) {
203 if (client_sockfd[i] <= 0) {
204 client_sockfd[i] = com_fd;
205 break;
206 }
207 }
208 } else {
209 str_len = read(n, buf, sizeof(buf));
210 if (str_len == 0) {
211 for (i = 0; i < _MOPEN_RILD_CONNET_NUM; i++) {
212 if (client_sockfd[i] == n) {
213 client_sockfd[i] = 0;
214 break;
215 }
216 }
217 FD_CLR(n, &reads);
218 close(n);
219 printf("closed client: %d \n", n);
220 } else {
221 socket_log_print(log_fd, &file_list, buf);
222 memset(buf, 0, sizeof(buf));
223 }
224 }
225 }
226 }
227 }
228 close(listen_fd);
229 close(log_fd);
230 unlink(_MOPEN_RILD_SOCKET);
231 printf("%s exit \n", __FUNCTION__);
232 pthread_exit(NULL);
233
234 return NULL;
235}