10 /* log session to file stuff ... */
13 enum { L_CLOSED, L_OPENING, L_OPEN, L_ERROR } state;
15 Filename currlogfilename;
20 static void xlatlognam(Filename *d, Filename s, char *hostname, struct tm *tm);
23 * Internal wrapper function which must be called for _all_ output
24 * to the log file. It takes care of opening the log file if it
25 * isn't open, buffering data if it's in the process of being
26 * opened asynchronously, etc.
28 static void logwrite(struct LogContext *ctx, void *data, int len)
31 * In state L_CLOSED, we call logfopen, which will set the state
32 * to one of L_OPENING, L_OPEN or L_ERROR. Hence we process all of
33 * those three _after_ processing L_CLOSED.
35 if (ctx->state == L_CLOSED)
38 if (ctx->state == L_OPENING) {
39 bufchain_add(&ctx->queue, data, len);
40 } else if (ctx->state == L_OPEN) {
42 fwrite(data, 1, len, ctx->lgfp);
43 } /* else L_ERROR, so ignore the write */
47 * Convenience wrapper on logwrite() which printf-formats the
50 static void logprintf(struct LogContext *ctx, const char *fmt, ...)
56 data = dupvprintf(fmt, ap);
59 logwrite(ctx, data, strlen(data));
64 * Flush any open log file.
66 void logflush(void *handle) {
67 struct LogContext *ctx = (struct LogContext *)handle;
68 if (ctx->cfg.logtype > 0)
69 if (ctx->state == L_OPEN)
73 static void logfopen_callback(void *handle, int mode)
75 struct LogContext *ctx = (struct LogContext *)handle;
76 char buf[256], *event;
81 ctx->state = L_ERROR; /* disable logging */
83 fmode = (mode == 1 ? "ab" : "wb");
84 ctx->lgfp = f_open(ctx->currlogfilename, fmode);
91 if (ctx->state == L_OPEN) {
92 /* Write header line into log file. */
94 strftime(buf, 24, "%Y.%m.%d %H:%M:%S", &tm);
95 logprintf(ctx, "=~=~=~=~=~=~=~=~=~=~=~= PuTTY log %s"
96 " =~=~=~=~=~=~=~=~=~=~=~=\r\n", buf);
99 event = dupprintf("%s session log (%s mode) to file: %s",
100 (mode == 0 ? "Disabled writing" :
101 mode == 1 ? "Appending" : "Writing new"),
102 (ctx->cfg.logtype == LGTYP_ASCII ? "ASCII" :
103 ctx->cfg.logtype == LGTYP_DEBUG ? "raw" :
104 ctx->cfg.logtype == LGTYP_PACKETS ? "SSH packets" :
106 filename_to_str(&ctx->currlogfilename));
107 logevent(ctx->frontend, event);
111 * Having either succeeded or failed in opening the log file,
112 * we should write any queued data out.
114 assert(ctx->state != L_OPENING); /* make _sure_ it won't be requeued */
115 while (bufchain_size(&ctx->queue)) {
118 bufchain_prefix(&ctx->queue, &data, &len);
119 logwrite(ctx, data, len);
120 bufchain_consume(&ctx->queue, len);
125 * Open the log file. Takes care of detecting an already-existing
126 * file and asking the user whether they want to append, overwrite
129 void logfopen(void *handle)
131 struct LogContext *ctx = (struct LogContext *)handle;
135 /* Prevent repeat calls */
136 if (ctx->state != L_CLOSED)
139 if (!ctx->cfg.logtype)
144 /* substitute special codes in file name */
145 xlatlognam(&ctx->currlogfilename, ctx->cfg.logfilename,ctx->cfg.host, &tm);
147 ctx->lgfp = f_open(ctx->currlogfilename, "r"); /* file already present? */
150 if (ctx->cfg.logxfovr != LGXF_ASK) {
151 mode = ((ctx->cfg.logxfovr == LGXF_OVR) ? 2 : 1);
153 mode = askappend(ctx->frontend, ctx->currlogfilename,
154 logfopen_callback, ctx);
156 mode = 2; /* create == overwrite */
159 ctx->state = L_OPENING;
161 logfopen_callback(ctx, mode); /* open the file */
164 void logfclose(void *handle)
166 struct LogContext *ctx = (struct LogContext *)handle;
171 ctx->state = L_CLOSED;
175 * Log session traffic.
177 void logtraffic(void *handle, unsigned char c, int logmode)
179 struct LogContext *ctx = (struct LogContext *)handle;
180 if (ctx->cfg.logtype > 0) {
181 if (ctx->cfg.logtype == logmode)
182 logwrite(ctx, &c, 1);
187 * Log an Event Log entry. Used in SSH packet logging mode; this is
188 * also as convenient a place as any to put the output of Event Log
189 * entries to stderr when a command-line tool is in verbose mode.
190 * (In particular, this is a better place to put it than in the
191 * front ends, because it only has to be done once for all
192 * platforms. Platforms which don't have a meaningful stderr can
193 * just avoid defining FLAG_STDERR.
195 void log_eventlog(void *handle, const char *event)
197 struct LogContext *ctx = (struct LogContext *)handle;
198 if ((flags & FLAG_STDERR) && (flags & FLAG_VERBOSE)) {
199 fprintf(stderr, "%s\n", event);
202 if (ctx->cfg.logtype != LGTYP_PACKETS)
204 logprintf(ctx, "Event Log: %s\r\n", event);
209 * If n_blanks != 0, blank or omit some parts.
210 * Set of blanking areas must be in increasing order.
212 void log_packet(void *handle, int direction, int type,
213 char *texttype, void *data, int len,
214 int n_blanks, const struct logblank_t *blanks)
216 struct LogContext *ctx = (struct LogContext *)handle;
217 char dumpdata[80], smalldata[5];
218 int p = 0, b = 0, omitted = 0;
219 int output_pos = 0; /* NZ if pending output in dumpdata */
221 if (ctx->cfg.logtype != LGTYP_PACKETS)
225 logprintf(ctx, "%s packet type %d / 0x%02x (%s)\r\n",
226 direction == PKT_INCOMING ? "Incoming" : "Outgoing",
227 type, type, texttype);
230 * Output a hex/ASCII dump of the packet body, blanking/omitting
231 * parts as specified.
236 /* Move to a current entry in the blanking array. */
237 while ((b < n_blanks) &&
238 (p >= blanks[b].offset + blanks[b].len))
240 /* Work out what type of blanking to apply to
242 blktype = PKTLOG_EMIT; /* default */
243 if ((b < n_blanks) &&
244 (p >= blanks[b].offset) &&
245 (p < blanks[b].offset + blanks[b].len))
246 blktype = blanks[b].type;
248 /* If we're about to stop omitting, it's time to say how
249 * much we omitted. */
250 if ((blktype != PKTLOG_OMIT) && omitted) {
251 logprintf(ctx, " (%d byte%s omitted)\r\n",
252 omitted, (omitted==1?"":"s"));
256 /* (Re-)initialise dumpdata as necessary
257 * (start of row, or if we've just stopped omitting) */
258 if (!output_pos && !omitted)
259 sprintf(dumpdata, " %08x%*s\r\n", p-(p%16), 1+3*16+2+16, "");
261 /* Deal with the current byte. */
262 if (blktype == PKTLOG_OMIT) {
266 if (blktype == PKTLOG_BLANK) {
268 sprintf(smalldata, "XX");
269 } else { /* PKTLOG_EMIT */
270 c = ((unsigned char *)data)[p];
271 sprintf(smalldata, "%02x", c);
273 dumpdata[10+2+3*(p%16)] = smalldata[0];
274 dumpdata[10+2+3*(p%16)+1] = smalldata[1];
275 dumpdata[10+1+3*16+2+(p%16)] = (isprint(c) ? c : '.');
276 output_pos = (p%16) + 1;
281 /* Flush row if necessary */
282 if (((p % 16) == 0) || (p == len) || omitted) {
284 strcpy(dumpdata + 10+1+3*16+2+output_pos, "\r\n");
285 logwrite(ctx, dumpdata, strlen(dumpdata));
294 logprintf(ctx, " (%d byte%s omitted)\r\n",
295 omitted, (omitted==1?"":"s"));
299 void *log_init(void *frontend, Config *cfg)
301 struct LogContext *ctx = snew(struct LogContext);
303 ctx->state = L_CLOSED;
304 ctx->frontend = frontend;
305 ctx->cfg = *cfg; /* STRUCTURE COPY */
306 bufchain_init(&ctx->queue);
310 void log_free(void *handle)
312 struct LogContext *ctx = (struct LogContext *)handle;
315 bufchain_clear(&ctx->queue);
319 void log_reconfig(void *handle, Config *cfg)
321 struct LogContext *ctx = (struct LogContext *)handle;
324 if (!filename_equal(ctx->cfg.logfilename, cfg->logfilename) ||
325 ctx->cfg.logtype != cfg->logtype)
326 reset_logging = TRUE;
328 reset_logging = FALSE;
333 ctx->cfg = *cfg; /* STRUCTURE COPY */
340 * translate format codes into time/date strings
341 * and insert them into log file name
343 * "&Y":YYYY "&m":MM "&d":DD "&T":hhmm "&h":<hostname> "&&":&
345 static void xlatlognam(Filename *dest, Filename src,
346 char *hostname, struct tm *tm) {
349 char buffer[FILENAME_MAX];
350 int len = sizeof(buffer)-1;
355 s = filename_to_str(&src);
358 /* Let (bufp, len) be the string to append. */
359 bufp = buf; /* don't usually override this */
364 if (*s) switch (c = *s++, tolower(c)) {
366 size = strftime(buf, sizeof(buf), "%Y", tm);
369 size = strftime(buf, sizeof(buf), "%m", tm);
372 size = strftime(buf, sizeof(buf), "%d", tm);
375 size = strftime(buf, sizeof(buf), "%H%M%S", tm);
393 memcpy(d, bufp, size);
399 *dest = filename_from_str(buffer);