-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathw.cpp
More file actions
219 lines (179 loc) · 3.77 KB
/
w.cpp
File metadata and controls
219 lines (179 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <curses.h>
#include <string.h>
#include "w.h"
pthread_t tid_http_log;
pthread_t tid_http_stat;
pthread_mutex_t _lock_L1;
pthread_mutex_t _lock_R1;
#define CMD_HTTP_LOG "tail -n 1 /usr/local/httpd/logs/error_log"
#define FILE_HTTP_LOG "/usr/local/httpd/logs/access_log"
#define CMD_HTTP_STAT "sh showlogerror.sh"
static int running = 1;
static Win *win_L1 = NULL;
static Win *win_R1 = NULL;
void update_win(Win *win, char *log, int clean)
{
int len = 0;
int i = 0;
len = strlen(log);
for (i=len - 1; i>0 && len>0; i--) {
if (log[i] == '\n' || log[i] == '\r') {
log[i] = '\0';
continue;
}
break;
}
if (NULL == win) {
printf("%s[%d]\n", log, (int)strlen(log));
return;
}
len = strlen(log);
if (len >= win->locate.w) {
log[win->locate.w] = '\0';
}
if (clean) {
wclear(win->win);
}
wprintw(win->win, "%s\n", log);
wrefresh(win->win);
}
void lock_R1()
{
pthread_mutex_lock(&_lock_R1);
}
void unlock_R1()
{
pthread_mutex_unlock(&_lock_R1);
}
void lock_L1()
{
pthread_mutex_lock(&_lock_L1);
}
void unlock_L1()
{
pthread_mutex_unlock(&_lock_L1);
}
void *http_log(void *arg)
{
char *ret = NULL;
FILE *fp = NULL;
char buf[1024] = {0};\
int lastpos = 0;
// TODO: Read from config file
fp = fopen(FILE_HTTP_LOG, "r");
fseek(fp, lastpos, SEEK_END);
lastpos = ftell(fp);
fclose(fp);
fp = NULL;
while(running) {
if (NULL == fp) {
usleep(100);
fp = fopen(FILE_HTTP_LOG, "r");
fseek(fp, lastpos, SEEK_SET);
}
ret = fgets(buf, sizeof(buf), fp);
lastpos = ftell(fp);
// TODO:Save to config file
if (NULL == ret) {
fclose(fp);
fp = NULL;
continue;
}
lock_L1();
update_win(win_L1, buf, 0);
unlock_L1();
}
/*
printf("thread\n");
fp = popen(CMD_HTTP_LOG, "r");
while(running) {
fread(buf, sizeof(char), sizeof(buf), fp);
printf("%s\n", buf);
}
*/
/*
char buffer[1024] = {0};
FILE *fp = NULL;
int fd = 0;
int wd = 0;
fp = fopen(CMD_HTTP_LOG, "r");
if (fp < 0) {
goto quit;
}
fd = inotify_init();
wd = inotify_add_watch(fd, CMD_HTTP_LOG, IN_MODIFY);
while(running) {
read(fp, buffer, sizeof(buffer));
printf("%s\n", buffer);
pthread_mutex_lock(&lock_http);
pthread_mutex_unlock(&lock_http);
}
quit:
*/
return (void *)0;
}
void *http_stat(void *arg)
{
FILE *fp = NULL;
char buf[256] = {0};
int ret = 0;
while(running) {
fp = popen(CMD_HTTP_STAT, "r");
ret = fread(buf, sizeof(char), sizeof(buf), fp);
lock_R1();
update_win(win_R1, buf, 1);
unlock_R1();
pclose(fp);
fp = NULL;
sleep(10);
}
return (void *)0;
}
void wait_work_thread()
{
int ret = 0;
void *tret;
ret = pthread_join(tid_http_log, &tret);
if (ret != 0) {
}
}
int start_work_thread(Win *L1, Win *R1)
{
int ret = 0;
win_L1 = L1;
win_R1 = R1;
start_http_log();
start_http_stat();
start_free_mem();
return ret;
}
int start_http_log()
{
int ret = 0;
ret = pthread_create(&tid_http_log, NULL, http_log, NULL);
return ret;
}
int start_http_stat()
{
int ret = 0;
ret = pthread_create(&tid_http_stat, NULL, http_stat, NULL);
return ret;
}
int start_free_mem()
{
int ret = 0;
return ret;
}
#ifdef DEBUG_MAIN
int main()
{
start_work_thread(NULL, NULL);
sleep(3);
running = 0;
// wait_work_thread();
return 0;
}
#endif