Changeset 42947f1 for logging.c


Ignore:
Timestamp:
Jun 1, 2007, 4:01:55 AM (13 years ago)
Author:
Alex Vandiver <alexmv@mit.edu>
Branches:
master, barnowl_perlaim, debian, release-1.4, release-1.5, release-1.6, release-1.7, release-1.8, release-1.9
Children:
f1fc47f
Parents:
8757122
git-author:
Alex Vandiver <alexmv@mit.edu> (06/01/07 04:01:29)
git-committer:
Alex Vandiver <alexmv@mit.edu> (06/01/07 04:01:55)
Message:
 * Outgoing messages now log as much information as incomign messages
 * Code reuse is a Good Thing
File:
1 edited

Legend:

Unmodified
Added
Removed
  • logging.c

    r3066d23 r42947f1  
    3333
    3434  /* handle outgoing messages */
    35   if (owl_message_is_type_aim(m)) {
    36     owl_log_outgoing_aim(m);
    37   } else if (owl_message_is_type_zephyr(m)) {
    38     owl_log_outgoing_zephyr(m);
    39   } else if (owl_message_is_type_loopback(m)) {
    40     owl_log_outgoing_loopback(m);
    41   } else if (owl_message_is_type_jabber(m)) {
    42    owl_log_outgoing_jabber(m);   
    43   }else {
    44     owl_function_error("Unknown message type for logging");
    45   }
     35  owl_log_outgoing(m);
     36
    4637  owl_function_debugmsg("owl_log_message: leaving");
    4738}
     
    8475}
    8576
    86 void owl_log_outgoing_zephyr(owl_message *m)
     77void owl_log_zephyr(owl_message *m, FILE *file) {
     78    char *tmp;
     79    tmp=short_zuser(owl_message_get_sender(m));
     80    fprintf(file, "Class: %s Instance: %s", owl_message_get_class(m), owl_message_get_instance(m));
     81    if (strcmp(owl_message_get_opcode(m), "")) fprintf(file, " Opcode: %s", owl_message_get_opcode(m));
     82    fprintf(file, "\n");
     83    fprintf(file, "Time: %s Host: %s\n", owl_message_get_timestr(m), owl_message_get_hostname(m));
     84    fprintf(file, "From: %s <%s>\n\n", owl_message_get_zsig(m), tmp);
     85    fprintf(file, "%s\n\n", owl_message_get_body(m));
     86    owl_free(tmp);
     87}
     88
     89void owl_log_aim(owl_message *m, FILE *file) {
     90    fprintf(file, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
     91    fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m));
     92    if (owl_message_is_login(m))
     93        fprintf(file, "LOGIN\n\n");
     94    else if (owl_message_is_logout(m))
     95        fprintf(file, "LOGOUT\n\n");
     96    else
     97        fprintf(file, "%s\n\n", owl_message_get_body(m));
     98}
     99
     100void owl_log_jabber(owl_message *m, FILE *file) {
     101    fprintf(file, "From: <%s> To: <%s>\n",owl_message_get_sender(m), owl_message_get_recipient(m));
     102    fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m));
     103    fprintf(file, "%s\n\n",owl_message_get_body(m));
     104}
     105
     106void owl_log_generic(owl_message *m, FILE *file) {
     107    fprintf(file, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
     108    fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m));
     109    fprintf(file, "%s\n\n", owl_message_get_body(m));
     110}
     111
     112void owl_log_append(owl_message *m, char *filename) {
     113    FILE *file;
     114    file=fopen(filename, "a");
     115    if (!file) {
     116        owl_function_error("Unable to open file for logging");
     117        return;
     118    }
     119    if (owl_message_is_type_zephyr(m)) {
     120        owl_log_zephyr(m, file);
     121    } else if (owl_message_is_type_jabber(m)) {
     122        owl_log_jabber(m, file);
     123    } else if (owl_message_is_type_aim(m)) {
     124        owl_log_aim(m, file);
     125    } else {
     126        owl_log_generic(m, file);
     127    }
     128    fclose(file);
     129}
     130
     131void owl_log_outgoing(owl_message *m)
    87132{
    88   FILE *file;
    89133  char filename[MAXPATHLEN], *logpath;
    90   char *to, *text;
    91 
    92   /* strip local realm */
    93   to=short_zuser(owl_message_get_recipient(m));
     134  char *to;
     135
     136  /* Figure out what path to log to */
     137  if (owl_message_is_type_zephyr(m)) {
     138      to = short_zuser(owl_message_get_recipient(m));
     139  } else if (owl_message_is_type_jabber(m)) {
     140      to = owl_sprintf("jabber:%s", owl_message_get_recipient(m));
     141  } else if (owl_message_is_type_aim(m)) {
     142      char *temp;
     143      temp = owl_aim_normalize_screenname(owl_message_get_recipient(m));
     144      downstr(temp);
     145      to = owl_sprintf("aim:%s", temp);
     146      owl_free(temp);
     147  } else {
     148      to = owl_sprintf("loopback");
     149  }
    94150
    95151  /* expand ~ in path names */
    96   logpath=owl_text_substitute(owl_global_get_logpath(&g), "~", owl_global_get_homedir(&g));
    97 
    98   text=owl_message_get_body(m);
    99 
     152  logpath = owl_text_substitute(owl_global_get_logpath(&g), "~", owl_global_get_homedir(&g));
    100153  snprintf(filename, MAXPATHLEN, "%s/%s", logpath, to);
    101   file=fopen(filename, "a");
    102   if (!file) {
    103     owl_function_error("Unable to open file for outgoing logging");
    104     owl_free(logpath);
    105     owl_free(to);
    106     return;
    107   }
    108   fprintf(file, "OUTGOING (owl): %s\n%s\n", to, text);
    109   if (text[strlen(text)-1]!='\n') {
    110     fprintf(file, "\n");
    111   }
    112   fclose(file);
     154  owl_free(to);
     155
     156  owl_log_append(m, filename);
    113157
    114158  snprintf(filename, MAXPATHLEN, "%s/all", logpath);
    115159  owl_free(logpath);
    116   file=fopen(filename, "a");
    117   if (!file) {
    118     owl_function_error("Unable to open file for outgoing logging");
    119     owl_free(to);
    120     return;
    121   }
    122   fprintf(file, "OUTGOING (owl): %s\n%s\n", to, text);
    123   if (text[strlen(text)-1]!='\n') {
    124     fprintf(file, "\n");
    125   }
    126   fclose(file);
    127 
    128   owl_free(to);
    129 }
     160
     161  owl_log_append(m, filename);
     162}
     163
    130164
    131165void owl_log_outgoing_zephyr_error(char *to, char *text)
     
    185219}
    186220
    187 void owl_log_outgoing_jabber(owl_message *m)
    188 {
    189   FILE *file;
    190   char filename[MAXPATHLEN], *logpath;
    191   char *tobuff, *normalto, *text;
    192   owl_function_debugmsg("owl_log_outgoing_jabber: entering");
    193   /* normalize and downcase the screenname */
    194   normalto = owl_message_get_recipient(m);
    195  
    196   /* downstr(normalto); */
    197   tobuff=owl_sprintf("jabber:%s", normalto);
    198   /* owl_free(normalto); */
    199 
    200   /* expand ~ in path names */
    201   logpath = owl_text_substitute(owl_global_get_logpath(&g), "~", owl_global_get_homedir(&g));
    202 
    203   text=owl_message_get_body(m);
    204  
    205   snprintf(filename, MAXPATHLEN, "%s/%s", logpath, tobuff);
    206   file=fopen(filename, "a");
    207   if (!file) {
    208     owl_function_error("Unable to open file for outgoing logging");
    209     owl_free(logpath);
    210     owl_free(tobuff);
    211     return;
    212   }
    213   fprintf(file, "OUTGOING (owl): %s\n%s\n", tobuff, text);
    214   if (text[strlen(text)-1]!='\n') {
    215     fprintf(file, "\n");
    216   }
    217   fclose(file);
    218 
    219   snprintf(filename, MAXPATHLEN, "%s/all", logpath);
    220   owl_free(logpath);
    221   file=fopen(filename, "a");
    222   if (!file) {
    223     owl_function_error("Unable to open file for outgoing logging");
    224     owl_free(tobuff);
    225     return;
    226   }
    227   fprintf(file, "OUTGOING (owl): %s\n%s\n", tobuff, text);
    228   if (text[strlen(text)-1]!='\n') {
    229     fprintf(file, "\n");
    230   }
    231   fclose(file);
    232 
    233   owl_free(tobuff);
    234 }
    235 
    236 
    237 void owl_log_outgoing_aim(owl_message *m)
    238 {
    239   FILE *file;
    240   char filename[MAXPATHLEN], *logpath;
    241   char *tobuff, *normalto, *text;
    242 
    243   owl_function_debugmsg("owl_log_outgoing_aim: entering");
    244 
    245   /* normalize and downcase the screenname */
    246   normalto=owl_aim_normalize_screenname(owl_message_get_recipient(m));
    247   downstr(normalto);
    248   tobuff=owl_sprintf("aim:%s", normalto);
    249   owl_free(normalto);
    250 
    251   /* expand ~ in path names */
    252   logpath = owl_text_substitute(owl_global_get_logpath(&g), "~", owl_global_get_homedir(&g));
    253 
    254   text=owl_message_get_body(m);
    255  
    256   snprintf(filename, MAXPATHLEN, "%s/%s", logpath, tobuff);
    257   file=fopen(filename, "a");
    258   if (!file) {
    259     owl_function_error("Unable to open file for outgoing logging");
    260     owl_free(logpath);
    261     owl_free(tobuff);
    262     return;
    263   }
    264   fprintf(file, "OUTGOING (owl): %s\n%s\n", tobuff, text);
    265   if (text[strlen(text)-1]!='\n') {
    266     fprintf(file, "\n");
    267   }
    268   fclose(file);
    269 
    270   snprintf(filename, MAXPATHLEN, "%s/all", logpath);
    271   owl_free(logpath);
    272   file=fopen(filename, "a");
    273   if (!file) {
    274     owl_function_error("Unable to open file for outgoing logging");
    275     owl_free(tobuff);
    276     return;
    277   }
    278   fprintf(file, "OUTGOING (owl): %s\n%s\n", tobuff, text);
    279   if (text[strlen(text)-1]!='\n') {
    280     fprintf(file, "\n");
    281   }
    282   fclose(file);
    283 
    284   owl_free(tobuff);
    285 }
    286 
    287 void owl_log_outgoing_loopback(owl_message *m)
    288 {
    289   FILE *file;
    290   char filename[MAXPATHLEN], *logpath;
    291   char *tobuff, *text;
    292 
    293   tobuff=owl_sprintf("loopback");
    294 
    295   /* expand ~ in path names */
    296   logpath = owl_text_substitute(owl_global_get_logpath(&g), "~", owl_global_get_homedir(&g));
    297 
    298   text=owl_message_get_body(m);
    299 
    300   snprintf(filename, MAXPATHLEN, "%s/%s", logpath, tobuff);
    301   file=fopen(filename, "a");
    302   if (!file) {
    303     owl_function_error("Unable to open file for outgoing logging");
    304     owl_free(logpath);
    305     owl_free(tobuff);
    306     return;
    307   }
    308   fprintf(file, "OUTGOING (owl): %s\n%s\n", tobuff, text);
    309   if (text[strlen(text)-1]!='\n') {
    310     fprintf(file, "\n");
    311   }
    312   fclose(file);
    313 
    314   snprintf(filename, MAXPATHLEN, "%s/all", logpath);
    315   owl_free(logpath);
    316   file=fopen(filename, "a");
    317   if (!file) {
    318     owl_function_error("Unable to open file for outgoing logging");
    319     owl_free(tobuff);
    320     return;
    321   }
    322   fprintf(file, "OUTGOING (owl): %s\n%s\n", tobuff, text);
    323   if (text[strlen(text)-1]!='\n') {
    324     fprintf(file, "\n");
    325   }
    326   fclose(file);
    327 
    328   owl_free(tobuff);
    329 }
    330 
    331221void owl_log_incoming(owl_message *m)
    332222{
    333   FILE *file, *allfile;
    334223  char filename[MAXPATHLEN], allfilename[MAXPATHLEN], *logpath;
    335   char *frombuff=NULL, *from=NULL, *zsig=NULL;
     224  char *frombuff=NULL, *from=NULL;
    336225  int len, ch, i, personal;
    337226
     
    339228  if (owl_message_is_type_zephyr(m)) {
    340229    if (owl_message_is_personal(m)) {
    341       personal=1;
    342     } else {
    343       personal=0;
     230      personal = 1;
     231    } else {
     232      personal = 0;
    344233    }
    345234  } else if (owl_message_is_type_jabber(m)) {
    346           /* This needs to be fixed to handle groupchat */
    347           char* msgtype = owl_message_get_attribute_value(m,"jtype");
    348           if (msgtype && !strcmp(msgtype,"groupchat")) {
    349                   personal =0;
    350           } else {
    351              personal=1;
    352           }
     235    /* This needs to be fixed to handle groupchat */
     236    char* msgtype = owl_message_get_attribute_value(m,"jtype");
     237    if (msgtype && !strcmp(msgtype,"groupchat")) {
     238      personal = 0;
     239    } else {
     240      personal = 1;
     241    }
    353242  } else {
    354243    if (owl_message_is_private(m) || owl_message_is_loginout(m)) {
    355       personal=1;
    356     } else {
    357       personal=0;
    358     }
    359   }
    360 
    361  
     244      personal = 1;
     245    } else {
     246      personal = 0;
     247    }
     248  }
     249
    362250
    363251  if (owl_message_is_type_zephyr(m)) {
     
    415303  }
    416304  owl_free(logpath);
    417  
    418   file=fopen(filename, "a");
    419   if (!file) {
    420     owl_function_error("Unable to open file for incoming logging");
    421     owl_free(frombuff);
    422     return;
    423   }
    424 
    425   allfile=NULL;
    426   if (personal) {
    427     allfile=fopen(allfilename, "a");
    428     if (!allfile) {
    429       owl_function_error("Unable to open file for incoming logging");
    430       owl_free(frombuff);
    431       fclose(file);
    432       return;
    433     }
    434   }
    435 
    436   /* write to the main file */
    437   if (owl_message_is_type_zephyr(m)) {
    438     char *tmp;
    439    
    440     tmp=short_zuser(owl_message_get_sender(m));
    441     fprintf(file, "Class: %s Instance: %s", owl_message_get_class(m), owl_message_get_instance(m));
    442     if (strcmp(owl_message_get_opcode(m), "")) fprintf(file, " Opcode: %s", owl_message_get_opcode(m));
    443     fprintf(file, "\n");
    444     fprintf(file, "Time: %s Host: %s\n", owl_message_get_timestr(m), owl_message_get_hostname(m));
    445     zsig=owl_message_get_zsig(m);
    446     fprintf(file, "From: %s <%s>\n\n", zsig, tmp);
    447     fprintf(file, "%s\n\n", owl_message_get_body(m));
    448     owl_free(tmp);
    449   } else if (owl_message_is_type_aim(m) && !owl_message_is_loginout(m)) {
    450     fprintf(file, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
    451     fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m));
    452     fprintf(file, "%s\n\n", owl_message_get_body(m));
    453   } else if (owl_message_is_type_aim(m) && owl_message_is_loginout(m)) {
    454     fprintf(file, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
    455     fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m));
    456     if (owl_message_is_login(m)) fprintf(file, "LOGIN\n\n");
    457     if (owl_message_is_logout(m)) fprintf(file, "LOGOUT\n\n");
    458   } else if (owl_message_is_type_jabber(m)) {
    459     fprintf(file, "From: <%s> To: <%s>\n",owl_message_get_sender(m), owl_message_get_recipient(m));
    460     fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m));
    461     fprintf(file, "%s\n\n",owl_message_get_body(m));
    462   }
    463   else {
    464     fprintf(file, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
    465     fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m));
    466     fprintf(file, "%s\n\n", owl_message_get_body(m));
    467   }
    468 
    469   fclose(file);
    470 
    471   /* if it's a personal message, also write to the 'all' file */
    472   if (personal) {
    473     if (owl_message_is_type_zephyr(m)) {
    474       char *tmp;
    475 
    476       tmp=short_zuser(owl_message_get_sender(m));
    477       fprintf(allfile, "Class: %s Instance: %s", owl_message_get_class(m), owl_message_get_instance(m));
    478       if (strcmp(owl_message_get_opcode(m), "")) fprintf(allfile, " Opcode: %s", owl_message_get_opcode(m));
    479       fprintf(allfile, "\n");
    480       fprintf(allfile, "Time: %s Host: %s\n", owl_message_get_timestr(m), owl_message_get_hostname(m));
    481       fprintf(allfile, "From: %s <%s>\n\n", zsig, tmp);
    482       fprintf(allfile, "%s\n\n", owl_message_get_body(m));
    483       owl_free(tmp);
    484     } else if (owl_message_is_type_aim(m) && !owl_message_is_loginout(m)) {
    485       fprintf(allfile, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
    486       fprintf(allfile, "Time: %s\n\n", owl_message_get_timestr(m));
    487       fprintf(allfile, "%s\n\n", owl_message_get_body(m));
    488     } else if (owl_message_is_type_aim(m) && owl_message_is_loginout(m)) {
    489       fprintf(allfile, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
    490       fprintf(allfile, "Time: %s\n\n", owl_message_get_timestr(m));
    491       if (owl_message_is_login(m)) fprintf(allfile, "LOGIN\n\n");
    492       if (owl_message_is_logout(m)) fprintf(allfile, "LOGOUT\n\n");
    493     } else {
    494       fprintf(allfile, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
    495       fprintf(allfile, "Time: %s\n\n", owl_message_get_timestr(m));
    496       fprintf(allfile, "%s\n\n", owl_message_get_body(m));
    497     }
    498     fclose(allfile);
    499   }
    500 
    501305  owl_free(frombuff);
    502 }
     306
     307  owl_log_append(m, filename);
     308
     309  if (personal)
     310      owl_log_append(m, allfilename);
     311
     312}
Note: See TracChangeset for help on using the changeset viewer.