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 #include <stdio.h>
00026 #include <sys/time.h>
00027 #include <stdarg.h>
00028 #include <stdlib.h>
00029 #include <string.h>
00030 #include <strings.h>
00031 #include <time.h>
00032 #include <unistd.h>
00033
00034 #include <ccn/ccn.h>
00035 #include <ccn/charbuf.h>
00036 #include <ccn/uri.h>
00037
00038 #include "ccnr_private.h"
00039
00040 #include "ccnr_msg.h"
00041
00042
00043
00044
00045
00046
00047 int
00048 ccnr_msg_level_from_string(const char *s)
00049 {
00050 long v;
00051 char *ep;
00052
00053 if (s == NULL || s[0] == 0)
00054 return(1);
00055 if (0 == strcasecmp(s, "NONE"))
00056 return(CCNL_NONE);
00057 if (0 == strcasecmp(s, "SEVERE"))
00058 return(CCNL_SEVERE);
00059 if (0 == strcasecmp(s, "ERROR"))
00060 return(CCNL_ERROR);
00061 if (0 == strcasecmp(s, "WARNING"))
00062 return(CCNL_WARNING);
00063 if (0 == strcasecmp(s, "INFO"))
00064 return(CCNL_INFO);
00065 if (0 == strcasecmp(s, "FINE"))
00066 return(CCNL_FINE);
00067 if (0 == strcasecmp(s, "FINER"))
00068 return(CCNL_FINER);
00069 if (0 == strcasecmp(s, "FINEST"))
00070 return(CCNL_FINEST);
00071 v = strtol(s, &ep, 10);
00072 if (v > CCNL_FINEST || v < 0 || ep[0] != 0)
00073 return(-1);
00074 return(v);
00075 }
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085 void
00086 ccnr_msg(struct ccnr_handle *h, const char *fmt, ...)
00087 {
00088 struct timeval t;
00089 va_list ap;
00090 struct ccn_charbuf *b;
00091 int res;
00092 if (h == NULL || h->debug == 0 || h->logger == 0)
00093 return;
00094 b = ccn_charbuf_create();
00095 if (b == NULL)
00096 return;
00097 gettimeofday(&t, NULL);
00098 if ((h->debug >= CCNL_FINE) &&
00099 ((h->logbreak-- < 0 && t.tv_sec != h->logtime) ||
00100 t.tv_sec >= h->logtime + 30)) {
00101 ccn_charbuf_putf(b, "%ld.000000 ccnr[%d]: %s ____________________ %s",
00102 (long)t.tv_sec, h->logpid, h->portstr ? h->portstr : "", ctime(&t.tv_sec));
00103 h->logtime = t.tv_sec;
00104 h->logbreak = 30;
00105 }
00106 ccn_charbuf_putf(b, "%ld.%06u ccnr[%d]: %s\n",
00107 (long)t.tv_sec, (unsigned)t.tv_usec, h->logpid, fmt);
00108 va_start(ap, fmt);
00109
00110 res = (*h->logger)(h->loggerdata, ccn_charbuf_as_string(b), ap);
00111 va_end(ap);
00112 ccn_charbuf_destroy(&b);
00113
00114 if (res < 0)
00115 h->debug = 0;
00116 }
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128 void
00129 ccnr_debug_ccnb(struct ccnr_handle *h,
00130 int lineno,
00131 const char *msg,
00132 struct fdholder *fdholder,
00133 const unsigned char *ccnb,
00134 size_t ccnb_size)
00135 {
00136 struct ccn_charbuf *c;
00137 struct ccn_parsed_interest pi;
00138 const unsigned char *nonce = NULL;
00139 size_t nonce_size = 0;
00140 size_t i;
00141
00142
00143 if (h != NULL && h->debug == 0)
00144 return;
00145 c = ccn_charbuf_create();
00146 ccn_charbuf_putf(c, "debug.%d %s ", lineno, msg);
00147 if (fdholder != NULL)
00148 ccn_charbuf_putf(c, "%u ", fdholder->filedesc);
00149 ccn_uri_append(c, ccnb, ccnb_size, 1);
00150 ccn_charbuf_putf(c, " (%u bytes)", (unsigned)ccnb_size);
00151 if (ccn_parse_interest(ccnb, ccnb_size, &pi, NULL) >= 0) {
00152 const char *p = "";
00153 ccn_ref_tagged_BLOB(CCN_DTAG_Nonce, ccnb,
00154 pi.offset[CCN_PI_B_Nonce],
00155 pi.offset[CCN_PI_E_Nonce],
00156 &nonce,
00157 &nonce_size);
00158 if (nonce_size > 0) {
00159 ccn_charbuf_putf(c, " ");
00160 if (nonce_size == 12)
00161 p = "CCC-P-F-T-NN";
00162 for (i = 0; i < nonce_size; i++)
00163 ccn_charbuf_putf(c, "%s%02X", (*p) && (*p++)=='-' ? "-" : "", nonce[i]);
00164 }
00165 }
00166 ccnr_msg(h, "%s", ccn_charbuf_as_string(c));
00167 ccn_charbuf_destroy(&c);
00168 }
00169