10 /* log session to file stuff ... */
13 Filename currlogfilename;
18 static void xlatlognam(Filename *d, Filename s, char *hostname, struct tm *tm);
21 * Log session traffic.
23 void logtraffic(void *handle, unsigned char c, int logmode)
25 struct LogContext *ctx = (struct LogContext *)handle;
26 if (ctx->cfg.logtype > 0) {
27 if (ctx->cfg.logtype == logmode) {
28 /* deferred open file from pgm start? */
38 * Flush any open log file.
40 void logflush(void *handle) {
41 struct LogContext *ctx = (struct LogContext *)handle;
42 if (ctx->cfg.logtype > 0)
48 * Log an Event Log entry. Used in SSH packet logging mode; this is
49 * also as convenient a place as any to put the output of Event Log
50 * entries to stderr when a command-line tool is in verbose mode.
51 * (In particular, this is a better place to put it than in the
52 * front ends, because it only has to be done once for all
53 * platforms. Platforms which don't have a meaningful stderr can
54 * just avoid defining FLAG_STDERR.
56 void log_eventlog(void *handle, const char *event)
58 struct LogContext *ctx = (struct LogContext *)handle;
59 if ((flags & FLAG_STDERR) && (flags & FLAG_VERBOSE)) {
60 fprintf(stderr, "%s\n", event);
63 if (ctx->cfg.logtype != LGTYP_PACKETS)
68 fprintf(ctx->lgfp, "Event Log: %s\r\n", event);
74 void log_packet(void *handle, int direction, int type,
75 char *texttype, void *data, int len)
77 struct LogContext *ctx = (struct LogContext *)handle;
79 char dumpdata[80], smalldata[5];
81 if (ctx->cfg.logtype != LGTYP_PACKETS)
86 fprintf(ctx->lgfp, "%s packet type %d / 0x%02x (%s)\r\n",
87 direction == PKT_INCOMING ? "Incoming" : "Outgoing",
88 type, type, texttype);
89 for (i = 0; i < len; i += 16) {
90 sprintf(dumpdata, " %08x%*s\r\n", i, 1+3*16+2+16, "");
91 for (j = 0; j < 16 && i+j < len; j++) {
92 int c = ((unsigned char *)data)[i+j];
93 sprintf(smalldata, "%02x", c);
94 dumpdata[10+2+3*j] = smalldata[0];
95 dumpdata[10+2+3*j+1] = smalldata[1];
96 dumpdata[10+1+3*16+2+j] = (isprint(c) ? c : '.');
98 strcpy(dumpdata + 10+1+3*16+2+j, "\r\n");
99 fputs(dumpdata, ctx->lgfp);
105 /* open log file append/overwrite mode */
106 void logfopen(void *handle)
108 struct LogContext *ctx = (struct LogContext *)handle;
114 /* Prevent repeat calls */
118 if (!ctx->cfg.logtype)
120 sprintf(writemod, "wb"); /* default to rewrite */
125 /* substitute special codes in file name */
126 xlatlognam(&ctx->currlogfilename, ctx->cfg.logfilename,ctx->cfg.host, &tm);
128 ctx->lgfp = f_open(ctx->currlogfilename, "r"); /* file already present? */
132 if (ctx->cfg.logxfovr != LGXF_ASK) {
133 i = ((ctx->cfg.logxfovr == LGXF_OVR) ? 2 : 1);
135 i = askappend(ctx->frontend, ctx->currlogfilename);
137 writemod[0] = 'a'; /* set append mode */
138 else if (i == 0) { /* cancelled */
140 ctx->cfg.logtype = 0; /* disable logging */
145 ctx->lgfp = f_open(ctx->currlogfilename, writemod);
146 if (ctx->lgfp) { /* enter into event log */
147 /* --- write header line into log file */
148 fputs("=~=~=~=~=~=~=~=~=~=~=~= PuTTY log ", ctx->lgfp);
149 strftime(buf, 24, "%Y.%m.%d %H:%M:%S", &tm);
150 fputs(buf, ctx->lgfp);
151 fputs(" =~=~=~=~=~=~=~=~=~=~=~=\r\n", ctx->lgfp);
153 sprintf(buf, "%s session log (%s mode) to file: ",
154 (writemod[0] == 'a') ? "Appending" : "Writing new",
155 (ctx->cfg.logtype == LGTYP_ASCII ? "ASCII" :
156 ctx->cfg.logtype == LGTYP_DEBUG ? "raw" :
157 ctx->cfg.logtype == LGTYP_PACKETS ? "SSH packets" : "<ukwn>"));
158 /* Make sure we do not exceed the output buffer size */
159 strncat(buf, filename_to_str(&ctx->currlogfilename), 128);
160 buf[strlen(buf)] = '\0';
161 logevent(ctx->frontend, buf);
165 void logfclose(void *handle)
167 struct LogContext *ctx = (struct LogContext *)handle;
174 void *log_init(void *frontend, Config *cfg)
176 struct LogContext *ctx = snew(struct LogContext);
178 ctx->frontend = frontend;
179 ctx->cfg = *cfg; /* STRUCTURE COPY */
183 void log_free(void *handle)
185 struct LogContext *ctx = (struct LogContext *)handle;
191 void log_reconfig(void *handle, Config *cfg)
193 struct LogContext *ctx = (struct LogContext *)handle;
196 if (!filename_equal(ctx->cfg.logfilename, cfg->logfilename) ||
197 ctx->cfg.logtype != cfg->logtype)
198 reset_logging = TRUE;
200 reset_logging = FALSE;
205 ctx->cfg = *cfg; /* STRUCTURE COPY */
212 * translate format codes into time/date strings
213 * and insert them into log file name
215 * "&Y":YYYY "&m":MM "&d":DD "&T":hhmm "&h":<hostname> "&&":&
217 static void xlatlognam(Filename *dest, Filename src,
218 char *hostname, struct tm *tm) {
221 char buffer[FILENAME_MAX];
222 int len = sizeof(buffer)-1;
227 s = filename_to_str(&src);
230 /* Let (bufp, len) be the string to append. */
231 bufp = buf; /* don't usually override this */
236 if (*s) switch (c = *s++, tolower(c)) {
238 size = strftime(buf, sizeof(buf), "%Y", tm);
241 size = strftime(buf, sizeof(buf), "%m", tm);
244 size = strftime(buf, sizeof(buf), "%d", tm);
247 size = strftime(buf, sizeof(buf), "%H%M%S", tm);
265 memcpy(d, bufp, size);
271 *dest = filename_from_str(buffer);