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