Commit bf05625e authored by Jeroen Vreeken's avatar Jeroen Vreeken
Browse files

Better handle log lines comming from the controller

parent 105bde4c
......@@ -110,7 +110,6 @@ int main (int argc, char **argv)
{
int fd_listen;
int fd_log;
FILE *f_log;
int *fd_clients = NULL;
int nr_clients = 0;
int fd_logfile = -1;
......@@ -153,7 +152,6 @@ int main (int argc, char **argv)
ioctl(fd_listen, FIONBIO, &(int){ 1 });
fd_log = tcp_connect(dt_host_controller(), CTRL_LOG_PORT);
f_log = fdopen(fd_log, "r");
while (1) {
int fdnew;
......@@ -189,23 +187,43 @@ int main (int argc, char **argv)
entry->buffer[strlen(entry->buffer)] = '\n';
printf(entry->buffer);
entry->buflen = strlen(entry->buffer);
f_log = fdopen(fd_log, "r");
printf("fd_log: %d f_log %p\n", fd_log, f_log);
}
} else if (FD_ISSET(fd_log, &fd_rd)) {
char *s;
entry = log_entry_alloc();
static char s[LOG_ENTRY_SIZE];
static size_t pos;
static ssize_t ret;
printf("Going to wait for data\n");
s = fgets(entry->buffer, LOG_ENTRY_LEN, f_log);
if (s)
entry->buflen = strlen(entry->buffer);
printf("rb: %zd\n", entry->buflen);
if (pos >= LOG_ENTRY_LEN)
pos = 0;
ret = read(fd_log, s+pos, LOG_ENTRY_LEN - pos);
if (ret > 0) {
int i = pos;
printf("read %zd bytes\n", ret);
pos += ret;
printf("buffer now at %zd bytes\n", pos);
for (; i < pos; i++) {
if (s[i] == '\n') {
printf("enewline at %d\n", i);
entry = log_entry_alloc();
strncpy(entry->buffer, s, i+1);
entry->buffer[i+1] = 0;
entry->buflen = i+1;
printf("move %zd bytes back\n", pos - i);
memmove(s, s+i + 1, pos - i - 1);
pos -= i + 1;
i = -1;
}
}
}
if (!s) {
fclose(f_log);
if (ret < 0 && errno != EAGAIN && errno != EWOULDBLOCK) {
close(fd_log);
fd_log = -1;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment