#17 Add feature to prepend timestamp to output logfile lines

This commit is contained in:
Aaron Cook 2024-04-30 16:20:38 -05:00
commit c274a55c0e
4 changed files with 52 additions and 3 deletions

View file

@ -71,6 +71,9 @@ Configs get loaded from ${HOME}/.bytenuts/config (if file exists)
--inter_cmd_to=<ms> --inter_cmd_to=<ms>
Set the intercommand timeout in milliseconds (default is 10ms). Set the intercommand timeout in milliseconds (default is 10ms).
--time_fmt=<fmt>
Time format as used by strftime to prepend to every log line.
``` ```
## Navigation ## Navigation
@ -85,7 +88,7 @@ If the output window reaches the current line of output, then the output will co
## Configuration File ## Configuration File
There are only three supported configs currently. Each config is defined like `<name>=<value>`. Here is a sample config: Many of the launch options have a corresponding configuration token that can be saved to a config file. Each config is defined like `<name>=<value>`. Here is a sample config:
``` ```
colors=1 colors=1
@ -93,6 +96,7 @@ echo=0
no_crlf=0 no_crlf=0
escape=a escape=a
inter_cmd_to=100 inter_cmd_to=100
time_fmt=%X %m/%d %Z|>
``` ```
- `colors` - enable parsing of 8-bit ANSI color codes - `colors` - enable parsing of 8-bit ANSI color codes
@ -100,6 +104,7 @@ inter_cmd_to=100
- `no_crlf` - just send a line feed (`\n`) for user input rather than carriage return + line feed (`\r\n`) - `no_crlf` - just send a line feed (`\n`) for user input rather than carriage return + line feed (`\r\n`)
- `escape` - change what character is used as an escape sequence for commands (e.g. if set to `escape=a`, Bytenuts can be exited with `ctrl+a, q`) - `escape` - change what character is used as an escape sequence for commands (e.g. if set to `escape=a`, Bytenuts can be exited with `ctrl+a, q`)
- `inter_cmd_to` - Set a timeout in milliseconds that must be met. Useful for pasting in multiple lines and ensuring a short delay in between the commands. - `inter_cmd_to` - Set a timeout in milliseconds that must be met. Useful for pasting in multiple lines and ensuring a short delay in between the commands.
- `time_fmt` - The time format string (see `man 3 strftime`) to be prepended to every line in the log file (will not get printed in the console view)
Bytenuts looks for the configs at `~/.config/bytenuts/config`. Bytenuts looks for the configs at `~/.config/bytenuts/config`.

View file

