00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #ifdef HAVE_CONFIG_H
00034 #include "config.h"
00035 #endif
00036
00037 #include <string.h>
00038 #include <time.h>
00039
00040 #include "rdd.h"
00041 #include "rdd_internals.h"
00042 #include "msgprinter.h"
00043
00044 typedef struct _RDD_LOG_MSGPRINTER {
00045 RDD_MSGPRINTER *next;
00046 } RDD_LOG_MSGPRINTER;
00047
00048 static void log_print(RDD_MSGPRINTER *printer,
00049 rdd_message_t type, int errcode, const char *msg);
00050 static int log_close(RDD_MSGPRINTER *printer, unsigned flags);
00051
00052 static RDD_MSGPRINTER_OPS log_ops = {
00053 log_print,
00054 log_close
00055 };
00056
00057 int
00058 rdd_mp_open_log_printer(RDD_MSGPRINTER **printer, RDD_MSGPRINTER *next)
00059 {
00060 RDD_LOG_MSGPRINTER *log = 0;
00061 RDD_MSGPRINTER *p = 0;
00062 int rc = RDD_OK;
00063
00064 rc = rdd_mp_open_printer(&p, &log_ops, sizeof(RDD_LOG_MSGPRINTER));
00065 if (rc != RDD_OK) {
00066 return rc;
00067 }
00068
00069 log = (RDD_LOG_MSGPRINTER *) p->state;
00070 log->next = next;
00071
00072 *printer = p;
00073 return RDD_OK;
00074 }
00075
00076 static void
00077 log_print(RDD_MSGPRINTER *printer, rdd_message_t type, int errcode,
00078 const char *msg)
00079 {
00080 RDD_LOG_MSGPRINTER *log = (RDD_LOG_MSGPRINTER *) printer->state;
00081 time_t now_unix;
00082 char now_buf[64];
00083 struct tm *now_local;
00084 size_t n;
00085
00086 if ((now_unix = time(NULL)) == (time_t) -1)
00087 goto error;
00088 if ((now_local = localtime(&now_unix)) == NULL)
00089 goto error;
00090 n = strftime(now_buf, sizeof now_buf, "%Y-%m-%d %T %z", now_local);
00091 if (n == 0 || n >= (sizeof now_buf))
00092 goto error;
00093 now_buf[(sizeof now_buf) - 1] = '\000';
00094
00095 rdd_mp_print(log->next, type, errcode, "%s: %s", now_buf, msg);
00096 return;
00097
00098 error:
00099
00100
00101
00102
00103
00104
00105 rdd_mp_print(log->next, type, errcode, "???: %s", msg);
00106 }
00107
00108 static int
00109 log_close(RDD_MSGPRINTER *printer, unsigned flags)
00110 {
00111 RDD_LOG_MSGPRINTER *log = (RDD_LOG_MSGPRINTER *) printer->state;
00112 int rc;
00113
00114 if ((flags & RDD_MP_RECURSE) != 0) {
00115 if ((rc = rdd_mp_close(log->next, flags)) != RDD_OK) {
00116 return rc;
00117 }
00118 }
00119
00120 memset(log, 0, sizeof *log);
00121 return RDD_OK;
00122 }