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
);
67 /* open log file append/overwrite mode */
77 sprintf(writemod
, "wb"); /* default to rewrite */
82 /* substitute special codes in file name */
83 xlatlognam(currlogfilename
,cfg
.logfilename
,cfg
.host
, &tm
);
85 lgfp
= fopen(currlogfilename
, "r"); /* file already present? */
89 i
= askappend(currlogfilename
);
91 writemod
[0] = 'a'; /* set append mode */
92 else if (i
== 0) { /* cancelled */
94 cfg
.logtype
= 0; /* disable logging */
99 lgfp
= fopen(currlogfilename
, writemod
);
100 if (lgfp
) { /* enter into event log */
101 sprintf(buf
, "%s session log (%s mode) to file : ",
102 (writemod
[0] == 'a') ?
"Appending" : "Writing new",
103 (cfg
.logtype
== LGTYP_ASCII ?
"ASCII" :
104 cfg
.logtype
== LGTYP_DEBUG ?
"raw" : "<ukwn>"));
105 /* Make sure we do not exceed the output buffer size */
106 strncat(buf
, currlogfilename
, 128);
107 buf
[strlen(buf
)] = '\0';
110 /* --- write header line into log file */
111 fputs("=~=~=~=~=~=~=~=~=~=~=~= PuTTY log ", lgfp
);
112 strftime(buf
, 24, "%Y.%m.%d %H:%M:%S", &tm
);
114 fputs(" =~=~=~=~=~=~=~=~=~=~=~=\r\n", lgfp
);
127 * translate format codes into time/date strings
128 * and insert them into log file name
130 * "&Y":YYYY "&m":MM "&d":DD "&T":hhmm "&h":<hostname> "&&":&
132 static void xlatlognam(char *d
, char *s
, char *hostname
, struct tm
*tm
) {
135 char *ds
= d
; /* save start pos. */
136 int len
= FILENAME_MAX
-1;
139 /* Let (bufp, len) be the string to append. */
140 bufp
= buf
; /* don't usually override this */
144 if (*s
) switch (c
= *s
++, tolower(c
)) {
146 size
= strftime(buf
, sizeof(buf
), "%Y", tm
);
149 size
= strftime(buf
, sizeof(buf
), "%m", tm
);
152 size
= strftime(buf
, sizeof(buf
), "%d", tm
);
155 size
= strftime(buf
, sizeof(buf
), "%H%M%S", tm
);
173 memcpy(d
, bufp
, size
);