Mercurial > pidgin
comparison src/util.c @ 4184:af2eeb7f7cf8
[gaim-migrate @ 4415]
Moves most of the logging functions into their own file, log.c.
committer: Tailor Script <tailor@pidgin.im>
| author | Christian Hammond <chipx86@chipx86.com> |
|---|---|
| date | Fri, 03 Jan 2003 07:53:15 +0000 |
| parents | 858822a27529 |
| children | aa20cc16dc08 |
comparison
equal
deleted
inserted
replaced
| 4183:e6810f691393 | 4184:af2eeb7f7cf8 |
|---|---|
| 339 text[cnt] = 0; | 339 text[cnt] = 0; |
| 340 g_free(cpy); | 340 g_free(cpy); |
| 341 return cnt; | 341 return cnt; |
| 342 } | 342 } |
| 343 | 343 |
| 344 | |
| 345 FILE *open_gaim_log_file(const char *name, int *flag) | |
| 346 { | |
| 347 char *buf; | |
| 348 char *buf2; | |
| 349 char log_all_file[256]; | |
| 350 struct stat st; | |
| 351 FILE *fd; | |
| 352 #ifndef _WIN32 | |
| 353 int res; | |
| 354 #endif | |
| 355 gchar *gaim_dir; | |
| 356 | |
| 357 buf = g_malloc(BUF_LONG); | |
| 358 buf2 = g_malloc(BUF_LONG); | |
| 359 gaim_dir = gaim_user_dir(); | |
| 360 | |
| 361 /* Dont log yourself */ | |
| 362 strncpy(log_all_file, gaim_dir, 256); | |
| 363 | |
| 364 #ifndef _WIN32 | |
| 365 stat(log_all_file, &st); | |
| 366 if (!S_ISDIR(st.st_mode)) | |
| 367 unlink(log_all_file); | |
| 368 | |
| 369 fd = fopen(log_all_file, "r"); | |
| 370 | |
| 371 if (!fd) { | |
| 372 res = mkdir(log_all_file, S_IRUSR | S_IWUSR | S_IXUSR); | |
| 373 if (res < 0) { | |
| 374 g_snprintf(buf, BUF_LONG, _("Unable to make directory %s for logging"), | |
| 375 log_all_file); | |
| 376 do_error_dialog(buf, NULL, GAIM_ERROR); | |
| 377 g_free(buf); | |
| 378 g_free(buf2); | |
| 379 return NULL; | |
| 380 } | |
| 381 } else | |
| 382 fclose(fd); | |
| 383 | |
| 384 g_snprintf(log_all_file, 256, "%s" G_DIR_SEPARATOR_S "logs", gaim_dir); | |
| 385 | |
| 386 if (stat(log_all_file, &st) < 0) | |
| 387 *flag = 1; | |
| 388 if (!S_ISDIR(st.st_mode)) | |
| 389 unlink(log_all_file); | |
| 390 | |
| 391 fd = fopen(log_all_file, "r"); | |
| 392 if (!fd) { | |
| 393 res = mkdir(log_all_file, S_IRUSR | S_IWUSR | S_IXUSR); | |
| 394 if (res < 0) { | |
| 395 g_snprintf(buf, BUF_LONG, _("Unable to make directory %s for logging"), | |
| 396 log_all_file); | |
| 397 do_error_dialog(buf, NULL, GAIM_ERROR); | |
| 398 g_free(buf); | |
| 399 g_free(buf2); | |
| 400 return NULL; | |
| 401 } | |
| 402 } else | |
| 403 fclose(fd); | |
| 404 #else /* _WIN32 */ | |
| 405 g_snprintf(log_all_file, 256, "%s" G_DIR_SEPARATOR_S "logs", gaim_dir); | |
| 406 | |
| 407 if( _mkdir(log_all_file) < 0 && errno != EEXIST ) { | |
| 408 g_snprintf(buf, BUF_LONG, _("Unable to make directory %s for logging"), log_all_file); | |
| 409 do_error_dialog(buf, NULL, GAIM_ERROR); | |
| 410 g_free(buf); | |
| 411 g_free(buf2); | |
| 412 return NULL; | |
| 413 } | |
| 414 #endif | |
| 415 | |
| 416 g_snprintf(log_all_file, 256, "%s" G_DIR_SEPARATOR_S "logs" G_DIR_SEPARATOR_S "%s", gaim_dir, name); | |
| 417 if (stat(log_all_file, &st) < 0) | |
| 418 *flag = 1; | |
| 419 | |
| 420 debug_printf("Logging to: \"%s\"\n", log_all_file); | |
| 421 | |
| 422 fd = fopen(log_all_file, "a"); | |
| 423 | |
| 424 g_free(buf); | |
| 425 g_free(buf2); | |
| 426 return fd; | |
| 427 } | |
| 428 | |
| 429 FILE *open_log_file(const char *name, int is_chat) | |
| 430 { | |
| 431 struct stat st; | |
| 432 char realname[256]; | |
| 433 struct log_conversation *l; | |
| 434 FILE *fd; | |
| 435 int flag = 0; | |
| 436 | |
| 437 if (((is_chat == 2) && !(logging_options & OPT_LOG_INDIVIDUAL)) | |
| 438 || ((is_chat == 1) && !(logging_options & OPT_LOG_CHATS)) | |
| 439 || ((is_chat == 0) && !(logging_options & OPT_LOG_CONVOS))) { | |
| 440 | |
| 441 l = find_log_info(name); | |
| 442 if (!l) | |
| 443 return NULL; | |
| 444 | |
| 445 if (stat(l->filename, &st) < 0) | |
| 446 flag = 1; | |
| 447 | |
| 448 fd = fopen(l->filename, "a"); | |
| 449 | |
| 450 if (flag) { /* is a new file */ | |
| 451 if (logging_options & OPT_LOG_STRIP_HTML) { | |
| 452 fprintf(fd, _("IM Sessions with %s\n"), name); | |
| 453 } else { | |
| 454 fprintf(fd, "<HTML><HEAD><TITLE>"); | |
| 455 fprintf(fd, _("IM Sessions with %s"), name); | |
| 456 fprintf(fd, "</TITLE></HEAD><BODY BGCOLOR=\"ffffff\">\n"); | |
| 457 } | |
| 458 } | |
| 459 | |
| 460 return fd; | |
| 461 } | |
| 462 | |
| 463 g_snprintf(realname, sizeof(realname), "%s.log", normalize(name)); | |
| 464 fd = open_gaim_log_file(realname, &flag); | |
| 465 | |
| 466 if (fd && flag) { /* is a new file */ | |
| 467 if (logging_options & OPT_LOG_STRIP_HTML) { | |
| 468 fprintf(fd, _("IM Sessions with %s\n"), name); | |
| 469 } else { | |
| 470 fprintf(fd, "<HTML><HEAD><TITLE>"); | |
| 471 fprintf(fd, _("IM Sessions with %s"), name); | |
| 472 fprintf(fd, "</TITLE></HEAD><BODY BGCOLOR=\"ffffff\">\n"); | |
| 473 } | |
| 474 } | |
| 475 | |
| 476 return fd; | |
| 477 } | |
| 478 | |
| 479 FILE *open_system_log_file(char *name) | |
| 480 { | |
| 481 int x; | |
| 482 | |
| 483 if (name) | |
| 484 return open_log_file(name, 2); | |
| 485 else | |
| 486 return open_gaim_log_file("system", &x); | |
| 487 } | |
| 488 | 344 |
| 489 const char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" "0123456789+/"; | 345 const char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" "0123456789+/"; |
| 490 | 346 |
| 491 /* XXX Find bug */ | 347 /* XXX Find bug */ |
| 492 char *tobase64(const char *text) | 348 char *tobase64(const char *text) |
| 984 do_away_message(NULL, message); | 840 do_away_message(NULL, message); |
| 985 } | 841 } |
| 986 return; | 842 return; |
| 987 } | 843 } |
| 988 | 844 |
| 989 void system_log(enum log_event what, struct gaim_connection *gc, struct buddy *who, int why) | |
| 990 { | |
| 991 FILE *fd; | |
| 992 char text[256], html[256]; | |
| 993 | |
| 994 if ((logging_options & why) != why) | |
| 995 return; | |
| 996 | |
| 997 if (logging_options & OPT_LOG_INDIVIDUAL) { | |
| 998 if (why & OPT_LOG_MY_SIGNON) | |
| 999 fd = open_system_log_file(gc ? gc->username : NULL); | |
| 1000 else | |
| 1001 fd = open_system_log_file(who->name); | |
| 1002 } else | |
| 1003 fd = open_system_log_file(NULL); | |
| 1004 | |
| 1005 if (!fd) | |
| 1006 return; | |
| 1007 | |
| 1008 if (why & OPT_LOG_MY_SIGNON) { | |
| 1009 switch (what) { | |
| 1010 case log_signon: | |
| 1011 g_snprintf(text, sizeof(text), "+++ %s (%s) signed on @ %s", | |
| 1012 gc->username, gc->prpl->name, full_date()); | |
| 1013 g_snprintf(html, sizeof(html), "<B>%s</B>", text); | |
| 1014 break; | |
| 1015 case log_signoff: | |
| 1016 g_snprintf(text, sizeof(text), "+++ %s (%s) signed off @ %s", | |
| 1017 gc->username, gc->prpl->name, full_date()); | |
| 1018 g_snprintf(html, sizeof(html), "<I><FONT COLOR=GRAY>%s</FONT></I>", text); | |
| 1019 break; | |
| 1020 case log_away: | |
| 1021 g_snprintf(text, sizeof(text), "+++ %s (%s) changed away state @ %s", | |
| 1022 gc->username, gc->prpl->name, full_date()); | |
| 1023 g_snprintf(html, sizeof(html), "<FONT COLOR=OLIVE>%s</FONT>", text); | |
| 1024 break; | |
| 1025 case log_back: | |
| 1026 g_snprintf(text, sizeof(text), "+++ %s (%s) came back @ %s", | |
| 1027 gc->username, gc->prpl->name, full_date()); | |
| 1028 g_snprintf(html, sizeof(html), "%s", text); | |
| 1029 break; | |
| 1030 case log_idle: | |
| 1031 g_snprintf(text, sizeof(text), "+++ %s (%s) became idle @ %s", | |
| 1032 gc->username, gc->prpl->name, full_date()); | |
| 1033 g_snprintf(html, sizeof(html), "<FONT COLOR=GRAY>%s</FONT>", text); | |
| 1034 break; | |
| 1035 case log_unidle: | |
| 1036 g_snprintf(text, sizeof(text), "+++ %s (%s) returned from idle @ %s", | |
| 1037 gc->username, gc->prpl->name, full_date()); | |
| 1038 g_snprintf(html, sizeof(html), "%s", text); | |
| 1039 break; | |
| 1040 case log_quit: | |
| 1041 g_snprintf(text, sizeof(text), "+++ Program exit @ %s", full_date()); | |
| 1042 g_snprintf(html, sizeof(html), "<I><FONT COLOR=GRAY>%s</FONT></I>", text); | |
| 1043 break; | |
| 1044 } | |
| 1045 } else if (strcmp(who->name, who->show)) { | |
| 1046 switch (what) { | |
| 1047 case log_signon: | |
| 1048 g_snprintf(text, sizeof(text), "%s (%s) reported that %s (%s) signed on @ %s", | |
| 1049 gc->username, gc->prpl->name, who->show, who->name, full_date()); | |
| 1050 g_snprintf(html, sizeof(html), "<B>%s</B>", text); | |
| 1051 break; | |
| 1052 case log_signoff: | |
| 1053 g_snprintf(text, sizeof(text), "%s (%s) reported that %s (%s) signed off @ %s", | |
| 1054 gc->username, gc->prpl->name, who->show, who->name, full_date()); | |
| 1055 g_snprintf(html, sizeof(html), "<I><FONT COLOR=GRAY>%s</FONT></I>", text); | |
| 1056 break; | |
| 1057 case log_away: | |
| 1058 g_snprintf(text, sizeof(text), "%s (%s) reported that %s (%s) went away @ %s", | |
| 1059 gc->username, gc->prpl->name, who->show, who->name, full_date()); | |
| 1060 g_snprintf(html, sizeof(html), "<FONT COLOR=OLIVE>%s</FONT>", text); | |
| 1061 break; | |
| 1062 case log_back: | |
| 1063 g_snprintf(text, sizeof(text), "%s (%s) reported that %s (%s) came back @ %s", | |
| 1064 gc->username, gc->prpl->name, who->show, who->name, full_date()); | |
| 1065 g_snprintf(html, sizeof(html), "%s", text); | |
| 1066 break; | |
| 1067 case log_idle: | |
| 1068 g_snprintf(text, sizeof(text), "%s (%s) reported that %s (%s) became idle @ %s", | |
| 1069 gc->username, gc->prpl->name, who->show, who->name, full_date()); | |
| 1070 g_snprintf(html, sizeof(html), "<FONT COLOR=GRAY>%s</FONT>", text); | |
| 1071 break; | |
| 1072 case log_unidle: | |
| 1073 g_snprintf(text, sizeof(text), | |
| 1074 "%s (%s) reported that %s (%s) returned from idle @ %s", gc->username, | |
| 1075 gc->prpl->name, who->show, who->name, full_date()); | |
| 1076 g_snprintf(html, sizeof(html), "%s", text); | |
| 1077 break; | |
| 1078 default: | |
| 1079 fclose(fd); | |
| 1080 return; | |
| 1081 break; | |
| 1082 } | |
| 1083 } else { | |
| 1084 switch (what) { | |
| 1085 case log_signon: | |
| 1086 g_snprintf(text, sizeof(text), "%s (%s) reported that %s signed on @ %s", | |
| 1087 gc->username, gc->prpl->name, who->name, full_date()); | |
| 1088 g_snprintf(html, sizeof(html), "<B>%s</B>", text); | |
| 1089 break; | |
| 1090 case log_signoff: | |
| 1091 g_snprintf(text, sizeof(text), "%s (%s) reported that %s signed off @ %s", | |
| 1092 gc->username, gc->prpl->name, who->name, full_date()); | |
| 1093 g_snprintf(html, sizeof(html), "<I><FONT COLOR=GRAY>%s</FONT></I>", text); | |
| 1094 break; | |
| 1095 case log_away: | |
| 1096 g_snprintf(text, sizeof(text), "%s (%s) reported that %s went away @ %s", | |
| 1097 gc->username, gc->prpl->name, who->name, full_date()); | |
| 1098 g_snprintf(html, sizeof(html), "<FONT COLOR=OLIVE>%s</FONT>", text); | |
| 1099 break; | |
| 1100 case log_back: | |
| 1101 g_snprintf(text, sizeof(text), "%s (%s) reported that %s came back @ %s", | |
| 1102 gc->username, gc->prpl->name, who->name, full_date()); | |
| 1103 g_snprintf(html, sizeof(html), "%s", text); | |
| 1104 break; | |
| 1105 case log_idle: | |
| 1106 g_snprintf(text, sizeof(text), "%s (%s) reported that %s became idle @ %s", | |
| 1107 gc->username, gc->prpl->name, who->name, full_date()); | |
| 1108 g_snprintf(html, sizeof(html), "<FONT COLOR=GRAY>%s</FONT>", text); | |
| 1109 break; | |
| 1110 case log_unidle: | |
| 1111 g_snprintf(text, sizeof(text), | |
| 1112 "%s (%s) reported that %s returned from idle @ %s", gc->username, | |
| 1113 gc->prpl->name, who->name, full_date()); | |
| 1114 g_snprintf(html, sizeof(html), "%s", text); | |
| 1115 break; | |
| 1116 default: | |
| 1117 fclose(fd); | |
| 1118 return; | |
| 1119 break; | |
| 1120 } | |
| 1121 } | |
| 1122 | |
| 1123 if (logging_options & OPT_LOG_STRIP_HTML) { | |
| 1124 fprintf(fd, "---- %s ----\n", text); | |
| 1125 } else { | |
| 1126 if (logging_options & OPT_LOG_INDIVIDUAL) | |
| 1127 fprintf(fd, "<HR>%s<BR><HR><BR>\n", html); | |
| 1128 else | |
| 1129 fprintf(fd, "%s<BR>\n", html); | |
| 1130 } | |
| 1131 | |
| 1132 fclose(fd); | |
| 1133 } | |
| 1134 | |
| 1135 void strip_linefeed(gchar *text) | 845 void strip_linefeed(gchar *text) |
| 1136 { | 846 { |
| 1137 int i, j; | 847 int i, j; |
| 1138 gchar *text2 = g_malloc(strlen(text) + 1); | 848 gchar *text2 = g_malloc(strlen(text) + 1); |
| 1139 | 849 |
