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