10 /* log session to file stuff ... */
13 char currlogfilename[FILENAME_MAX];
17 static void xlatlognam(char *d, char *s, char *hostname, struct tm *tm);
20 * Log session traffic.
22 void logtraffic(void *handle, unsigned char c, int logmode)
24 struct LogContext *ctx = (struct LogContext *)handle;
25 if (cfg.logtype > 0) {
26 if (cfg.logtype == logmode) {
27 /* deferred open file from pgm start? */
37 * Log an Event Log entry. Used in SSH packet logging mode; this is
38 * also as convenient a place as any to put the output of Event Log
39 * entries to stderr when a command-line tool is in verbose mode.
40 * (In particular, this is a better place to put it than in the
41 * front ends, because it only has to be done once for all
42 * platforms. Platforms which don't have a meaningful stderr can
43 * just avoid defining FLAG_STDERR.
45 void log_eventlog(void *handle, char *event)
47 struct LogContext *ctx = (struct LogContext *)handle;
48 if ((flags & FLAG_STDERR) && (flags & FLAG_VERBOSE)) {
49 fprintf(stderr, "%s\n", event);
52 if (cfg.logtype != LGTYP_PACKETS)
57 fprintf(ctx->lgfp, "Event Log: %s\r\n", event);
63 void log_packet(void *handle, int direction, int type,
64 char *texttype, void *data, int len)
66 struct LogContext *ctx = (struct LogContext *)handle;
68 char dumpdata[80], smalldata[5];
70 if (cfg.logtype != LGTYP_PACKETS)
75 fprintf(ctx->lgfp, "%s packet type %d / 0x%02x (%s)\r\n",
76 direction == PKT_INCOMING ? "Incoming" : "Outgoing",
77 type, type, texttype);
78 for (i = 0; i < len; i += 16) {
79 sprintf(dumpdata, " %08x%*s\r\n", i, 1+3*16+2+16, "");
80 for (j = 0; j < 16 && i+j < len; j++) {
81 int c = ((unsigned char *)data)[i+j];
82 sprintf(smalldata, "%02x", c);
83 dumpdata[10+2+3*j] = smalldata[0];
84 dumpdata[10+2+3*j+1] = smalldata[1];
85 dumpdata[10+1+3*16+2+j] = (isprint(c) ? c : '.');
87 strcpy(dumpdata + 10+1+3*16+2+j, "\r\n");
88 fputs(dumpdata, ctx->lgfp);
94 /* open log file append/overwrite mode */
95 void logfopen(void *handle)
97 struct LogContext *ctx = (struct LogContext *)handle;
103 /* Prevent repeat calls */
109 sprintf(writemod, "wb"); /* default to rewrite */
114 /* substitute special codes in file name */
115 xlatlognam(ctx->currlogfilename, cfg.logfilename,cfg.host, &tm);
117 ctx->lgfp = fopen(ctx->currlogfilename, "r"); /* file already present? */
121 i = askappend(ctx->frontend, ctx->currlogfilename);
123 writemod[0] = 'a'; /* set append mode */
124 else if (i == 0) { /* cancelled */
126 cfg.logtype = 0; /* disable logging */
131 ctx->lgfp = fopen(ctx->currlogfilename, writemod);
132 if (ctx->lgfp) { /* enter into event log */
133 /* --- write header line into log file */
134 fputs("=~=~=~=~=~=~=~=~=~=~=~= PuTTY log ", ctx->lgfp);
135 strftime(buf, 24, "%Y.%m.%d %H:%M:%S", &tm);
136 fputs(buf, ctx->lgfp);
137 fputs(" =~=~=~=~=~=~=~=~=~=~=~=\r\n", ctx->lgfp);
139 sprintf(buf, "%s session log (%s mode) to file: ",
140 (writemod[0] == 'a') ? "Appending" : "Writing new",
141 (cfg.logtype == LGTYP_ASCII ? "ASCII" :
142 cfg.logtype == LGTYP_DEBUG ? "raw" :
143 cfg.logtype == LGTYP_PACKETS ? "SSH packets" : "<ukwn>"));
144 /* Make sure we do not exceed the output buffer size */
145 strncat(buf, ctx->currlogfilename, 128);
146 buf[strlen(buf)] = '\0';
147 logevent(ctx->frontend, buf);
151 void logfclose(void *handle)
153 struct LogContext *ctx = (struct LogContext *)handle;
160 void *log_init(void *frontend)
162 struct LogContext *ctx = smalloc(sizeof(struct LogContext));
164 ctx->frontend = frontend;
169 * translate format codes into time/date strings
170 * and insert them into log file name
172 * "&Y":YYYY "&m":MM "&d":DD "&T":hhmm "&h":<hostname> "&&":&
174 static void xlatlognam(char *d, char *s, char *hostname, struct tm *tm) {
177 int len = FILENAME_MAX-1;
180 /* Let (bufp, len) be the string to append. */
181 bufp = buf; /* don't usually override this */
186 if (*s) switch (c = *s++, tolower(c)) {
188 size = strftime(buf, sizeof(buf), "%Y", tm);
191 size = strftime(buf, sizeof(buf), "%m", tm);
194 size = strftime(buf, sizeof(buf), "%d", tm);
197 size = strftime(buf, sizeof(buf), "%H%M%S", tm);
215 memcpy(d, bufp, size);