mcabber/src/histolog.c
changeset 113 8ac67e951eab
parent 111 d896962c16fa
child 118 1e8f646e2c5b
equal deleted inserted replaced
112:edb5591e2e64 113:8ac67e951eab
    20  */
    20  */
    21 
    21 
    22 #include <string.h>
    22 #include <string.h>
    23 #include <stdlib.h>
    23 #include <stdlib.h>
    24 #include <time.h>
    24 #include <time.h>
       
    25 #include <ctype.h>
       
    26 #include <sys/types.h>
       
    27 #include <sys/stat.h>
       
    28 #include <fcntl.h>
    25 
    29 
    26 #include "histolog.h"
    30 #include "histolog.h"
       
    31 #include "jabglue.h"
    27 #include "screen.h"
    32 #include "screen.h"
    28 
    33 
    29 static guint UseFileLogging;
    34 static guint UseFileLogging;
    30 static char *RootDir;
    35 static char *RootDir;
    31 
    36 
    45   return filename;
    50   return filename;
    46 }
    51 }
    47 
    52 
    48 //  write()
    53 //  write()
    49 // Adds a history (multi-)line to the jid's history logfile
    54 // Adds a history (multi-)line to the jid's history logfile
    50 static void write(const char *jid,
    55 static void write_histo_line(const char *jid,
    51         time_t timestamp, guchar type, guchar info, char *data)
    56         time_t timestamp, guchar type, guchar info, const char *data)
    52 {
    57 {
    53   guint len = 0;
    58   guint len = 0;
       
    59   FILE *fp;
    54   time_t ts;
    60   time_t ts;
    55   char *p;
    61   const char *p;
    56   char *filename = user_histo_file(jid);
    62   char *filename = user_histo_file(jid);
    57 
    63 
    58   if (!filename)
    64   if (!filename)
    59     return;
    65     return;
    60 
    66 
    78    * - M message    Info: S (send) R (receive)
    84    * - M message    Info: S (send) R (receive)
    79    * - S status     Info: [oaifdcn]
    85    * - S status     Info: [oaifdcn]
    80    * We don't check them, we'll trust the caller.
    86    * We don't check them, we'll trust the caller.
    81    */
    87    */
    82 
    88 
    83   scr_LogPrint("Log to [%s]:", filename);
    89   fp = fopen(filename, "a");
    84   scr_LogPrint("%c %c %10d %03d %s", type, info, ts, len, data);
    90   if (!fp)
       
    91     return;
       
    92   fprintf(fp, "%c %c %10u %03d %s\n", type, info, (unsigned int)ts, len, data);
       
    93   fclose(fp);
    85 }
    94 }
    86 
    95 
    87 //  hlog_enable()
    96 //  hlog_enable()
    88 // Enable logging to files.  If root_dir is NULL, then $HOME/.mcabber is used.
    97 // Enable logging to files.  If root_dir is NULL, then $HOME/.mcabber is used.
    89 void hlog_enable(guint enable, char *root_dir)
    98 void hlog_enable(guint enable, char *root_dir)
   119     if (RootDir) {
   128     if (RootDir) {
   120     g_free(RootDir);
   129     g_free(RootDir);
   121   }
   130   }
   122 }
   131 }
   123 
   132 
       
   133 inline void hlog_write_message(const char *jid, time_t timestamp, int sent,
       
   134         const char *msg)
       
   135 {
       
   136   write_histo_line(jid, timestamp, 'M', ((sent) ? 'S' : 'R'), msg);
       
   137 }
       
   138 
       
   139 inline void hlog_write_status(const char *jid, time_t timestamp,
       
   140         enum imstatus status)
       
   141 {
       
   142   // #1 XXX Check status value?
       
   143   // #2 We could add a user-readable comment
       
   144   write_histo_line(jid, timestamp, 'S', toupper(imstatus2char[status]),
       
   145           NULL);
       
   146 }
       
   147