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 <stdarg.h>
00038 #include <stdlib.h>
00039 #include <stdio.h>
00040 #include <string.h>
00041
00042 #include "rdd.h"
00043 #include "rdd_internals.h"
00044 #include "msgprinter.h"
00045
00046 typedef struct _RDD_MSGPRINTER_POS {
00047 char *msgbuf;
00048 unsigned buflen;
00049 } RDD_MSGPRINTER_POS;
00050
00051 int
00052 rdd_mp_open_printer(RDD_MSGPRINTER **printer, RDD_MSGPRINTER_OPS *ops,
00053 unsigned statesize)
00054 {
00055 RDD_MSGPRINTER *p = 0;
00056 *printer = 0;
00057
00058 if ((p = calloc(1, sizeof(RDD_MSGPRINTER))) == 0) {
00059 return RDD_NOMEM;
00060 }
00061
00062 if ((p->state = calloc(1, statesize)) == 0) {
00063 free(p);
00064 return RDD_NOMEM;
00065 }
00066
00067 p->ops = ops;
00068 p->mask = RDD_MSG_INFO|RDD_MSG_WARN|RDD_MSG_ERROR|RDD_MSG_DEBUG;
00069
00070 *printer = p;
00071 return RDD_OK;
00072 }
00073
00074 int
00075 rdd_mp_close(RDD_MSGPRINTER *printer, unsigned flags)
00076 {
00077
00078 RDD_MSGPRINTER_OPS *ops = printer->ops;
00079
00080 if (ops->close != 0) {
00081 return (*ops->close)(printer, flags);
00082 }
00083
00084 free(printer->state);
00085 printer->state = 0;
00086 free(printer);
00087 return RDD_OK;
00088 }
00089
00090 uint32_t
00091 rdd_mp_get_mask(RDD_MSGPRINTER *printer)
00092 {
00093 return printer->mask;
00094 }
00095
00096 void
00097 rdd_mp_set_mask(RDD_MSGPRINTER *printer, uint32_t mask)
00098 {
00099 printer->mask = mask;
00100 }
00101
00102 static int
00103 mp_accept_message(RDD_MSGPRINTER *printer, rdd_message_t type)
00104 {
00105 if (printer == 0) return 0;
00106
00107 if (printer->ops->print == 0) return 0;
00108
00109 if ((printer->mask & type) == 0) return 0;
00110
00111 return 1;
00112 }
00113
00114 void
00115 rdd_mp_print(RDD_MSGPRINTER *printer,
00116 rdd_message_t type, int errcode, const char *fmt, ...)
00117 {
00118 va_list ap;
00119
00120 if (! mp_accept_message(printer, type)) return;
00121
00122 va_start(ap, fmt);
00123 vsnprintf(printer->printbuf, sizeof(printer->printbuf), fmt, ap);
00124 printer->printbuf[sizeof(printer->printbuf) - 1] = '\000';
00125 va_end(ap);
00126
00127 (*printer->ops->print)(printer, type, errcode, printer->printbuf);
00128 }
00129
00130 void
00131 rdd_mp_vmessage(RDD_MSGPRINTER *printer,
00132 rdd_message_t type, const char *fmt, va_list ap)
00133 {
00134 if (! mp_accept_message(printer, type)) return;
00135
00136 vsnprintf(printer->printbuf, sizeof(printer->printbuf), fmt, ap);
00137 printer->printbuf[sizeof(printer->printbuf) - 1] = '\000';
00138
00139 (*printer->ops->print)(printer, type, 0, printer->printbuf);
00140 }
00141
00142 void
00143 rdd_mp_message(RDD_MSGPRINTER *printer,
00144 rdd_message_t type, const char *fmt, ...)
00145 {
00146 va_list ap;
00147
00148 va_start(ap, fmt);
00149 rdd_mp_vmessage(printer, type, fmt, ap);
00150 va_end(ap);
00151 }
00152
00153 static void
00154 mp_init(RDD_MSGPRINTER_POS *pos, RDD_MSGPRINTER *printer)
00155 {
00156 printer->printbuf[0] = '\000';
00157 pos->msgbuf = printer->printbuf;
00158 pos->buflen = sizeof(printer->printbuf);
00159 }
00160
00161 static void
00162 mp_vprintf(RDD_MSGPRINTER_POS *pos, const char *fmt, va_list ap)
00163 {
00164 unsigned msglen;
00165
00166 if (pos->buflen <= 0) return;
00167
00168 vsnprintf(pos->msgbuf, pos->buflen, fmt, ap);
00169 pos->msgbuf[pos->buflen - 1] = '\000';
00170 msglen = strlen(pos->msgbuf);
00171 pos->msgbuf += msglen;
00172 pos->buflen -= msglen;
00173 }
00174
00175 static void
00176 mp_printf(RDD_MSGPRINTER_POS *pos, const char *fmt, ...)
00177 {
00178 va_list ap;
00179
00180 va_start(ap, fmt);
00181 mp_vprintf(pos, fmt, ap);
00182 va_end(ap);
00183 }
00184
00185 void
00186 rdd_mp_unixmsg(RDD_MSGPRINTER *printer,
00187 rdd_message_t type, int unix_errno, const char *fmt, ...)
00188 {
00189 RDD_MSGPRINTER_POS pos;
00190 va_list ap;
00191
00192 if (! mp_accept_message(printer, type)) return;
00193
00194 va_start(ap, fmt);
00195 mp_init(&pos, printer);
00196 mp_vprintf(&pos, fmt, ap);
00197 mp_printf(&pos, ": %s", strerror(unix_errno));
00198 va_end(ap);
00199
00200 (*printer->ops->print)(printer, type, unix_errno, printer->printbuf);
00201 }
00202
00203 void
00204 rdd_mp_vrddmsg(RDD_MSGPRINTER *printer,
00205 rdd_message_t type, int rdd_errno, const char *fmt, va_list ap)
00206 {
00207 RDD_MSGPRINTER_POS pos;
00208 char rddbuf[128];
00209
00210 if (! mp_accept_message(printer, type)) return;
00211
00212 mp_init(&pos, printer);
00213 mp_vprintf(&pos, fmt, ap);
00214 if (rdd_strerror(rdd_errno, rddbuf, sizeof rddbuf) == RDD_OK) {
00215 rddbuf[(sizeof rddbuf) - 1] = '\000';
00216 mp_printf(&pos, ": %s", rddbuf);
00217 }
00218
00219 (*printer->ops->print)(printer, type, rdd_errno, printer->printbuf);
00220 }
00221
00222 void
00223 rdd_mp_rddmsg(RDD_MSGPRINTER *printer,
00224 rdd_message_t type, int rdd_errno, const char *fmt, ...)
00225 {
00226 va_list ap;
00227
00228 va_start(ap, fmt);
00229 rdd_mp_vrddmsg(printer, type, rdd_errno, fmt, ap);
00230 va_end(ap);
00231 }