@ -29,7 +29,8 @@
"--echo=<0|1>\n Turn input echoing off/on.\n\n" \ "--echo=<0|1>\n Turn input echoing off/on.\n\n" \
"--no_crlf=<0|1>\n Choose to send LF and not CRLF on input.\n\n" \ "--no_crlf=<0|1>\n Choose to send LF and not CRLF on input.\n\n" \
"--escape=<char>\n Change the default ctrl+b escape character.\n\n" \ "--escape=<char>\n Change the default ctrl+b escape character.\n\n" \
"--inter_cmd_to=<ms>\n Set the intercommand timeout in milliseconds (default is 10ms).\n" \ "--inter_cmd_to=<ms>\n Set the intercommand timeout in milliseconds (default is 10ms).\n\n" \
"--time_fmt=<fmt>\n Time format as used by strftime to prepend to every log line.\n" \
) )
static int parse_args(int argc, char **argv); static int parse_args(int argc, char **argv);
@ -191,6 +192,8 @@ bytenuts_print_stats()
cheerios_insert(st_line, strlen(st_line)); cheerios_insert(st_line, strlen(st_line));
sprintf(st_line, "inter_cmd_to: %d\r\n", bytenuts.config.inter_cmd_to); sprintf(st_line, "inter_cmd_to: %d\r\n", bytenuts.config.inter_cmd_to);
cheerios_insert(st_line, strlen(st_line)); cheerios_insert(st_line, strlen(st_line));
sprintf(st_line, "time_fmt: %s\r\n", bytenuts.config.time_fmt);
cheerios_insert(st_line, strlen(st_line));
return 0; return 0;
} }
@ -385,6 +388,10 @@ parse_args(int argc, char **argv)
bytenuts.config_overrides[4] = 1; bytenuts.config_overrides[4] = 1;
} }
} }
else if (arg_len > 11 && !memcmp(argv[i], "--time_fmt=", 11)) {
bytenuts.config.time_fmt = strdup(&argv[i][11]);
bytenuts.config_overrides[5] = 1;
}
else if (!strcmp(argv[i], "--resume") || !strcmp(argv[i], "-r")) { else if (!strcmp(argv[i], "--resume") || !strcmp(argv[i], "-r")) {
bytenuts.resume = 1; bytenuts.resume = 1;
} }
@ -438,6 +445,22 @@ load_configs()
bytenuts.config.inter_cmd_to = cmd_to; bytenuts.config.inter_cmd_to = cmd_to;
} }
} }
else if (!bytenuts.config_overrides[5] && !memcmp(line, "time_fmt=", 9)) {
size_t time_fmt_len;
bytenuts.config.time_fmt = strdup(&line[9]);
time_fmt_len = strlen(bytenuts.config.time_fmt);
while (
time_fmt_len > 0 &&
(
bytenuts.config.time_fmt[time_fmt_len-1] == '\r' ||
bytenuts.config.time_fmt[time_fmt_len-1] == '\n'
)
) {
bytenuts.config.time_fmt[time_fmt_len-1] = 0;
time_fmt_len--;
}
}
} }
return 0; return 0;

View file

@ -33,6 +33,9 @@ typedef struct bytenuts_config_struct {
char *log_path; /* path to the log file (if it exists) */ char *log_path; /* path to the log file (if it exists) */
char *serial_path; /* path to the target serial device */ char *serial_path; /* path to the target serial device */
uint32_t inter_cmd_to; /* inter command timeout in ms */ uint32_t inter_cmd_to; /* inter command timeout in ms */
/* time format to be prepended to all log lines in the output file only,
* NULL for no time prepended */
char *time_fmt;
} bytenuts_config_t; } bytenuts_config_t;
#define CONFIG_DEFAULT (bytenuts_config_t){ \ #define CONFIG_DEFAULT (bytenuts_config_t){ \
@ -45,12 +48,13 @@ typedef struct bytenuts_config_struct {
.log_path = NULL, \ .log_path = NULL, \
.serial_path = NULL, \ .serial_path = NULL, \
.inter_cmd_to = 10, \ .inter_cmd_to = 10, \
.time_fmt = NULL, \
} }
typedef struct bytenuts_struct { typedef struct bytenuts_struct {
serial_t serial_fd; serial_t serial_fd;
bytenuts_config_t config; bytenuts_config_t config;
int config_overrides[5]; int config_overrides[6];
int resume; int resume;
bytenuts_state_t state; bytenuts_state_t state;
WINDOW *status_win; WINDOW *status_win;

View file

@ -667,5 +667,22 @@ newline(line_buffer_t *lines)
lines->line_lens = realloc(lines->line_lens, sizeof(int) * lines->n_lines); lines->line_lens = realloc(lines->line_lens, sizeof(int) * lines->n_lines);
lines->line_lens[lines->n_lines - 1] = 0; lines->line_lens[lines->n_lines - 1] = 0;
lines->pos = 0; lines->pos = 0;
if (cheerios.config->time_fmt && (cheerios.log || cheerios.backup)) {
char tstr[128];
time_t now;
struct tm *tinfo;
size_t tstr_len;
time(&now);
tinfo = localtime(&now);
tstr_len = strftime(tstr, 128, cheerios.config->time_fmt, tinfo);
if (cheerios.log)
fwrite(tstr, 1, tstr_len, cheerios.log);
if (cheerios.backup)
fwrite(tstr, 1, tstr_len, cheerios.backup);
}
return 0; return 0;
} }