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

Add timestamps functionality #32

Open
wants to merge 3 commits 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
1 change: 1 addition & 0 deletions 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
1 change: 1 addition & 0 deletions microcom.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ extern int debug;
extern int opt_force;
extern int listenonly;
extern char *answerback;
extern int timestamps;

struct cmd {
char *name;
Expand Down
38 changes: 32 additions & 6 deletions mux.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@

#define BUFSIZE 1024

struct timeval now;
struct tm *timeinfo;

char new_line = true;

/* 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 @@ -212,12 +217,33 @@ char *answerback;

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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation looks broken. Please fix.

Copy link
Author

@draganjovanovich draganjovanovich Aug 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used ClangFormat from VIM, and it automatically messed up this file for some reason (tab size indentation probably), i will manually reformat and commit.

Thanks.

if (buf[len - 1] == '\n' || buf[len -1] == '\r') {
new_line = true;
write(STDOUT_FILENO, buf, len);
} else {
if (new_line) {
new_line = false;
char tbuf[30];
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));
write(STDOUT_FILENO, buf, len);
} else {
write(STDOUT_FILENO, 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