Skip to content

Commit 7b763aa

Browse files
Msg:
Added timestamp option for STDOUT logs, on each line, option can be toggled from commands input Signed-off-by: Dragan Jovanović <[email protected]>
1 parent 927bda7 commit 7b763aa

File tree

4 files changed

+49
-7
lines changed

4 files changed

+49
-7
lines changed

commands.c

+14
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,15 @@ static int cmd_log(int argc, char *argv[])
189189
return ret;
190190
}
191191

192+
static int cmd_timestamp(int argc, char *argv[])
193+
{
194+
if(timestamps)
195+
timestamps = 0;
196+
else
197+
timestamps = 1;
198+
return MICROCOM_CMD_START;
199+
}
200+
192201
static int cmd_comment(int argc, char *argv[])
193202
{
194203
return 0;
@@ -245,6 +254,11 @@ static struct cmd cmds[] = {
245254
.name = "#",
246255
.fn = cmd_comment,
247256
.info = "comment",
257+
}, {
258+
.name = "timestamps",
259+
.fn = cmd_timestamp,
260+
.info = "turns on timestamps for each line of output",
261+
.help = "toggle on/off",
248262
},
249263
};
250264

microcom.c

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ static struct termios sots; /* old stdout/in termios settings to restore */
3636

3737
struct ios_ops *ios;
3838
int debug;
39+
int timestamps = 0;
3940

4041
void init_terminal(void)
4142
{

microcom.h

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ extern int debug;
7777
extern int opt_force;
7878
extern int listenonly;
7979
extern char *answerback;
80+
extern int timestamps;
8081

8182
struct cmd {
8283
char *name;

mux.c

+33-7
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525

2626
#define BUFSIZE 1024
2727

28+
struct timeval now;
29+
struct tm *timeinfo;
30+
31+
char new_line = true;
32+
2833
/* This is called with buf[-2:0] being IAC SB COM_PORT_OPTION */
2934
static int do_com_port_option(struct ios_ops *ios, unsigned char *buf, int len)
3035
{
@@ -210,14 +215,35 @@ static int do_subneg(struct ios_ops *ios, unsigned char *buf, int len)
210215
static int logfd = -1;
211216
char *answerback;
212217

213-
static void write_receive_buf(const unsigned char *buf, int len)
214-
{
215-
if (!len)
216-
return;
218+
static void write_receive_buf(const unsigned char *buf, int len) {
219+
if (!len) return;
220+
if (timestamps) {
221+
if (buf[0] == '\n' || buf[0] == '\r') {
222+
new_line = true;
223+
write(STDOUT_FILENO, buf, len);
224+
} else {
225+
if (new_line) {
226+
new_line = false;
227+
char tbuf[30];
228+
memset(tbuf, 0, sizeof(tbuf));
229+
gettimeofday(&now, NULL);
230+
timeinfo = gmtime(&now.tv_sec);
231+
snprintf(tbuf, sizeof(tbuf),
232+
"[%02d-%02d-%02d %02d:%02d:%02d:%03d] ",
233+
timeinfo->tm_mday, timeinfo->tm_mon + 1,
234+
timeinfo->tm_year + 1900, timeinfo->tm_hour,
235+
timeinfo->tm_min, timeinfo->tm_sec, now.tv_usec / 1000);
236+
write(STDOUT_FILENO, tbuf, strlen(tbuf));
237+
write(STDOUT_FILENO, buf, len);
238+
} else {
239+
write(STDOUT_FILENO, buf, len);
240+
}
241+
}
242+
} else {
243+
write(STDOUT_FILENO, buf, len);
244+
}
217245

218-
write(STDOUT_FILENO, buf, len);
219-
if (logfd >= 0)
220-
write(logfd, buf, len);
246+
if (logfd >= 0) write(logfd, buf, len);
221247
}
222248

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

0 commit comments

Comments
 (0)