akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 1 | #include<stdio.h> |
| 2 | #include<string.h> |
| 3 | #include<stdlib.h> |
| 4 | #include <unistd.h> |
| 5 | #include <getopt.h> |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 6 | #include<ctype.h> |
| 7 | #include<stdarg.h> |
| 8 | #include <sys/types.h> |
| 9 | #include <pwd.h> |
| 10 | #include <errno.h> |
| 11 | #include <sys/stat.h> |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 12 | #include <sys/time.h> |
akmhoque | 0476dc4 | 2012-08-13 09:59:08 -0500 | [diff] [blame] | 13 | #include <time.h> |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 14 | #include <assert.h> |
| 15 | #ifdef HAVE_CONFIG_H |
| 16 | #include <config.h> |
| 17 | #endif |
| 18 | |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 19 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 20 | #include <ccn/ccn.h> |
| 21 | #include <ccn/uri.h> |
| 22 | #include <ccn/keystore.h> |
| 23 | #include <ccn/signing.h> |
| 24 | #include <ccn/schedule.h> |
| 25 | #include <ccn/hashtb.h> |
| 26 | |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 27 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 28 | #include "utility.h" |
| 29 | |
| 30 | |
| 31 | char * getLocalTimeStamp(void) |
| 32 | { |
| 33 | char *timestamp = (char *)malloc(sizeof(char) * 16); |
| 34 | time_t ltime; |
| 35 | ltime=time(NULL); |
| 36 | struct tm *tm; |
| 37 | tm=localtime(<ime); |
| 38 | |
| 39 | sprintf(timestamp, "%04d%02d%02d%02d%02d%02d", tm->tm_year+1900, tm->tm_mon+1, |
| 40 | tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec); |
| 41 | |
| 42 | return timestamp; |
| 43 | } |
| 44 | |
akmhoque | c928669 | 2012-08-16 09:57:58 -0500 | [diff] [blame] | 45 | char * getGmTimeStamp(void) |
| 46 | { |
| 47 | char *timestamp = (char *)malloc(sizeof(char) * 16); |
| 48 | time_t gtime; |
| 49 | gtime=time(NULL); |
| 50 | struct tm *tm; |
| 51 | tm=gmtime(>ime); |
| 52 | |
| 53 | sprintf(timestamp, "%04d%02d%02d%02d%02d%02d", tm->tm_year+1900, tm->tm_mon+1, |
| 54 | tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec); |
| 55 | |
| 56 | return timestamp; |
| 57 | } |
| 58 | |
| 59 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 60 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 61 | |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 62 | long int |
| 63 | get_current_time_sec(void) |
| 64 | { |
| 65 | struct timeval now; |
| 66 | gettimeofday(&now,NULL); |
| 67 | return now.tv_sec; |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 68 | } |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 69 | |
| 70 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 71 | void |
| 72 | get_current_timestamp_micro(char * microSec) |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 73 | { |
| 74 | struct timeval now; |
| 75 | gettimeofday(&now, NULL); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 76 | sprintf(microSec,"%ld%06ld",now.tv_sec,(long int)now.tv_usec); |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 77 | } |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 78 | |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 79 | |
| 80 | long int |
akmhoque | da5b683 | 2012-09-13 22:33:55 -0500 | [diff] [blame] | 81 | get_time_diff(const char *time1, const char *time2) |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 82 | { |
| 83 | long int diff_secs; |
| 84 | |
| 85 | long int time1_in_sec, time2_in_sec; |
| 86 | |
| 87 | char *time1_sec=(char *)malloc(strlen(time1)-6+1); |
| 88 | memset(time1_sec,0,strlen(time1)-6+1); |
| 89 | memcpy(time1_sec,time1,strlen(time1)-6); |
| 90 | |
| 91 | char *time2_sec=(char *)malloc(strlen(time2)-6+1); |
| 92 | memset(time2_sec,0,strlen(time2)-6+1); |
| 93 | memcpy(time2_sec,time2,strlen(time2)-6); |
| 94 | |
| 95 | time1_in_sec=strtol(time1_sec,NULL,10); |
| 96 | time2_in_sec=strtol(time2_sec,NULL,10); |
| 97 | |
| 98 | diff_secs=time1_in_sec-time2_in_sec; |
| 99 | |
| 100 | free(time1_sec); |
| 101 | free(time2_sec); |
| 102 | |
| 103 | return diff_secs; |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 104 | } |
| 105 | |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 106 | |
| 107 | void |
| 108 | startLogging(char *loggingDir) |
| 109 | { |
| 110 | struct passwd pd; |
| 111 | struct passwd* pwdptr=&pd; |
| 112 | struct passwd* tempPwdPtr; |
| 113 | char *pwdbuffer; |
| 114 | int pwdlinelen = 200; |
| 115 | char *logDir; |
| 116 | char *logFileName; |
| 117 | char *ret; |
| 118 | char *logExt; |
| 119 | char *defaultLogDir; |
| 120 | int status; |
| 121 | struct stat st; |
| 122 | int isLogDirExists=0; |
| 123 | char *time=getLocalTimeStamp(); |
| 124 | |
| 125 | pwdbuffer=(char *)malloc(sizeof(char)*200); |
| 126 | memset(pwdbuffer,0,200); |
| 127 | logDir=(char *)malloc(sizeof(char)*200); |
| 128 | memset(logDir,0,200); |
| 129 | logFileName=(char *)malloc(sizeof(char)*200); |
| 130 | memset(logFileName,0,200); |
| 131 | logExt=(char *)malloc(sizeof(char)*5); |
| 132 | memset(logExt,0,5); |
| 133 | defaultLogDir=(char *)malloc(sizeof(char)*10); |
| 134 | memset(defaultLogDir,0,10); |
| 135 | |
| 136 | memcpy(logExt,".log",4); |
| 137 | logExt[4]='\0'; |
| 138 | memcpy(defaultLogDir,"/nlsrLog",9); |
| 139 | defaultLogDir[9]='\0'; |
| 140 | |
| 141 | if(loggingDir!=NULL) |
| 142 | { |
| 143 | if( stat( loggingDir, &st)==0) |
| 144 | { |
| 145 | if ( st.st_mode & S_IFDIR ) |
| 146 | { |
| 147 | if( st.st_mode & S_IWUSR) |
| 148 | { |
| 149 | isLogDirExists=1; |
| 150 | memcpy(logDir,loggingDir,strlen(loggingDir)+1); |
| 151 | } |
| 152 | else printf("User do not have write permission to %s \n",loggingDir); |
| 153 | } |
| 154 | else printf("Provided path for %s is not a directory!!\n",loggingDir); |
| 155 | } |
| 156 | else printf("Log directory: %s does not exists\n",loggingDir); |
| 157 | } |
| 158 | |
| 159 | if(isLogDirExists == 0) |
| 160 | { |
| 161 | if ((getpwuid_r(getuid(),pwdptr,pwdbuffer,pwdlinelen,&tempPwdPtr))!=0) |
| 162 | perror("getpwuid_r() error."); |
| 163 | else |
| 164 | { |
| 165 | memcpy(logDir,pd.pw_dir,strlen(pd.pw_dir)+1); |
| 166 | memcpy(logDir+strlen(logDir),defaultLogDir,strlen(defaultLogDir)+1); |
| 167 | if(stat(logDir,&st) != 0) |
| 168 | status = mkdir(logDir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); |
| 169 | } |
| 170 | } |
| 171 | memcpy(logFileName,logDir,strlen(logDir)+1); |
| 172 | if( logDir[strlen(logDir)-1]!='/') |
| 173 | { |
| 174 | memcpy(logFileName+strlen(logFileName),"/",1); |
| 175 | memcpy(logFileName+strlen(logFileName),"\0",1); |
| 176 | } |
| 177 | memcpy(logFileName+strlen(logFileName),time,strlen(time)+1); |
| 178 | memcpy(logFileName+strlen(logFileName),logExt,strlen(logExt)+1); |
| 179 | ret=(char *)malloc(strlen(logFileName)+1); |
| 180 | memset(ret,0,strlen(logFileName)+1); |
| 181 | memcpy(ret,logFileName,strlen(logFileName)+1); |
| 182 | |
| 183 | setenv("NLSR_LOG_FILE",ret,1); |
| 184 | |
| 185 | free(time); |
| 186 | free(logDir); |
| 187 | free(logFileName); |
| 188 | free(pwdbuffer); |
| 189 | free(logExt); |
| 190 | free(defaultLogDir); |
| 191 | free(ret); |
| 192 | } |
| 193 | |
| 194 | |
| 195 | void |
| 196 | writeLogg(const char *source_file, const char *function, const int line, const char *format, ...) |
| 197 | { |
| 198 | char *file=getenv("NLSR_LOG_FILE"); |
| 199 | if (file != NULL) |
| 200 | { |
| 201 | FILE *fp = fopen(file, "a"); |
| 202 | |
| 203 | if (fp != NULL) |
| 204 | { |
| 205 | struct timeval t; |
| 206 | gettimeofday(&t, NULL); |
akmhoque | 9e9fc72 | 2012-09-26 14:03:25 -0500 | [diff] [blame^] | 207 | fprintf(fp,"%ld.%06u - %s, %s, %d :",(long)t.tv_sec , (unsigned)t.tv_usec , source_file , function , line); |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 208 | va_list args; |
| 209 | va_start(args, format); |
| 210 | vfprintf(fp, format, args); |
| 211 | fclose(fp); |
| 212 | va_end(args); |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | |