comparison mcabber/src/histolog.c @ 241:8584f919d9b9

[/trunk] Changeset 254 by mikael * Use ISO8601 for time format in log file * Update TODO
author mikael
date Mon, 13 Jun 2005 19:29:09 +0000
parents 73f6ce668ba8
children 701651393076
comparison
equal deleted inserted replaced
240:723433a677f0 241:8584f919d9b9
29 29
30 #include "histolog.h" 30 #include "histolog.h"
31 #include "hbuf.h" 31 #include "hbuf.h"
32 #include "jabglue.h" 32 #include "jabglue.h"
33 #include "screen.h" 33 #include "screen.h"
34 #include "utils.h"
34 35
35 static guint UseFileLogging; 36 static guint UseFileLogging;
36 static guint FileLoadLogs; 37 static guint FileLoadLogs;
37 static char *RootDir; 38 static char *RootDir;
38 39
65 guint len = 0; 66 guint len = 0;
66 FILE *fp; 67 FILE *fp;
67 time_t ts; 68 time_t ts;
68 const char *p; 69 const char *p;
69 char *filename; 70 char *filename;
71 char str_ts[20];
70 72
71 if (!UseFileLogging) return; 73 if (!UseFileLogging) return;
72 74
73 filename = user_histo_file(jid); 75 filename = user_histo_file(jid);
74 76
83 85
84 // Count number of extra lines 86 // Count number of extra lines
85 for (p=data ; *p ; p++) 87 for (p=data ; *p ; p++)
86 if (*p == '\n') len++; 88 if (*p == '\n') len++;
87 89
88 /* Line format: "TI DDDDDDDDDD LLL [data]" 90 /* Line format: "TI yyyymmddThh:mm:ssZ [data]"
89 * T=Type, I=Info, DDDDDDDDDD=date, LLL=0-padded-len 91 * (Old format: "TI DDDDDDDDDD LLL [data])"
92 * T=Type, I=Info, yyyymmddThh:mm:ssZ=date, LLL=0-padded-len
90 * 93 *
91 * Types: 94 * Types:
92 * - M message Info: S (send) R (receive) 95 * - M message Info: S (send) R (receive)
93 * - S status Info: [oaifdcn] 96 * - S status Info: [oaifdcn]
94 * We don't check them, we'll trust the caller. 97 * We don't check them, we'll trust the caller.
96 99
97 fp = fopen(filename, "a"); 100 fp = fopen(filename, "a");
98 g_free(filename); 101 g_free(filename);
99 if (!fp) return; 102 if (!fp) return;
100 103
101 fprintf(fp, "%c%c %10u %03d %s\n", type, info, (unsigned int)ts, len, data); 104 to_iso8601(str_ts, ts);
105 fprintf(fp, "%c%c %-18.18s %03d %s\n", type, info, str_ts, len, data);
102 fclose(fp); 106 fclose(fp);
103 } 107 }
104 108
105 // hlog_read_history() 109 // hlog_read_history()
106 // Reads the jid's history logfile 110 // Reads the jid's history logfile
113 guint prefix_flags; 117 guint prefix_flags;
114 guint len; 118 guint len;
115 FILE *fp; 119 FILE *fp;
116 struct stat bufstat; 120 struct stat bufstat;
117 guint err = 0; 121 guint err = 0;
122 guint oldformat;
118 123
119 if (!FileLoadLogs) return; 124 if (!FileLoadLogs) return;
120 125
121 data = g_new(char, HBB_BLOCKSIZE+32); 126 data = g_new(char, HBB_BLOCKSIZE+32);
122 if (!data) { 127 if (!data) {
137 scr_LogPrint("Reading <%s> history file...", jid); 142 scr_LogPrint("Reading <%s> history file...", jid);
138 } 143 }
139 144
140 /* See write_histo_line() for line format... */ 145 /* See write_histo_line() for line format... */
141 while (!feof(fp)) { 146 while (!feof(fp)) {
147 int format_off =0;
142 if (fgets(data, HBB_BLOCKSIZE+24, fp) == NULL) break; 148 if (fgets(data, HBB_BLOCKSIZE+24, fp) == NULL) break;
143 149
144 for (tail = data; *tail; tail++) ; 150 for (tail = data; *tail; tail++) ;
145 151
146 type = data[0]; 152 type = data[0];
147 info = data[1]; 153 info = data[1];
154
155 // XXX Log format detection. We can read both old and new log formats
156 // To be removed in a future version
157 oldformat = (data[11] != 'T' || data[20] != 'Z');
158 if (!oldformat)
159 format_off = 8;
160
148 if ((type != 'M' && type != 'S') || 161 if ((type != 'M' && type != 'S') ||
149 (data[13] != ' ') || (data[17] != ' ')) { 162 (oldformat && ((data[13] != ' ') || (data[17] != ' '))) ||
163 ((!oldformat) && ((data[21] != ' ') || (data[25] != ' ')))) {
150 if (!err) { 164 if (!err) {
151 scr_LogPrint("Error in history file format (%s)", jid); 165 scr_LogPrint("Error in history file format (%s)", jid);
152 err = 1; 166 err = 1;
153 } 167 }
154 //break; 168 //break;
155 continue; 169 continue;
156 } 170 }
157 data[13] = data[17] = 0; 171 data[13+format_off] = data[17+format_off] = 0;
158 timestamp = (unsigned long) atol(&data[3]); 172 if (oldformat)
159 len = (unsigned long) atol(&data[14]); 173 timestamp = (unsigned long) atol(&data[3]);
174 else
175 timestamp = from_iso8601(&data[3], 1);
176 len = (unsigned long) atol(&data[14+format_off]);
160 177
161 // Some checks 178 // Some checks
162 if (((type == 'M') && (info != 'S' && info != 'R')) || 179 if (((type == 'M') && (info != 'S' && info != 'R')) ||
163 ((type == 'I') && (!strchr("OAIFDCN", info)))) { 180 ((type == 'I') && (!strchr("OAIFDCN", info)))) {
164 if (!err) { 181 if (!err) {
174 if (fgets(tail, HBB_BLOCKSIZE+24 - (tail-data), fp) == NULL) break; 191 if (fgets(tail, HBB_BLOCKSIZE+24 - (tail-data), fp) == NULL) break;
175 192
176 while (*tail) tail++; 193 while (*tail) tail++;
177 } 194 }
178 // Remove last CR 195 // Remove last CR
179 if ((tail > data+18) && (*(tail-1) == '\n')) 196 if ((tail > data+18+format_off) && (*(tail-1) == '\n'))
180 *(tail-1) = 0; 197 *(tail-1) = 0;
181 198
182 if (type == 'M') { 199 if (type == 'M') {
183 if (info == 'S') 200 if (info == 'S')
184 prefix_flags = HBB_PREFIX_OUT; 201 prefix_flags = HBB_PREFIX_OUT;
185 else 202 else
186 prefix_flags = HBB_PREFIX_IN; 203 prefix_flags = HBB_PREFIX_IN;
187 hbuf_add_line(p_buddyhbuf, &data[18], timestamp, prefix_flags, width); 204 hbuf_add_line(p_buddyhbuf, &data[18+format_off], timestamp, prefix_flags, width);
188 err = 0; 205 err = 0;
189 } 206 }
190 } 207 }
191 fclose(fp); 208 fclose(fp);
192 g_free(data); 209 g_free(data);