10 /* log session to file stuff ... */
11 static FILE *lgfp = NULL;
12 static char currlogfilename[FILENAME_MAX];
14 static void xlatlognam(char *d, char *s, char *hostname, struct tm *tm);
17 * Log session traffic.
19 void logtraffic(unsigned char c, int logmode)
21 if (cfg.logtype > 0) {
22 if (cfg.logtype == logmode) {
23 /* deferred open file from pgm start? */
33 * Log an Event Log entry (used in SSH packet logging mode).
35 void log_eventlog(char *event)
37 if (cfg.logtype != LGTYP_PACKETS)
42 fprintf(lgfp, "Event Log: %s\n", event);
48 void log_packet(int direction, int type, char *texttype, void *data, int len)
51 char dumpdata[80], smalldata[5];
53 if (cfg.logtype != LGTYP_PACKETS)
58 fprintf(lgfp, "%s packet type %d / 0x%02x (%s)\n",
59 direction == PKT_INCOMING ? "Incoming" : "Outgoing",
60 type, type, texttype);
61 for (i = 0; i < len; i += 16) {
62 sprintf(dumpdata, " %08x%*s\n", i, 1+3*16+2+16, "");
63 for (j = 0; j < 16 && i+j < len; j++) {
64 int c = ((unsigned char *)data)[i+j];
65 sprintf(smalldata, "%02x", c);
66 dumpdata[10+2+3*j] = smalldata[0];
67 dumpdata[10+2+3*j+1] = smalldata[1];
68 dumpdata[10+1+3*16+2+j] = (isprint(c) ? c : '.');
70 strcpy(dumpdata + 10+1+3*16+2+j, "\n");
71 fputs(dumpdata, lgfp);
77 /* open log file append/overwrite mode */
85 /* Prevent repeat calls */
91 sprintf(writemod, "wb"); /* default to rewrite */
96 /* substitute special codes in file name */
97 xlatlognam(currlogfilename,cfg.logfilename,cfg.host, &tm);
99 lgfp = fopen(currlogfilename, "r"); /* file already present? */
103 i = askappend(currlogfilename);
105 writemod[0] = 'a'; /* set append mode */
106 else if (i == 0) { /* cancelled */
108 cfg.logtype = 0; /* disable logging */
113 lgfp = fopen(currlogfilename, writemod);
114 if (lgfp) { /* enter into event log */
115 /* --- write header line into log file */
116 fputs("=~=~=~=~=~=~=~=~=~=~=~= PuTTY log ", lgfp);
117 strftime(buf, 24, "%Y.%m.%d %H:%M:%S", &tm);
119 fputs(" =~=~=~=~=~=~=~=~=~=~=~=\r\n", lgfp);
121 sprintf(buf, "%s session log (%s mode) to file: ",
122 (writemod[0] == 'a') ? "Appending" : "Writing new",
123 (cfg.logtype == LGTYP_ASCII ? "ASCII" :
124 cfg.logtype == LGTYP_DEBUG ? "raw" :
125 cfg.logtype == LGTYP_PACKETS ? "SSH packets" : "<ukwn>"));
126 /* Make sure we do not exceed the output buffer size */
127 strncat(buf, currlogfilename, 128);
128 buf[strlen(buf)] = '\0';
142 * translate format codes into time/date strings
143 * and insert them into log file name
145 * "&Y":YYYY "&m":MM "&d":DD "&T":hhmm "&h":<hostname> "&&":&
147 static void xlatlognam(char *d, char *s, char *hostname, struct tm *tm) {
150 int len = FILENAME_MAX-1;
153 /* Let (bufp, len) be the string to append. */
154 bufp = buf; /* don't usually override this */
159 if (*s) switch (c = *s++, tolower(c)) {
161 size = strftime(buf, sizeof(buf), "%Y", tm);
164 size = strftime(buf, sizeof(buf), "%m", tm);
167 size = strftime(buf, sizeof(buf), "%d", tm);
170 size = strftime(buf, sizeof(buf), "%H%M%S", tm);
188 memcpy(d, bufp, size);