Skip to content

Commit

Permalink
#164 write human delta to /tmp/wd.delta on all changes, WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-courtis committed May 6, 2024
1 parent d449c03 commit d30fd2c
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 32 deletions.
16 changes: 15 additions & 1 deletion inc/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ struct LogCapLine {
};
extern struct SList *log_cap_lines;


void log_set_threshold(enum LogThreshold threshold, bool cli);

void log_set_times(bool times);


void log_(enum LogThreshold threshold, const char *__restrict __format, ...);


void log_debug(const char *__restrict __format, ...);

void log_info(const char *__restrict __format, ...);
Expand All @@ -37,10 +40,12 @@ void log_error(const char *__restrict __format, ...);

void log_error_errno(const char *__restrict __format, ...);


void log_suppress_start(void);

void log_suppress_stop(void);


void log_capture_start(void);

void log_capture_stop(void);
Expand All @@ -50,7 +55,16 @@ void log_capture_clear(void);
// NULL plays back log_cap_lines
void log_capture_playback(struct SList *lines);

void log_cap_line_free(void *log_cap_line);

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

void log_cap_lines_stop(struct SList **lines);

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


void log_cap_line_free(void *line);

#endif // LOG_H

6 changes: 4 additions & 2 deletions src/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ bool mkdir_p(char *path, mode_t mode) {
}

bool file_write(const char *path, const char *contents, const char *mode) {
if (!path || !contents) {
if (!path) {
return false;
}

Expand All @@ -51,7 +51,9 @@ bool file_write(const char *path, const char *contents, const char *mode) {
return false;
}

fprintf(f, "%s\n", contents);
if (contents) {
fprintf(f, "%s\n", contents);
}

fflush(f);

Expand Down
22 changes: 7 additions & 15 deletions src/layout.c
Original file line number Diff line number Diff line change
Expand Up @@ -299,25 +299,17 @@ void apply(void) {

} else {

// TODO provide a means to capture into a provided list or buffer or file, as other captures are going on
log_capture_clear();
log_capture_start();
struct SList *cap_lines = NULL;

print_heads(INFO, DELTA, heads);
log_cap_lines_start(&cap_lines);

log_capture_stop();
print_heads(INFO, DELTA, heads);

// TODO unique file name, pass it to the callback
if (file_write("/tmp/wd.delta", "", "w")) {
for (struct SList *i = log_cap_lines; i; i = i->nex) {
struct LogCapLine *cap_line = (struct LogCapLine*)i->val;
if (strlen(cap_line->line) > 0) {
file_write("/tmp/wd.delta", cap_line->line, "a");
}
}
}
// TODO unique file passed to the user
log_cap_lines_stop(&cap_lines);
log_cap_lines_write(&cap_lines, "/tmp/wd.delta");

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

// all changes except mode
for (i = heads_changing; i; i = i->nex) {
Expand Down
56 changes: 42 additions & 14 deletions src/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "log.h"

#include "fs.h"
#include "slist.h"

#define LS 16384
Expand All @@ -27,7 +28,9 @@ struct LogActive active = {
.suppressing = false,
};

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

char threshold_char[] = {
'?',
Expand Down Expand Up @@ -56,11 +59,11 @@ void print_time(enum LogThreshold threshold, FILE *__restrict __stream) {
fprintf(__stream, "%c [%s] ", threshold_char[threshold], buf);
}

void capture_line(enum LogThreshold threshold, char *l) {
struct LogCapLine *cap_line = calloc(1, sizeof(struct LogCapLine));
cap_line->line = strdup(l);
cap_line->threshold = threshold;
slist_append(&log_cap_lines, cap_line);
void capture_line(struct SList **lines, enum LogThreshold threshold, char *l) {
struct LogCapLine *line = calloc(1, sizeof(struct LogCapLine));
line->line = strdup(l);
line->threshold = threshold;
slist_append(lines, line);
}

void print_raw(enum LogThreshold threshold, bool prefix, const char *l) {
Expand Down Expand Up @@ -99,7 +102,11 @@ void print_line(enum LogThreshold threshold, bool prefix, int eno, const char *_
}

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

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

print_raw(threshold, prefix, l);
Expand Down Expand Up @@ -205,25 +212,46 @@ void log_capture_playback(struct SList *lines) {
lines = log_cap_lines;

for (struct SList *i = lines; i; i = i->nex) {
struct LogCapLine *cap_line = i->val;
if (!cap_line)
struct LogCapLine *line = i->val;
if (!line)
continue;

print_raw(cap_line->threshold, true, cap_line->line);
print_raw(line->threshold, true, line->line);
}
}

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

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

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

// write empty to clear and validate
if (path && file_write(path, NULL, "w")) {
for (struct SList *i = *lines; i; i = i->nex) {
struct LogCapLine *line = (struct LogCapLine*)i->val;
if (strlen(line->line) > 0) {
file_write(path, line->line, "a");
}
}
}
}

void log_cap_line_free(void *data) {
struct LogCapLine *cap_line = data;
struct LogCapLine *line = data;

if (!cap_line) {
if (!line) {
return;
}

if (cap_line->line) {
free(cap_line->line);
if (line->line) {
free(line->line);
}

free(cap_line);
free(line);
}

0 comments on commit d30fd2c

Please sign in to comment.