]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - logging.c
Move the stderr output of event log messages in verbose mode to the
[PuTTY.git] / logging.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <ctype.h>
4
5 #include <time.h>
6 #include <assert.h>
7
8 #include "putty.h"
9
10 /* log session to file stuff ... */
11 struct LogContext {
12     FILE *lgfp;
13     char currlogfilename[FILENAME_MAX];
14     void *frontend;
15 };
16
17 static void xlatlognam(char *d, char *s, char *hostname, struct tm *tm);
18
19 /*
20  * Log session traffic.
21  */
22 void logtraffic(void *handle, unsigned char c, int logmode)
23 {
24     struct LogContext *ctx = (struct LogContext *)handle;
25     if (cfg.logtype > 0) {
26         if (cfg.logtype == logmode) {
27             /* deferred open file from pgm start? */
28             if (!ctx->lgfp)
29                 logfopen(ctx);
30             if (ctx->lgfp)
31                 fputc(c, ctx->lgfp);
32         }
33     }
34 }
35
36 /*
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.
44  */
45 void log_eventlog(void *handle, char *event)
46 {
47     struct LogContext *ctx = (struct LogContext *)handle;
48     if ((flags & FLAG_STDERR) && (flags & FLAG_VERBOSE)) {
49         fprintf(stderr, "%s\n", event);
50         fflush(stderr);
51     }
52     if (cfg.logtype != LGTYP_PACKETS)
53         return;
54     if (!ctx->lgfp)
55         logfopen(ctx);
56     if (ctx->lgfp)
57         fprintf(ctx->lgfp, "Event Log: %s\r\n", event);
58 }
59
60 /*
61  * Log an SSH packet.
62  */
63 void log_packet(void *handle, int direction, int type,
64                 char *texttype, void *data, int len)
65 {
66     struct LogContext *ctx = (struct LogContext *)handle;
67     int i, j;
68     char dumpdata[80], smalldata[5];
69
70     if (cfg.logtype != LGTYP_PACKETS)
71         return;
72     if (!ctx->lgfp)
73         logfopen(ctx);
74     if (ctx->lgfp) {
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 : '.');
86             }
87             strcpy(dumpdata + 10+1+3*16+2+j, "\r\n");
88             fputs(dumpdata, ctx->lgfp);
89         }
90         fflush(ctx->lgfp);
91     }
92 }
93
94 /* open log file append/overwrite mode */
95 void logfopen(void *handle)
96 {
97     struct LogContext *ctx = (struct LogContext *)handle;
98     char buf[256];
99     time_t t;
100     struct tm tm;
101     char writemod[4];
102
103     /* Prevent repeat calls */
104     if (ctx->lgfp)
105         return;
106
107     if (!cfg.logtype)
108         return;
109     sprintf(writemod, "wb");           /* default to rewrite */
110
111     time(&t);
112     tm = *localtime(&t);
113
114     /* substitute special codes in file name */
115     xlatlognam(ctx->currlogfilename, cfg.logfilename,cfg.host, &tm);
116
117     ctx->lgfp = fopen(ctx->currlogfilename, "r");  /* file already present? */
118     if (ctx->lgfp) {
119         int i;
120         fclose(ctx->lgfp);
121         i = askappend(ctx->frontend, ctx->currlogfilename);
122         if (i == 1)
123             writemod[0] = 'a';         /* set append mode */
124         else if (i == 0) {             /* cancelled */
125             ctx->lgfp = NULL;
126             cfg.logtype = 0;           /* disable logging */
127             return;
128         }
129     }
130
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);
138
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);
148     }
149 }
150
151 void logfclose(void *handle)
152 {
153     struct LogContext *ctx = (struct LogContext *)handle;
154     if (ctx->lgfp) {
155         fclose(ctx->lgfp);
156         ctx->lgfp = NULL;
157     }
158 }
159
160 void *log_init(void *frontend)
161 {
162     struct LogContext *ctx = smalloc(sizeof(struct LogContext));
163     ctx->lgfp = NULL;
164     ctx->frontend = frontend;
165     return ctx;
166 }
167
168 /*
169  * translate format codes into time/date strings
170  * and insert them into log file name
171  *
172  * "&Y":YYYY   "&m":MM   "&d":DD   "&T":hhmm   "&h":<hostname>   "&&":&
173  */
174 static void xlatlognam(char *d, char *s, char *hostname, struct tm *tm) {
175     char buf[10], *bufp;
176     int size;
177     int len = FILENAME_MAX-1;
178
179     while (*s) {
180         /* Let (bufp, len) be the string to append. */
181         bufp = buf;                    /* don't usually override this */
182         if (*s == '&') {
183             char c;
184             s++;
185             size = 0;
186             if (*s) switch (c = *s++, tolower(c)) {
187               case 'y':
188                 size = strftime(buf, sizeof(buf), "%Y", tm);
189                 break;
190               case 'm':
191                 size = strftime(buf, sizeof(buf), "%m", tm);
192                 break;
193               case 'd':
194                 size = strftime(buf, sizeof(buf), "%d", tm);
195                 break;
196               case 't':
197                 size = strftime(buf, sizeof(buf), "%H%M%S", tm);
198                 break;
199               case 'h':
200                 bufp = hostname;
201                 size = strlen(bufp);
202                 break;
203               default:
204                 buf[0] = '&';
205                 size = 1;
206                 if (c != '&')
207                     buf[size++] = c;
208             }
209         } else {
210             buf[0] = *s++;
211             size = 1;
212         }
213         if (size > len)
214             size = len;
215         memcpy(d, bufp, size);
216         d += size;
217         len -= size;
218     }
219     *d = '\0';
220 }