Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timestamps feature added #38

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,15 @@ static int cmd_log(int argc, char *argv[])
return ret;
}

static int cmd_timestamp(int argc, char *argv[])
{
if(timestamps)
timestamps = 0;
else
timestamps = 1;
return MICROCOM_CMD_START;
}

static int cmd_comment(int argc, char *argv[])
{
return 0;
Expand Down Expand Up @@ -245,6 +254,11 @@ static struct cmd cmds[] = {
.name = "#",
.fn = cmd_comment,
.info = "comment",
}, {
.name = "timestamps",
.fn = cmd_timestamp,
.info = "turns on timestamps for each line of output",
.help = "toggle on/off",
},
};

Expand Down
8 changes: 7 additions & 1 deletion microcom.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ static struct termios sots; /* old stdout/in termios settings to restore */

struct ios_ops *ios;
int debug;
int timestamps = 0;

void init_terminal(void)
{
Expand Down Expand Up @@ -97,6 +98,7 @@ void main_usage(int exitcode, char *str, char *dev)
" -f, --force ignore existing lock file\n"
" -d, --debug output debugging info\n"
" -l, --logfile=<logfile> log output to <logfile>\n"
" -z, --timestamps Enable timestamps\n"
" -o, --listenonly Do not modify local terminal, do not send input\n"
" from stdin\n"
" -a, --answerback=<str> specify the answerback string sent as response to\n"
Expand Down Expand Up @@ -135,11 +137,12 @@ int main(int argc, char *argv[])
{ "logfile", required_argument, NULL, 'l'},
{ "listenonly", no_argument, NULL, 'o'},
{ "answerback", required_argument, NULL, 'a'},
{ "timestamps", no_argument, NULL, 'z' },
{ "version", no_argument, NULL, 'v' },
{ },
};

while ((opt = getopt_long(argc, argv, "hp:s:t:c:dfl:oi:a:v", long_options, NULL)) != -1) {
while ((opt = getopt_long(argc, argv, "hp:s:t:c:dfl:oi:a:vz", long_options, NULL)) != -1) {
switch (opt) {
case '?':
main_usage(1, "", "");
Expand Down Expand Up @@ -180,6 +183,9 @@ int main(int argc, char *argv[])
case 'a':
answerback = optarg;
break;
case 'z':
timestamps = 1;
break;
}
}

Expand Down
1 change: 1 addition & 0 deletions microcom.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ extern int debug;
extern int opt_force;
extern int listenonly;
extern char *answerback;
extern int timestamps;

struct cmd {
char *name;
Expand Down
39 changes: 33 additions & 6 deletions mux.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@
#include <arpa/inet.h>
#include <stdarg.h>
#include <stdbool.h>
#include <time.h>

#define BUFSIZE 1024

struct timeval now;
struct tm *timeinfo;

/* This is called with buf[-2:0] being IAC SB COM_PORT_OPTION */
static int do_com_port_option(struct ios_ops *ios, unsigned char *buf, int len)
{
Expand Down Expand Up @@ -210,14 +214,37 @@ static int do_subneg(struct ios_ops *ios, unsigned char *buf, int len)
static int logfd = -1;
char *answerback;

static void write_with_ts(const unsigned char *buf, int len) {
char tbuf[30];

for (int i = 0 ; i < len ; i++) {

write(STDOUT_FILENO, &buf[i], 1);
if (logfd >= 0) write(logfd, &buf[i], 1);
if (buf[i] == '\n') {
memset(tbuf, 0, sizeof(tbuf));
gettimeofday(&now, NULL);
timeinfo = gmtime(&now.tv_sec);
snprintf(tbuf, sizeof(tbuf),
"[%02d-%02d-%02d %02d:%02d:%02d:%03d] ",
timeinfo->tm_mday, timeinfo->tm_mon + 1,
timeinfo->tm_year + 1900, timeinfo->tm_hour,
timeinfo->tm_min, timeinfo->tm_sec, now.tv_usec / 1000);
write(STDOUT_FILENO, tbuf, strlen(tbuf));
if (logfd >= 0) write(logfd, tbuf, strlen(tbuf));
}
}
}

static void write_receive_buf(const unsigned char *buf, int len)
{
if (!len)
return;

write(STDOUT_FILENO, buf, len);
if (logfd >= 0)
write(logfd, buf, len);
if (!len) return;
if (timestamps) {
write_with_ts(buf, len);
} else {
write(STDOUT_FILENO, buf, len);
if (logfd >= 0) write(logfd, buf, len);
}
}

static int ios_printf(struct ios_ops *ios, const char *format, ...)
Expand Down