comparison src/log.c @ 13406:3033597a4059

[gaim-migrate @ 15781] Index the .log files from the old logger. This works properly (rebuilds the index) if the .log files are modified. I'll be submitting a patch to the Guifications Plugin Pack to have the oldlogger plugin update the index as it goes. committer: Tailor Script <tailor@pidgin.im>
author Richard Laager <rlaager@wiktel.com>
date Mon, 06 Mar 2006 21:08:16 +0000
parents 0a8b72b12cef
children 0d223f4ee868
comparison
equal deleted inserted replaced
13405:851dd370e0fe 13406:3033597a4059
1296 int length; 1296 int length;
1297 }; 1297 };
1298 1298
1299 static GList *old_logger_list(GaimLogType type, const char *sn, GaimAccount *account) 1299 static GList *old_logger_list(GaimLogType type, const char *sn, GaimAccount *account)
1300 { 1300 {
1301 char *logfile = g_strdup_printf("%s.log", gaim_normalize(account, sn));
1302 char *pathstr = g_build_filename(gaim_user_dir(), "logs", logfile, NULL);
1303 GaimStringref *pathref = gaim_stringref_new(pathstr);
1304 struct stat st;
1305 time_t log_last_modified;
1306 FILE *index;
1301 FILE *file; 1307 FILE *file;
1308 GError *error;
1309 int index_fd;
1310 char *index_tmp;
1302 char buf[BUF_LONG]; 1311 char buf[BUF_LONG];
1303 struct tm tm; 1312 struct tm tm;
1304 char month[4]; 1313 char month[4];
1305 struct old_logger_data *data = NULL; 1314 struct old_logger_data *data = NULL;
1306 char *logfile = g_strdup_printf("%s.log", gaim_normalize(account, sn));
1307 char *pathstr = g_build_filename(gaim_user_dir(), "logs", logfile, NULL);
1308 GaimStringref *pathref = gaim_stringref_new(pathstr);
1309 char *newlog; 1315 char *newlog;
1310 int logfound = 0; 1316 int logfound = 0;
1311 int lastoff = 0; 1317 int lastoff = 0;
1312 int newlen; 1318 int newlen;
1313 time_t lasttime = 0; 1319 time_t lasttime = 0;
1314 1320
1315 GaimLog *log = NULL; 1321 GaimLog *log = NULL;
1316 GList *list = NULL; 1322 GList *list = NULL;
1317 1323
1318 g_free(logfile); 1324 g_free(logfile);
1319 g_free(pathstr); 1325
1326 if (g_stat(gaim_stringref_value(pathref), &st))
1327 {
1328 gaim_stringref_unref(pathref);
1329 g_free(pathstr);
1330 return NULL;
1331 }
1332 else
1333 log_last_modified = st.st_mtime;
1334
1335 /* Change the .log extension to .idx */
1336 strcpy(pathstr + strlen(pathstr) - 3, "idx");
1337
1338 if (g_stat(pathstr, &st) == 0)
1339 {
1340 if (st.st_mtime < log_last_modified)
1341 {
1342 gaim_debug_warning("log", "Index \"%s\" exists, but is older than the log.\n", pathstr);
1343 }
1344 else
1345 {
1346 /* The index file exists and is at least as new as the log, so open it. */
1347 if (!(index = g_fopen(pathstr, "rb")))
1348 {
1349 gaim_debug_error("log", "Failed to open index file \"%s\" for reading: %s\n",
1350 pathstr, strerror(errno));
1351
1352 /* Fall through so that we'll parse the log file. */
1353 }
1354 else
1355 {
1356 gaim_debug_info("log", "Using index: %s\n", pathstr);
1357 g_free(pathstr);
1358 while (fgets(buf, BUF_LONG, index))
1359 {
1360 unsigned long idx_time;
1361 if (sscanf(buf, "%d\t%d\t%lu", &lastoff, &newlen, &idx_time) == 3)
1362 {
1363 log = gaim_log_new(GAIM_LOG_IM, sn, account, NULL, -1, NULL);
1364 log->logger = old_logger;
1365 log->time = (time_t)idx_time;
1366 data = g_new0(struct old_logger_data, 1);
1367 data->offset = lastoff;
1368 data->length = newlen;
1369 data->pathref = gaim_stringref_ref(pathref);
1370 log->logger_data = data;
1371 list = g_list_prepend(list, log);
1372 }
1373 }
1374
1375 return list;
1376 }
1377 }
1378 }
1320 1379
1321 if (!(file = g_fopen(gaim_stringref_value(pathref), "rb"))) { 1380 if (!(file = g_fopen(gaim_stringref_value(pathref), "rb"))) {
1381 gaim_debug_error("log", "Failed to open log file \"%s\" for reading: %s\n",
1382 gaim_stringref_value(pathref), strerror(errno));
1322 gaim_stringref_unref(pathref); 1383 gaim_stringref_unref(pathref);
1384 g_free(pathstr);
1323 return NULL; 1385 return NULL;
1386 }
1387
1388 if ((index_fd = g_file_open_tmp(NULL, &index_tmp, &error)) == -1) {
1389 gaim_debug_error("log", "Failed to open index temp file: %s\n",
1390 error->message);
1391 g_error_free(error);
1392 g_free(pathstr);
1393 index = NULL;
1394 } else {
1395 if ((index = fdopen(index_fd, "wb")) == NULL)
1396 {
1397 gaim_debug_error("log", "Failed to fdopen() index temp file: %s\n",
1398 strerror(errno));
1399 close(index_fd);
1400 if (index_tmp != NULL)
1401 {
1402 g_unlink(index_tmp);
1403 g_free(index_tmp);
1404 }
1405 }
1324 } 1406 }
1325 1407
1326 while (fgets(buf, BUF_LONG, file)) { 1408 while (fgets(buf, BUF_LONG, file)) {
1327 if ((newlog = strstr(buf, "---- New C"))) { 1409 if ((newlog = strstr(buf, "---- New C"))) {
1328 int length; 1410 int length;
1361 data->offset = lastoff; 1443 data->offset = lastoff;
1362 data->length = newlen; 1444 data->length = newlen;
1363 data->pathref = gaim_stringref_ref(pathref); 1445 data->pathref = gaim_stringref_ref(pathref);
1364 log->logger_data = data; 1446 log->logger_data = data;
1365 list = g_list_prepend(list, log); 1447 list = g_list_prepend(list, log);
1448
1449 /* XXX: There is apparently Is there a proper way to print a time_t? */
1450 if (index != NULL)
1451 fprintf(index, "%d\t%d\t%lu\n", data->offset, data->length, (unsigned long)log->time);
1366 } 1452 }
1367 } 1453 }
1368 1454
1369 logfound = 1; 1455 logfound = 1;
1370 lastoff = offset; 1456 lastoff = offset;
1412 data->offset = lastoff; 1498 data->offset = lastoff;
1413 data->length = newlen; 1499 data->length = newlen;
1414 data->pathref = gaim_stringref_ref(pathref); 1500 data->pathref = gaim_stringref_ref(pathref);
1415 log->logger_data = data; 1501 log->logger_data = data;
1416 list = g_list_prepend(list, log); 1502 list = g_list_prepend(list, log);
1503
1504 /* XXX: Is there a proper way to print a time_t? */
1505 if (index != NULL)
1506 fprintf(index, "%d\t%d\t%d\n", data->offset, data->length, (int)log->time);
1417 } 1507 }
1418 } 1508 }
1419 1509
1420 gaim_stringref_unref(pathref); 1510 gaim_stringref_unref(pathref);
1421 fclose(file); 1511 fclose(file);
1512 if (index != NULL)
1513 {
1514 fclose(index);
1515
1516 if (index_tmp == NULL)
1517 {
1518 g_free(pathstr);
1519 g_return_val_if_reached(list);
1520 }
1521
1522 if (g_rename(index_tmp, pathstr))
1523 {
1524 gaim_debug_warning("log", "Failed to rename index temp file \"%s\" to \"%s\": %s\n",
1525 index_tmp, pathstr, strerror(errno));
1526 g_unlink(index_tmp);
1527 g_free(index_tmp);
1528 }
1529 else
1530 gaim_debug_info("log", "Built index: %s\n", pathstr);
1531
1532 g_free(pathstr);
1533 }
1422 return list; 1534 return list;
1423 } 1535 }
1424 1536
1425 static int old_logger_total_size(GaimLogType type, const char *name, GaimAccount *account) 1537 static int old_logger_total_size(GaimLogType type, const char *name, GaimAccount *account)
1426 { 1538 {