Skip to content

Commit

Permalink
#164 refactor server startup to use log_cap_lines
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-courtis committed May 7, 2024
1 parent a74ef4a commit dfa42b6
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 53 deletions.
21 changes: 6 additions & 15 deletions inc/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ struct LogCapLine {
char *line;
enum LogThreshold threshold;
};
extern struct SList *log_cap_lines;


void log_set_threshold(enum LogThreshold threshold, bool cli);
Expand Down Expand Up @@ -46,25 +45,17 @@ void log_suppress_start(void);
void log_suppress_stop(void);


void log_capture_start(void);

void log_capture_stop(void);

void log_capture_clear(void);

// NULL plays back log_cap_lines
void log_capture_playback(struct SList *lines);


// caller must call stop and free lines
void log_cap_lines_start(struct SList **lines);
void log_cap_lines_start(struct SList **log_cap_lines);

void log_cap_lines_stop(struct SList **log_cap_lines);

void log_cap_lines_stop(struct SList **lines);
void log_cap_lines_playback(struct SList *log_cap_lines);

void log_cap_lines_write(struct SList **lines, const char *path);
void log_cap_lines_write(struct SList **log_cap_lines, const char *path);


void log_cap_line_free(void *line);
void log_cap_line_free(void *log_cap_line);

#endif // LOG_H

4 changes: 2 additions & 2 deletions src/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ int handle_responses(const struct IpcRequest *ipc_request) {
fprintf(stdout, "%s\n", yaml);
} else {
// human errors
log_capture_playback(response->log_cap_lines);
log_cap_lines_playback(response->log_cap_lines);
}
} else {
// human
log_capture_playback(response->log_cap_lines);
log_cap_lines_playback(response->log_cap_lines);
}
}
slist_free_vals(&responses, ipc_response_free);
Expand Down
10 changes: 5 additions & 5 deletions src/layout.c
Original file line number Diff line number Diff line change
Expand Up @@ -299,17 +299,17 @@ void apply(void) {

} else {

struct SList *cap_lines = NULL;
struct SList *log_cap_lines = NULL;

log_cap_lines_start(&cap_lines);
log_cap_lines_start(&log_cap_lines);

print_heads(INFO, DELTA, heads);

// TODO unique file passed to the user
log_cap_lines_stop(&cap_lines);
log_cap_lines_write(&cap_lines, "/tmp/wd.delta");
log_cap_lines_stop(&log_cap_lines);
log_cap_lines_write(&log_cap_lines, "/tmp/wd.delta");

slist_free_vals(&cap_lines, log_cap_line_free);
slist_free_vals(&log_cap_lines, log_cap_line_free);

// all changes except mode
for (i = heads_changing; i; i = i->nex) {
Expand Down
32 changes: 6 additions & 26 deletions src/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,16 @@ struct LogActive {
enum LogThreshold threshold;
bool threshold_cli;
bool times;
bool capturing;
bool suppressing;
};
struct LogActive active = {
.threshold = LOG_THRESHOLD_DEFAULT,
.threshold_cli = false,
.times = false,
.capturing = false,
.suppressing = false,
};

// TODO merge these
struct SList *log_cap_lines = NULL;
struct SList *log_cap_lineses = NULL;
struct SList *log_cap_lines_active = NULL;

char threshold_char[] = {
'?',
Expand Down Expand Up @@ -101,11 +97,7 @@ void print_line(enum LogThreshold threshold, bool prefix, int eno, const char *_
sprintf(l + LS - 4, "...");
}

if (active.capturing) {
capture_line(&log_cap_lines, threshold, l);
}

for (struct SList *i = log_cap_lineses; i; i = i->nex) {
for (struct SList *i = log_cap_lines_active; i; i = i->nex) {
capture_line(i->val, threshold, l);
}

Expand Down Expand Up @@ -195,21 +187,9 @@ void log_suppress_stop(void) {
active.suppressing = false;
}

void log_capture_start(void) {
active.capturing = true;
}

void log_capture_stop(void) {
active.capturing = false;
}

void log_capture_clear(void) {
slist_free_vals(&log_cap_lines, log_cap_line_free);
}

void log_capture_playback(struct SList *lines) {
void log_cap_lines_playback(struct SList *lines) {
if (!lines)
lines = log_cap_lines;
return;

for (struct SList *i = lines; i; i = i->nex) {
struct LogCapLine *line = i->val;
Expand All @@ -221,11 +201,11 @@ void log_capture_playback(struct SList *lines) {
}

void log_cap_lines_start(struct SList **lines) {
slist_append(&log_cap_lineses, lines);
slist_append(&log_cap_lines_active, lines);
}

void log_cap_lines_stop(struct SList **lines) {
slist_remove_all(&log_cap_lineses, NULL, lines);
slist_remove_all(&log_cap_lines_active, NULL, lines);
}

void log_cap_lines_write(struct SList **lines, const char *path) {
Expand Down
9 changes: 5 additions & 4 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,8 @@ server(char *cfg_path) {
setup_signal_handlers();

// don't log anything until cfg log level is known
log_capture_start();
struct SList *log_cap_lines = NULL;
log_cap_lines_start(&log_cap_lines);
log_suppress_start();

log_info("way-displays version %s", VERSION);
Expand All @@ -254,9 +255,9 @@ server(char *cfg_path) {
// play back captured logs from cfg parse
log_set_threshold(cfg->log_threshold, false);
log_suppress_stop();
log_capture_stop();
log_capture_playback(NULL);
log_capture_clear();
log_cap_lines_stop(&log_cap_lines);
log_cap_lines_playback(log_cap_lines);
slist_free_vals(&log_cap_lines, log_cap_line_free);

// discover the lid state immediately
lid_init();
Expand Down
1 change: 0 additions & 1 deletion tst/tst-marshalling.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ int before_each(void **state) {
}

int after_each(void **state) {
log_capture_clear();
assert_logs_empty();
cfg_free(cfg);
cfg = NULL;
Expand Down

0 comments on commit dfa42b6

Please sign in to comment.