servlib/bundling/S10Logger.cc
changeset 0 2b3e5ec03512
child 12 7463e4bb80e4
equal deleted inserted replaced
-1:000000000000 0:2b3e5ec03512
       
     1 /*
       
     2  *    Copyright 2010 Trinity College Dublin
       
     3  * 
       
     4  *    Licensed under the Apache License, Version 2.0 (the "License");
       
     5  *    you may not use this file except in compliance with the License.
       
     6  *    You may obtain a copy of the License at
       
     7  * 
       
     8  *        http://www.apache.org/licenses/LICENSE-2.0
       
     9  * 
       
    10  *    Unless required by applicable law or agreed to in writing, software
       
    11  *    distributed under the License is distributed on an "AS IS" BASIS,
       
    12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    13  *    See the License for the specific language governing permissions and
       
    14  *    limitations under the License.
       
    15  */
       
    16 
       
    17 #ifdef HAVE_CONFIG_H
       
    18 #  include <dtn-config.h>
       
    19 #endif
       
    20 
       
    21 #include <string.h>
       
    22 
       
    23 #include <oasys/debug/DebugUtils.h>
       
    24 #include <oasys/thread/SpinLock.h>
       
    25 
       
    26 #include "Bundle.h"
       
    27 #include "BundleDaemon.h"
       
    28 #include "BundleList.h"
       
    29 #include "ExpirationTimer.h"
       
    30 
       
    31 #include "storage/GlobalStore.h"
       
    32 
       
    33 #include "S10Logger.h"
       
    34 
       
    35 static char *flagstr[]= {
       
    36 		"",
       
    37 		"BFLAG_TXCACK",
       
    38 		"BFLAG_RXCACK",
       
    39 		"BFLAG_PINGACK",
       
    40 		"BFLAG_TRACEACK"
       
    41 };
       
    42 
       
    43 // S10 event strings
       
    44 static char *eventstr[] = {
       
    45 				"",
       
    46 				"FROMAPP",
       
    47 				"FROMDB",
       
    48 				"DELIVERED",
       
    49 				"TAKECUST",
       
    50 				"RELCUST",
       
    51 				"EXPIRY",
       
    52 				"TX",
       
    53 				"RX",
       
    54 				"SPARE",
       
    55 				"TXADMIN",
       
    56 				"RXADMIN",
       
    57 				"DUP",
       
    58 				"CONTUP",
       
    59 				"CONTDOWN",
       
    60 				"STARTING",
       
    61 				"EXITING",
       
    62 				"OHCRAPBADEVENT"};
       
    63 
       
    64 static char *contstr[] ={
       
    65 	"",
       
    66 	"ALWAYSON",
       
    67 	"ONDEMAND",
       
    68 	"SCHEDULED",
       
    69 	"OPPORTUNISTIC"
       
    70 };
       
    71 
       
    72 // Field width for log info
       
    73 #define LOGFW 256
       
    74 
       
    75 // ok, I basically write C code, so sue me:-)
       
    76 // This is the generic CSV structure
       
    77 typedef struct logrep_struct {
       
    78 	char	rfc3339date[LOGFW];
       
    79 	time_t	eventTime;   // maybe not needed?
       
    80 	int		eventUsec;
       
    81 	char	loggingNode[LOGFW];
       
    82 	int		event;
       
    83 	size_t	size;
       
    84 	char	srcEID[LOGFW];
       
    85 	char 	bundleID[LOGFW];
       
    86 	char	destEID[LOGFW];
       
    87 	char	expiry[LOGFW];
       
    88 	int		bFlags; // dunno yat
       
    89 	char	peer[LOGFW];
       
    90 	char	otherBundleSrc[LOGFW];
       
    91 	char	otherBundleID[LOGFW];
       
    92 	time_t	otherTime;
       
    93 	int		otherUsec;
       
    94 	int		contType;
       
    95 	int		duration;
       
    96 	const	char *cmt;
       
    97 } logrep_t;
       
    98 
       
    99 int logLr(logrep_t	*lrp){
       
   100 
       
   101 	static bool inited=false;
       
   102 	if (!inited) {
       
   103 		log_info_p("S10","rfc3339,time_t.usec,node,event,size,src,id,dest,expiry,flags,peer,ob-src,ob-id,otime.ousec,conttype,duration,cmt");
       
   104 		inited=true;
       
   105 	}
       
   106 	log_info_p("S10","%s,%lu.%06d,%s,%s,%d,%s,%s,%s,%s,%s,%s,%s,%s,%lu.%06d,%s,%d,%s\n",
       
   107 		lrp->rfc3339date, lrp->eventTime, lrp->eventUsec,
       
   108 		lrp->loggingNode, 
       
   109 		eventstr[lrp->event], // need to protect this index
       
   110 		lrp->size,lrp->srcEID,lrp->bundleID,lrp->destEID,
       
   111 		lrp->expiry,flagstr[lrp->bFlags],
       
   112 		lrp->peer,lrp->otherBundleSrc,lrp->otherBundleID,
       
   113 		lrp->otherTime,lrp->otherUsec,contstr[lrp->contType], lrp->duration,
       
   114 		lrp->cmt?lrp->cmt:"");
       
   115 	return(0);
       
   116 }
       
   117 
       
   118 static bool localset=false;
       
   119 static char localeid[LOGFW];
       
   120 
       
   121 int s10_setlocal(const char *eid)
       
   122 {
       
   123 	if (localset) return(1);
       
   124 	snprintf(localeid,LOGFW,eid);
       
   125 	localset=true;
       
   126 	return(0);
       
   127 }
       
   128 
       
   129 int basicLr(logrep_t	*lrp)
       
   130 {
       
   131 	memset(lrp,0,sizeof(logrep_t));
       
   132     struct timeval tv;
       
   133     gettimeofday(&tv, 0);
       
   134 	lrp->eventTime=tv.tv_sec;
       
   135     lrp->eventUsec=tv.tv_usec;
       
   136 	char *fmtstr="%Y-%m-%d %H:%M:%S";
       
   137 	struct tm *tmval;
       
   138 	tmval=gmtime(&lrp->eventTime);
       
   139 	strftime(lrp->rfc3339date,1024,fmtstr,tmval);
       
   140 	if (localset) snprintf(lrp->loggingNode,LOGFW,localeid);
       
   141 	else gethostname(lrp->loggingNode,LOGFW);
       
   142 	return(0);
       
   143 }
       
   144 
       
   145 int s10_daemon(int event) {
       
   146 	if (event==S10_STARTING) {
       
   147 		log_notice_p("/S10", "Lines marked \"S10\" are log lines added to make parsing");
       
   148 		log_notice_p("/S10", "the logs easier after extended tests. These were added for");
       
   149 		log_notice_p("/S10", "the summer 2010 N4C summer trial and are mainly new lines");
       
   150 		log_notice_p("/S10", "that allow tracing bundles across multiple hops.");
       
   151 		log_notice_p("/S10", "To extract these do the following...");
       
   152 		log_notice_p("/S10", "\t grep \"S10 info\" <dtnd-log-files> | grep -v grep | awk '{print $4,$5}' | sort -n");
       
   153 
       
   154 	}
       
   155 	logrep_t lr;
       
   156 	basicLr(&lr);
       
   157 	if (event >0 && event <= S10_MAXEVENT) lr.event=event;
       
   158 	else lr.event=event; 
       
   159 	logLr(&lr);
       
   160 	return(0);
       
   161 }
       
   162 
       
   163 int s10_bundle(
       
   164 		int event, 
       
   165 		dtn::Bundle::Bundle* bp, 
       
   166 		const char *peer, 
       
   167 		time_t ot,
       
   168 		int ousec,
       
   169 		dtn::Bundle::Bundle* otherBundle,
       
   170 		const char *cmt)
       
   171 {
       
   172 	logrep_t lr;
       
   173 	basicLr(&lr);
       
   174 	if (event >0 && event <= S10_MAXEVENT) lr.event=event;
       
   175 	else lr.event=S10_OHCRAP; 
       
   176 	if (cmt) lr.cmt=cmt;
       
   177 
       
   178 	// set src
       
   179 	snprintf(lr.srcEID,LOGFW,bp->source().c_str());
       
   180 	snprintf(lr.bundleID,LOGFW,"%llu.%llu",bp->creation_ts().seconds_,bp->creation_ts().seqno_);
       
   181 	snprintf(lr.destEID,LOGFW,bp->dest().c_str());
       
   182 	lr.size=bp->payload().length();
       
   183 	snprintf(lr.expiry,LOGFW,"%llu",bp->expiration());
       
   184 	if (peer) snprintf(lr.peer,LOGFW,peer);
       
   185 	if (otherBundle) {
       
   186 		snprintf(lr.otherBundleSrc,LOGFW,otherBundle->source().c_str());
       
   187 		snprintf(lr.otherBundleID,LOGFW,"%llu.%llu",
       
   188 			otherBundle->creation_ts().seconds_,otherBundle->creation_ts().seqno_);
       
   189 	}
       
   190 	logLr(&lr);
       
   191 	return(0);
       
   192 }
       
   193 
       
   194 
       
   195 int s10_contact(
       
   196 		int event,
       
   197 		dtn::Contact::Contact *ct,
       
   198 		const char *cmt)
       
   199 {
       
   200 	logrep_t lr;
       
   201 	basicLr(&lr);
       
   202 	if (event != S10_CONTUP && event != S10_CONTDOWN) lr.event=S10_OHCRAP;
       
   203 	else lr.event=event; 
       
   204 	if (cmt) lr.cmt=cmt;
       
   205 	if (event==S10_CONTDOWN) {
       
   206 		lr.otherTime=ct->start_time().sec_;
       
   207 		lr.otherUsec=ct->start_time().usec_;
       
   208 		lr.duration=lr.eventTime-lr.otherTime;
       
   209 	}
       
   210 	snprintf(lr.peer,LOGFW,ct->link()->nexthop());
       
   211 	lr.contType=ct->link()->type();
       
   212 	logLr(&lr);
       
   213 	return(0);
       
   214 	return(0);
       
   215 }