Skip to content

Commit

Permalink
added --logfile option
Browse files Browse the repository at this point in the history
as discussed in npat-efault#24
  • Loading branch information
Joe Merten committed Dec 8, 2016
1 parent 74e0245 commit f92e8e3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
10 changes: 10 additions & 0 deletions picocom.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,16 @@ Picocom accepts the following command-line options.
local-echo is enabled). See
**[INPUT, OUTPUT, AND ECHO MAPPING]**. (Defaul: **delbs,crcrlf**)

**--logfile** | **-g**

: Use specified file for logging (recording) serial input, and
possibly serial output. If the file exists, it is appended to.
Every character read from the serial port is written to the
specified file (before input mapping is performed). If local-echo
mode is is enabled (see --echo option and C-c command), then every
character written to the serial port (after output mapping is
performed) is also logged to the same file.

**--lower-rts**

: Lower the RTS control signal after opening the serial port (by
Expand Down
34 changes: 33 additions & 1 deletion picocom.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ struct {
int imap;
int omap;
int emap;
char *log_filename;
int lower_rts;
int lower_dtr;
} opts = {
Expand All @@ -211,6 +212,7 @@ struct {
.imap = M_I_DFL,
.omap = M_O_DFL,
.emap = M_E_DFL,
.log_filename = NULL,
.lower_rts = 0,
.lower_dtr = 0
};
Expand All @@ -221,6 +223,7 @@ int sig_exit = 0;
#define STI STDIN_FILENO

int tty_fd;
int log_fd = -1;

#ifndef TTY_Q_SZ
#define TTY_Q_SZ 256
Expand Down Expand Up @@ -357,6 +360,11 @@ fatal (const char *format, ...)
uucp_unlock();
#endif

if (opts.log_filename) {
free(opts.log_filename);
close(log_fd);
}

exit(EXIT_FAILURE);
}

Expand Down Expand Up @@ -1182,6 +1190,9 @@ loop(void)
} else {
int i;
char *bmp = &buff_map[0];
if ( opts.log_filename )
if ( writen_ni(log_fd, buff_rd, n) < n )
fatal("write to logfile failed: %s", strerror(errno));
for (i = 0; i < n; i++) {
bmp += do_map(bmp, opts.imap, buff_rd[i]);
}
Expand All @@ -1202,6 +1213,9 @@ loop(void)
} while ( n < 0 && errno == EINTR );
if ( n <= 0 )
fatal("write to term failed: %s", strerror(errno));
if ( opts.lecho && opts.log_filename )
if ( writen_ni(log_fd, tty_q.buff, sz) < sz )
fatal("write to logfile failed: %s", strerror(errno));
memmove(tty_q.buff, tty_q.buff + n, tty_q.len - n);
tty_q.len -= n;
}
Expand Down Expand Up @@ -1294,6 +1308,7 @@ show_usage(char *name)
printf(" --imap <map> (input mappings)\n");
printf(" --omap <map> (output mappings)\n");
printf(" --emap <map> (local-echo mappings)\n");
printf(" --lo<g>file <filename>\n");
printf(" --lower-rts\n");
printf(" --lower-dtr\n");
printf(" --<h>elp\n");
Expand Down Expand Up @@ -1337,6 +1352,7 @@ parse_args(int argc, char *argv[])
{"parity", required_argument, 0, 'y'},
{"databits", required_argument, 0, 'd'},
{"stopbits", required_argument, 0, 'p'},
{"logfile", required_argument, 0, 'g'},
{"lower-rts", no_argument, 0, 'R'},
{"lower-dtr", no_argument, 0, 'D'},
{"help", no_argument, 0, 'h'},
Expand All @@ -1352,7 +1368,7 @@ parse_args(int argc, char *argv[])
/* no default error messages printed. */
opterr = 0;

c = getopt_long(argc, argv, "hirlcv:s:r:e:f:b:y:d:p:",
c = getopt_long(argc, argv, "hirlcv:s:r:e:f:b:y:d:p:g:",
longOptions, &optionIndex);

if (c < 0)
Expand Down Expand Up @@ -1477,6 +1493,10 @@ parse_args(int argc, char *argv[])
break;
}
break;
case 'g':
opts.log_filename = malloc(strlen(optarg) * sizeof(char));
strcpy(opts.log_filename, optarg);
break;
case 'R':
opts.lower_rts = 1;
break;
Expand Down Expand Up @@ -1537,6 +1557,7 @@ parse_args(int argc, char *argv[])
printf("imap is : "); print_map(opts.imap);
printf("omap is : "); print_map(opts.omap);
printf("emap is : "); print_map(opts.emap);
printf("logfile is : %s\n", opts.log_filename ? opts.log_filename : "none");
printf("\n");
#endif /* of NO_HELP */
}
Expand All @@ -1563,6 +1584,12 @@ main(int argc, char *argv[])
fatal("cannot lock %s: %s", opts.port, strerror(errno));
#endif

if (opts.log_filename) {
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)
fatal("cannot open %s: %s", opts.log_filename, strerror(errno));
}

tty_fd = open(opts.port, O_RDWR | O_NONBLOCK | O_NOCTTY);
if (tty_fd < 0)
fatal("cannot open %s: %s", opts.port, strerror(errno));
Expand Down Expand Up @@ -1652,6 +1679,11 @@ main(int argc, char *argv[])
uucp_unlock();
#endif

if (opts.log_filename) {
free(opts.log_filename);
close(log_fd);
}

return EXIT_SUCCESS;
}

Expand Down

0 comments on commit f92e8e3

Please sign in to comment.