14 /* log session to file stuff ... */
17 enum { L_CLOSED, L_OPENING, L_OPEN, L_ERROR } state;
19 Filename currlogfilename;
24 static void xlatlognam(Filename *d, Filename s, char *hostname, struct tm *tm);
27 * Internal wrapper function which must be called for _all_ output
28 * to the log file. It takes care of opening the log file if it
29 * isn't open, buffering data if it's in the process of being
30 * opened asynchronously, etc.
32 static void logwrite(struct LogContext *ctx, void *data, int len)
35 * In state L_CLOSED, we call logfopen, which will set the state
36 * to one of L_OPENING, L_OPEN or L_ERROR. Hence we process all of
37 * those three _after_ processing L_CLOSED.
39 if (ctx->state == L_CLOSED)
42 if (ctx->state == L_OPENING) {
43 bufchain_add(&ctx->queue, data, len);
44 } else if (ctx->state == L_OPEN) {
46 fwrite(data, 1, len, ctx->lgfp);
47 } /* else L_ERROR, so ignore the write */
51 * Convenience wrapper on logwrite() which printf-formats the
54 static void logprintf(struct LogContext *ctx, const char *fmt, ...)
60 data = dupvprintf(fmt, ap);
63 logwrite(ctx, data, strlen(data));
68 * Flush any open log file.
70 void logflush(void *handle) {
71 struct LogContext *ctx = (struct LogContext *)handle;
72 if (ctx->cfg.logtype > 0)
73 if (ctx->state == L_OPEN)
77 static void logfopen_callback(void *handle, int mode)
79 struct LogContext *ctx = (struct LogContext *)handle;
80 char buf[256], *event;
85 ctx->state = L_ERROR; /* disable logging */
87 fmode = (mode == 1 ? "ab" : "wb");
88 ctx->lgfp = f_open(ctx->currlogfilename, fmode);
95 if (ctx->state == L_OPEN) {
96 /* Write header line into log file. */
98 strftime(buf, 24, "%Y.%m.%d %H:%M:%S", &tm);
99 logprintf(ctx, "=~=~=~=~=~=~=~=~=~=~=~= PuTTY log %s"
100 " =~=~=~=~=~=~=~=~=~=~=~=\r\n", buf);
103 event = dupprintf("%s session log (%s mode) to file: %s",
104 (mode == 0 ? "Disabled writing" :
105 mode == 1 ? "Appending" : "Writing new"),
106 (ctx->cfg.logtype == LGTYP_ASCII ? "ASCII" :
107 ctx->cfg.logtype == LGTYP_DEBUG ? "raw" :
108 ctx->cfg.logtype == LGTYP_PACKETS ? "SSH packets" :
110 filename_to_str(&ctx->currlogfilename));
111 logevent(ctx->frontend, event);
115 * Having either succeeded or failed in opening the log file,
116 * we should write any queued data out.
118 assert(ctx->state != L_OPENING); /* make _sure_ it won't be requeued */
119 while (bufchain_size(&ctx->queue)) {
122 bufchain_prefix(&ctx->queue, &data, &len);
123 logwrite(ctx, data, len);
124 bufchain_consume(&ctx->queue, len);
129 * Open the log file. Takes care of detecting an already-existing
130 * file and asking the user whether they want to append, overwrite
133 void logfopen(void *handle)
135 struct LogContext *ctx = (struct LogContext *)handle;
139 /* Prevent repeat calls */
140 if (ctx->state != L_CLOSED)
143 if (!ctx->cfg.logtype)
148 /* substitute special codes in file name */
149 xlatlognam(&ctx->currlogfilename, ctx->cfg.logfilename,ctx->cfg.host, &tm);
151 ctx->lgfp = f_open(ctx->currlogfilename, "r"); /* file already present? */
154 if (ctx->cfg.logxfovr != LGXF_ASK) {
155 mode = ((ctx->cfg.logxfovr == LGXF_OVR) ? 2 : 1);
157 mode = askappend(ctx->frontend, ctx->currlogfilename,
158 logfopen_callback, ctx);
160 mode = 2; /* create == overwrite */
163 ctx->state = L_OPENING;
165 logfopen_callback(ctx, mode); /* open the file */
168 void logfclose(void *handle)
170 struct LogContext *ctx = (struct LogContext *)handle;
175 ctx->state = L_CLOSED;
179 * Log session traffic.
181 void logtraffic(void *handle, unsigned char c, int logmode)
183 struct LogContext *ctx = (struct LogContext *)handle;
184 if (ctx->cfg.logtype > 0) {
185 if (ctx->cfg.logtype == logmode)
186 logwrite(ctx, &c, 1);
191 * Log an Event Log entry. Used in SSH packet logging mode; this is
192 * also as convenient a place as any to put the output of Event Log
193 * entries to stderr when a command-line tool is in verbose mode.
194 * (In particular, this is a better place to put it than in the
195 * front ends, because it only has to be done once for all
196 * platforms. Platforms which don't have a meaningful stderr can
197 * just avoid defining FLAG_STDERR.
199 void log_eventlog(void *handle, const char *event)
201 struct LogContext *ctx = (struct LogContext *)handle;
202 if ((flags & FLAG_STDERR) && (flags & FLAG_VERBOSE)) {
203 fprintf(stderr, "%s\n", event);
206 if (ctx->cfg.logtype != LGTYP_PACKETS)
208 logprintf(ctx, "Event Log: %s\r\n", event);
213 * If n_blanks != 0, blank or omit some parts.
214 * Set of blanking areas must be in increasing order.
216 void log_packet(void *handle, int direction, int type,
217 char *texttype, void *data, int len,
218 int n_blanks, const struct logblank_t *blanks)
220 struct LogContext *ctx = (struct LogContext *)handle;
221 char dumpdata[80], smalldata[5];
222 int p = 0, b = 0, omitted = 0;
223 int output_pos = 0; /* NZ if pending output in dumpdata */
225 if (ctx->cfg.logtype != LGTYP_PACKETS)
229 logprintf(ctx, "%s packet type %d / 0x%02x (%s)\r\n",
230 direction == PKT_INCOMING ? "Incoming" : "Outgoing",
231 type, type, texttype);
234 * Output a hex/ASCII dump of the packet body, blanking/omitting
235 * parts as specified.
240 /* Move to a current entry in the blanking array. */
241 while ((b < n_blanks) &&
242 (p >= blanks[b].offset + blanks[b].len))
244 /* Work out what type of blanking to apply to
246 blktype = PKTLOG_EMIT; /* default */
247 if ((b < n_blanks) &&
248 (p >= blanks[b].offset) &&
249 (p < blanks[b].offset + blanks[b].len))
250 blktype = blanks[b].type;
252 /* If we're about to stop omitting, it's time to say how
253 * much we omitted. */
254 if ((blktype != PKTLOG_OMIT) && omitted) {
255 logprintf(ctx, " (%d byte%s omitted)\r\n",
256 omitted, (omitted==1?"":"s"));
260 /* (Re-)initialise dumpdata as necessary
261 * (start of row, or if we've just stopped omitting) */
262 if (!output_pos && !omitted)
263 sprintf(dumpdata, " %08x%*s\r\n", p-(p%16), 1+3*16+2+16, "");
265 /* Deal with the current byte. */
266 if (blktype == PKTLOG_OMIT) {
270 if (blktype == PKTLOG_BLANK) {
272 sprintf(smalldata, "XX");
273 } else { /* PKTLOG_EMIT */
274 c = ((unsigned char *)data)[p];
275 sprintf(smalldata, "%02x", c);
277 dumpdata[10+2+3*(p%16)] = smalldata[0];
278 dumpdata[10+2+3*(p%16)+1] = smalldata[1];
279 dumpdata[10+1+3*16+2+(p%16)] = (isprint(c) ? c : '.');
280 output_pos = (p%16) + 1;
285 /* Flush row if necessary */
286 if (((p % 16) == 0) || (p == len) || omitted) {
288 strcpy(dumpdata + 10+1+3*16+2+output_pos, "\r\n");
289 logwrite(ctx, dumpdata, strlen(dumpdata));
298 logprintf(ctx, " (%d byte%s omitted)\r\n",
299 omitted, (omitted==1?"":"s"));
303 void *log_init(void *frontend, Config *cfg)
305 struct LogContext *ctx = snew(struct LogContext);
307 ctx->state = L_CLOSED;
308 ctx->frontend = frontend;
309 ctx->cfg = *cfg; /* STRUCTURE COPY */
310 bufchain_init(&ctx->queue);
314 void log_free(void *handle)
316 struct LogContext *ctx = (struct LogContext *)handle;
319 bufchain_clear(&ctx->queue);
323 void log_reconfig(void *handle, Config *cfg)
325 struct LogContext *ctx = (struct LogContext *)handle;
328 if (!filename_equal(ctx->cfg.logfilename, cfg->logfilename) ||
329 ctx->cfg.logtype != cfg->logtype)
330 reset_logging = TRUE;
332 reset_logging = FALSE;
337 ctx->cfg = *cfg; /* STRUCTURE COPY */
344 * translate format codes into time/date strings
345 * and insert them into log file name
347 * "&Y":YYYY "&m":MM "&d":DD "&T":hhmm "&h":<hostname> "&&":&
349 static void xlatlognam(Filename *dest, Filename src,
350 char *hostname, struct tm *tm) {
353 char buffer[FILENAME_MAX];
354 int len = sizeof(buffer)-1;
359 s = filename_to_str(&src);
362 /* Let (bufp, len) be the string to append. */
363 bufp = buf; /* don't usually override this */
368 if (*s) switch (c = *s++, tolower(c)) {
370 size = strftime(buf, sizeof(buf), "%Y", tm);
373 size = strftime(buf, sizeof(buf), "%m", tm);
376 size = strftime(buf, sizeof(buf), "%d", tm);
379 size = strftime(buf, sizeof(buf), "%H%M%S", tm);
397 memcpy(d, bufp, size);
403 *dest = filename_from_str(buffer);