Working on new server loop

This commit is contained in:
2014-08-07 21:36:37 +01:00
parent 40c29b190b
commit a52023a711
10 changed files with 209 additions and 13 deletions

View File

@@ -29,7 +29,7 @@ log* log_new(const char* name, FILE *output) {
l->name = calloc(strlen(name)+1, sizeof(char));
strcpy(l->name, name);
l->running = false;
l->output_closeonstop = false;
return l;
}
void log_delete(log *l) {
@@ -60,6 +60,21 @@ bool log_start(log *l) {
return true;
}
void log_stop(log *l) {
l->running = false;
close(l->pWrite);
if (pthread_equal(l->thread, pthread_self())!=0) {
pthread_detach(l->thread);
} else {
pthread_join(l->thread, NULL);
}
close(l->pRead);
if (l->output_closeonstop == true
&& l->output != stdout
&& l->output != stderr) {
fclose(l->output);
}
}
void*log_loop(void* arg) {
log *l = (log*)arg;
void** buf = calloc(1, sizeof(void*));
@@ -90,16 +105,6 @@ void*log_loop(void* arg) {
free(buf);
free(timestr);
}
void log_stop(log *l) {
l->running = false;
close(l->pWrite);
if (pthread_equal(l->thread, pthread_self())!=0) {
pthread_detach(l->thread);
} else {
pthread_join(l->thread, NULL);
}
close(l->pRead);
}
void log_write(log *l, log_level level, const char* message) {
if (l == NULL) {
fprintf(stderr, "%s\n", message);
@@ -110,6 +115,7 @@ void log_write(log *l, log_level level, const char* message) {
}
char* msgstr = calloc(strlen(message)+1, sizeof(char));
strcpy(msgstr, message);
log_msg *msg = log_msg_new(level, msgstr);
write(l->pWrite, &msg, sizeof(log_msg*));
}