Package: picocom Severity: wishlist Tags: patch This is can be useful if you no need all output from picocom start, I hope this is will be useful.
-- System Information: Debian Release: 10.5 APT prefers stable APT policy: (500, 'stable') Architecture: i386 (i686) Kernel: Linux 4.19.0-10-686-pae (SMP w/1 CPU core) Locale: LANG=en_US.UTF-8, LC_CTYPE=en_US.UTF-8 (charmap=UTF-8), LANGUAGE=en_US.UTF-8 (charmap=UTF-8) Shell: /bin/sh linked to /bin/bash Init: systemd (via /run/systemd/system) Versions of packages picocom depends on: ii libc6 2.28-10 picocom recommends no packages. picocom suggests no packages.
>From 660dfad6caab1e2915d751074bfb23c3c8cf9d63 Mon Sep 17 00:00:00 2001 From: Brilliantov Kirill Vladimirovich <brillian...@inbox.ru> Date: Sun, 3 Jan 2021 12:38:31 +0300 Subject: [PATCH] Add switch-on/switch-off log file on-fly --- picocom.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/picocom.c b/picocom.c index 96021a1..1c57b3e 100644 --- a/picocom.c +++ b/picocom.c @@ -44,6 +44,8 @@ #include <dirent.h> #include <libgen.h> #endif +#include <sys/time.h> +#include <time.h> #define _GNU_SOURCE #include <getopt.h> @@ -105,6 +107,7 @@ const char *flow_str[] = { #define KEY_RECEIVE CKEY('r') /* receive file */ #define KEY_HEX CKEY('w') /* write hex */ #define KEY_BREAK CKEY('\\') /* break */ +#define KEY_TOG_LOG CKEY('l') /* toggle log */ /**********************************************************************/ @@ -211,6 +214,7 @@ struct { int omap; int emap; char *log_filename; + int log_arg; char *initstring; int exit_after; int exit; @@ -241,6 +245,7 @@ struct { .omap = M_O_DFL, .emap = M_E_DFL, .log_filename = NULL, + .log_arg = 0, .initstring = NULL, .exit_after = -1, .exit = 0, @@ -1050,6 +1055,8 @@ show_keys() KEYC(KEY_RECEIVE)); fd_printf(STO, "*** [C-%c] : Show port settings\r\n", KEYC(KEY_STATUS)); + fd_printf(STO, "*** [C-%c] : Toggle log if it doesn't defined via command line\r\n", + KEYC(KEY_TOG_LOG)); fd_printf(STO, "*** [C-%c] : Show this message\r\n", KEYC(KEY_HELP)); fd_printf(STO, "\r\n"); @@ -1378,6 +1385,52 @@ do_command (unsigned char c) term_break(tty_fd); fd_printf(STO, "\r\n*** break sent ***\r\n"); break; + case KEY_TOG_LOG: + if ( opts.log_arg ) + break; + else { + if ( opts.log_filename == NULL ) { + struct timeval tv; + struct tm *tm; + + if ( gettimeofday(&tv, NULL) ) { + fd_printf(STO, "*** failed get current timestamp ***\r\n"); + break; + } + tm = localtime(&tv.tv_sec); + if ( tm == NULL ) { + fd_printf(STO, "*** failed convert timestamp ***\r\n"); + break; + } + const char log_name[] = "picocom_20201231_235959.log"; + opts.log_filename = malloc(sizeof(log_name)); + if ( opts.log_filename == NULL ) { + fd_printf(STO, "*** failed allocate buffer **\r\n"); + break; + } + if ( strftime(opts.log_filename, sizeof(log_name), + "picocom_%Y%m%d_%H%M%S.log", tm) == 0 ) { + fd_printf(STO, "*** failed convert timestamp to string ***\r\n"); + break; + } + log_fd = open(opts.log_filename, + O_CREAT | O_RDWR | O_APPEND, + S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH); + if ( log_fd < 0 ) { + fd_printf(STO, "*** failed open log file %s ***\r\n", + opts.log_filename); + free(opts.log_filename); + opts.log_filename = NULL; + } + fd_printf(STO, "*** open log file %s ***\r\n", opts.log_filename); + } else { + close(log_fd); + fd_printf(STO, "*** close log file %s ***\r\n", opts.log_filename); + free(opts.log_filename); + opts.log_filename = NULL; + } + } + break; default: break; } @@ -1878,6 +1931,7 @@ parse_args(int argc, char *argv[]) case 'g': if ( opts.log_filename ) free(opts.log_filename); opts.log_filename = strdup(optarg); + opts.log_arg = 1; break; case 't': if ( opts.initstring ) free(opts.initstring); -- 2.29.2