putty 几乎是我用过的远程工具里面的最好的了。目前感觉还有的缺憾有
缺少标签页切换
没有命令窗口
log文件保存时没有保存时间线的选项
今天来动手为putty增加在保存的log的前面打上时间标签。
下载putty的源代码,假设你把它解压到putty
这个文件夹内。我们主要更改以下几个文件
- [putty\windows\winhelp.h](## winhelp.h)
- putty\putty.h
- putty\config.c
- putty\logging.c
UI上添加选项
winhelp.h
在 WINHELP_CTX_xxxxx 字段新增一条。这里我新增了一条
#define WINHELP_CTX_logging_timeheader "config-logtimeheader"
CTX 后面的 logging_timeheader
等下会用到。
putty.h
在 #define CONFIG_OPTIONS(X) \
字段新增一条,这里我新增的是
X(BOOL, NONE, logtimeheader) \
位置大概是1389行。
config.c
位置大概 1667 行,仿造原有的代码,添加一个checkbox。这里我写成
ctrl_checkbox(s, "Include time header of line", 'j',HELPCTX(logging_timeheader),conf_checkbox_handler, I(CONF_logtimeheader));
上面的代码中,HELPCTX(logging_timeheader)
里面的 logging_timeheader
是在 putty\windows\winhelp.h 新增的条目。
I(CONF_logtimeheader)
中的 logtimeheader
是在 putty\putty.h 中新增的条目。
功能实现
这一步的主逻辑是在 putty\logging.c 这个文件内添加的。修改位置为大概 54 行。static void logwrite(LogContext *ctx, ptrlen data) 这个函数体内。修改后如下所示
static void logwrite(LogContext *ctx, ptrlen data)
{
/*
* In state L_CLOSED, we call logfopen, which will set the state
* to one of L_OPENING, L_OPEN or L_ERROR. Hence we process all of
* those three _after_ processing L_CLOSED.
*/
if (ctx->state == L_CLOSED)
logfopen(ctx);
if (ctx->state == L_OPENING) {
bufchain_add(&ctx->queue, data.ptr, data.len);
} else if (ctx->state == L_OPEN) {
assert(ctx->lgfp);
if (fwrite(data.ptr, 1, data.len, ctx->lgfp) < data.len) {
logfclose(ctx);
ctx->state = L_ERROR;
lp_eventlog(ctx->lp, "Disabled writing session log "
"due to error while writing");
}
/************** 新增开始 **************/
if ((strcmp(data.ptr, "\n") == 0) && (conf_get_bool(ctx->conf, CONF_logtimeheader))) {
char buf[256];
struct tm tm;
tm = ltime();
strftime(buf, 24, "%Y.%m.%d %H:%M:%S ", &tm);
fwrite(buf, 1, strlen(buf), ctx->lgfp);
}
/*************** 新增结束 *************/
} /* else L_ERROR, so ignore the write */
}
logwrite 这个函数是用来将putty窗口内显示的字符输出到log文件内的。
如果调用这个函数的地方是一行一行传进来的的话,修改的地方应该是在调用这个函数的地方。但是根据调用的情况来看,多数情况下是一个字符一个字符写的。
所以我的做法是检查到输出的字符是 \n 时,就输出一个时间,这样下一行再输出的内容就是跟在这个时间后面的。
最后实现的效果大概是下面这个样子
转载请注明来源 https://bdznh.github.io/2020/06/21/add-new-config-into-putty/