12 /* log session to file stuff ... */
13 static FILE *lgfp = NULL;
14 static char timdatbuf[20];
15 static char currlogfilename[FILENAME_MAX];
17 static void xlatlognam(char *d, char *s, char *hostname, struct tm *tm);
20 * Log session traffic.
22 void logtraffic(unsigned char c, int logmode)
24 if (cfg.logtype > 0) {
25 if (cfg.logtype == logmode) {
26 /* deferred open file from pgm start? */
38 void log_packet(int direction, int type, char *texttype, void *data, int len)
41 char dumpdata[80], smalldata[5];
43 if (cfg.logtype != LGTYP_PACKETS)
48 fprintf(lgfp, "%s packet type %d / 0x%02x (%s)\n",
49 direction == PKT_INCOMING ? "Incoming" : "Outgoing",
50 type, type, texttype);
51 for (i = 0; i < len; i += 16) {
52 sprintf(dumpdata, " %08x%*s\n", i, 1+3*16+2+16, "");
53 for (j = 0; j < 16 && i+j < len; j++) {
54 int c = ((unsigned char *)data)[i+j];
55 sprintf(smalldata, "%02x", c);
56 dumpdata[10+2+3*j] = smalldata[0];
57 dumpdata[10+2+3*j+1] = smalldata[1];
58 dumpdata[10+1+3*16+2+j] = (isprint(c) ? c : '.');
60 strcpy(dumpdata + 10+1+3*16+2+j, "\n");
61 fputs(dumpdata, lgfp);
66 /* open log file append/overwrite mode */
76 sprintf(writemod, "wb"); /* default to rewrite */
81 /* substitute special codes in file name */
82 xlatlognam(currlogfilename,cfg.logfilename,cfg.host, &tm);
84 lgfp = fopen(currlogfilename, "r"); /* file already present? */
88 i = askappend(currlogfilename);
90 writemod[0] = 'a'; /* set append mode */
91 else if (i == 0) { /* cancelled */
93 cfg.logtype = 0; /* disable logging */
98 lgfp = fopen(currlogfilename, writemod);
99 if (lgfp) { /* enter into event log */
100 sprintf(buf, "%s session log (%s mode) to file : ",
101 (writemod[0] == 'a') ? "Appending" : "Writing new",
102 (cfg.logtype == LGTYP_ASCII ? "ASCII" :
103 cfg.logtype == LGTYP_DEBUG ? "raw" : "<ukwn>"));
104 /* Make sure we do not exceed the output buffer size */
105 strncat(buf, currlogfilename, 128);
106 buf[strlen(buf)] = '\0';
109 /* --- write header line into log file */
110 fputs("=~=~=~=~=~=~=~=~=~=~=~= PuTTY log ", lgfp);
111 strftime(buf, 24, "%Y.%m.%d %H:%M:%S", &tm);
113 fputs(" =~=~=~=~=~=~=~=~=~=~=~=\r\n", lgfp);
126 * translate format codes into time/date strings
127 * and insert them into log file name
129 * "&Y":YYYY "&m":MM "&d":DD "&T":hhmm "&h":<hostname> "&&":&
131 static void xlatlognam(char *d, char *s, char *hostname, struct tm *tm) {
134 char *ds = d; /* save start pos. */
135 int len = FILENAME_MAX-1;
138 /* Let (bufp, len) be the string to append. */
139 bufp = buf; /* don't usually override this */
143 if (*s) switch (c = *s++, tolower(c)) {
145 size = strftime(buf, sizeof(buf), "%Y", tm);
148 size = strftime(buf, sizeof(buf), "%m", tm);
151 size = strftime(buf, sizeof(buf), "%d", tm);
154 size = strftime(buf, sizeof(buf), "%H%M%S", tm);
172 memcpy(d, bufp, size);