📄 debug.c
字号:
/* * Copyright (C) 2001, University of California, Santa Barbara * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * Other copyrights might apply to parts of this software and are so * noted when applicable. *//* * Parts of this program has been derived from PIM sparse-mode pimd. * The pimd program is covered by the license in the accompanying file * named "LICENSE.pimd". * * The pimd program is COPYRIGHT 1998 by University of Southern California. * */#include <stdarg.h>#include <stdlib.h>#include <stdio.h>#include <syslog.h>#include <sys/time.h>#include <time.h>#include <netinet/in.h>#include "debug.h"#include "const.h"#include "inet.h"#include "main.h"#include "precursor.h"#include "rrep.h"#include "rreq.h"#include "routingTableEntry.h"unsigned int debug = LOG_DEBUG;u_int32_t tracetag = 0;int logToScreen = FALSE;int logToSysLog = FALSE;int showtracetags = FALSE;void log(int severity, int logToStderr, char *format, ...){ struct timeval now; struct tm *time; static char fmt[256]; char *msg; va_list ap; if (severity <= debug) { va_start(ap, format); //make ap point to first arg gettimeofday(&now,NULL); time = localtime(&now.tv_sec); vsprintf(fmt, format, ap); va_end(ap); msg=fmt; if (logToStderr) { fprintf(stderr,"%02d:%02d:%02d.%03ld %s\n", time->tm_hour, time->tm_min, time->tm_sec, now.tv_usec / 1000, msg); } if (logToScreen) { printf("%02d:%02d:%02d.%03ld %s\n", time->tm_hour, time->tm_min, time->tm_sec, now.tv_usec / 1000, msg); } if(logToSysLog){ syslog(severity, "%s", msg); } } if (severity <= LOG_ERR){ exit(-1); }}void trace(u_int32_t tag, char *format, ...){ struct timeval now; struct tm *time; static char fmt[256]; char *msg; int i,lengthNames; struct tagname *tnames; va_list ap; if(debug == LOG_DEBUG){ if((tracetag & tag) != 0){ va_start(ap, format); //make ap point to first arg gettimeofday(&now,NULL); time = localtime(&now.tv_sec); vsprintf(fmt, format, ap); va_end(ap); msg=fmt; if (showtracetags == FALSE){ if (logToScreen) { printf("%02d:%02d:%02d.%03ld TRACE %s\n", time->tm_hour, time->tm_min, time->tm_sec, now.tv_usec / 1000, msg); } if(logToSysLog){ syslog(LOG_DEBUG, "TRACE %s", msg); } }else{ lengthNames = sizeof(tagnames)/sizeof(tagnames[0]); for (i = 0,tnames = tagnames; i < lengthNames ; i++, tnames++){ if((tnames->val != -1) && ((tnames->val & tag) != 0)){ if (logToScreen) { printf("%02d:%02d:%02d.%03ld TRACE %-15s: %s\n" , time->tm_hour ,time->tm_min , time->tm_sec , now.tv_usec / 1000 , tnames->name , msg); } if(logToSysLog){ syslog(LOG_DEBUG, "TRACE %s", msg); } } } } } } }void printRREQ(struct RREQ *rreq){ trace(TRACE_RREQ, "Printing RREQ"); trace(TRACE_RREQ, "*************"); trace(TRACE_RREQ, "G:%i",rreq->G); trace(TRACE_RREQ, "hopCount:%i",rreq->hopCount); trace(TRACE_RREQ, "broadcastId:%u",ntohl(rreq->broadcastId)); trace(TRACE_RREQ, "dest:%s",inet_fmt_n(rreq->dest,s1)); trace(TRACE_RREQ, "destSeqNum:%u",ntohl(rreq->destSeqNum)); trace(TRACE_RREQ, "src:%s",inet_fmt_n(rreq->src,s1)); trace(TRACE_RREQ, "srcSeqNum:%u",ntohl(rreq->srcSeqNum)); trace(TRACE_RREQ, "*************");}void printRREP(struct RREP *rrep){ trace(TRACE_RREP, "Printing RREP"); trace(TRACE_RREP, "*************"); trace(TRACE_RREP, "R:%i",rrep->R); trace(TRACE_RREP, "A:%i",rrep->A); trace(TRACE_RREP, "prefixSize:%i",rrep->prefixSize); trace(TRACE_RREP, "hopCount:%i",rrep->hopCount); trace(TRACE_RREP, "dest:%s",inet_fmt_n(rrep->dest,s1)); trace(TRACE_RREP, "destSeqNum:%u",ntohl(rrep->destSeqNum)); trace(TRACE_RREP, "src:%s",inet_fmt_n(rrep->src,s1)); trace(TRACE_RREP, "lifetime:%u",ntohl(rrep->lifetime)); trace(TRACE_RREP, "*************");}void printRERR(struct RERR *rerr){ struct UnreachableDest *unreach = NULL; trace(TRACE_RERR, "Printing RERR"); trace(TRACE_RERR, "*************"); trace(TRACE_RERR, "N:%i",rerr->N); trace(TRACE_RERR, "destCount:%u",rerr->destCount); if(rerr->unreachables == NULL){ trace(TRACE_RERR, "unreach NULL"); }else { for (unreach = rerr->unreachables ; unreach != NULL ; unreach = unreach->next ) { trace(TRACE_RERR, "unreach->dest:%s" ,inet_fmt_n(unreach->unreachableDest,s1)); trace(TRACE_RERR, "unreach->destSeqNum:%u" ,ntohl(unreach->unreachableDestSeqNum)); } } trace(TRACE_RERR, "*************");}void printEntry(struct RoutingTableEntry *entry){ struct Precursor *precur = NULL; trace(TRACE_RT, "Printing RoutingTableEntry"); trace(TRACE_RT, "**************************"); trace(TRACE_RT, "dest:%s",inet_fmt_h(entry->dest,s1)); //trace(TRACE_RT, "dest:%u",entry->dest); trace(TRACE_RT, "destSeqNum:%u",entry->destSeqNum); trace(TRACE_RT, "interface:%u",entry->interface); trace(TRACE_RT, "hopCount:%u",entry->hopCount); trace(TRACE_RT, "lastHopCount:%u",entry->lastHopCount); trace(TRACE_RT, "nextHop:%s",inet_fmt_h(entry->nextHop,s1)); trace(TRACE_RT, "lifetime:%u",entry->lifetime); trace(TRACE_RT, "routingFlags:%u",entry->routingFlags); if(entry->precursors == NULL){ trace(TRACE_RT, "precur NULL"); }else { precur=entry->precursors; while(precur != NULL){ trace(TRACE_RT, "precursor:%s",inet_fmt_h(precur->dest,s1)); precur=precur->next; } } if(entry->next == NULL){ trace(TRACE_RT, "next NULL"); }else { trace(TRACE_RT, "next:%s",inet_fmt_h(entry->next->dest,s1)); //trace(TRACE_RT, "at:%u",entry->next); } trace(TRACE_RT, "**************************");}void printEntryForChange(struct RoutingTableEntry *entry){ trace(TRACE_RT_CHANGE, "**************************"); trace(TRACE_RT_CHANGE, "dest:%s,destSeqNum:%u" ,inet_fmt_h(entry->dest,s1),entry->destSeqNum); trace(TRACE_RT_CHANGE, "nextHop:%s",inet_fmt_h(entry->nextHop,s1)); trace(TRACE_RT_CHANGE, "hopCount:%u,lastHopCount:%u" ,entry->hopCount,entry->lastHopCount); trace(TRACE_RT_CHANGE, "**************************");}void initializeLogging(){#ifdef LOG_DAEMON (void)openlog("aodvd", LOG_PID, LOG_DAEMON); (void)setlogmask(LOG_UPTO(LOG_NOTICE));#else (void)openlog("aodvd", LOG_PID);#endif#ifdef SYSV srand48(time(NULL));#else srandom(time(NULL));#endif log(LOG_INFO, FALSE, "aodvd version %s started", VERSION); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -