blob: 1218623a6c4e685b37a21cdeeb8db6ae732b50f1 [file] [log] [blame]
akmhoque59980a52012-08-09 12:36:09 -05001#include<stdio.h>
2#include<string.h>
3#include<stdlib.h>
4#include <unistd.h>
5#include <getopt.h>
akmhoquebfefef22012-09-26 10:09:34 -05006#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>
akmhoque59980a52012-08-09 12:36:09 -050012#include <sys/time.h>
akmhoque0476dc42012-08-13 09:59:08 -050013#include <time.h>
akmhoque59980a52012-08-09 12:36:09 -050014#include <assert.h>
15#ifdef HAVE_CONFIG_H
16#include <config.h>
17#endif
18
akmhoquebfefef22012-09-26 10:09:34 -050019
akmhoque53f64222012-09-05 13:57:51 -050020#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
akmhoquebfefef22012-09-26 10:09:34 -050027
akmhoque59980a52012-08-09 12:36:09 -050028#include "utility.h"
29
30
31char * 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(&ltime);
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
akmhoquec9286692012-08-16 09:57:58 -050045char * 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(&gtime);
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
akmhoque59980a52012-08-09 12:36:09 -050060
akmhoque59980a52012-08-09 12:36:09 -050061
akmhoquef71d9082012-08-22 12:51:53 -040062long int
63get_current_time_sec(void)
64{
65 struct timeval now;
66 gettimeofday(&now,NULL);
67 return now.tv_sec;
akmhoque59980a52012-08-09 12:36:09 -050068}
akmhoqued79438d2012-08-27 13:31:42 -050069
70
akmhoque03004e62012-09-06 01:12:28 -050071void
72get_current_timestamp_micro(char * microSec)
akmhoque53f64222012-09-05 13:57:51 -050073{
akmhoque1001d282012-10-11 04:11:33 -050074 time_t gtime;
75 gtime=time(NULL);
76 struct tm *tm;
77 tm=localtime(&gtime);
78
akmhoque53f64222012-09-05 13:57:51 -050079 struct timeval now;
80 gettimeofday(&now, NULL);
akmhoque1001d282012-10-11 04:11:33 -050081 sprintf(microSec,"%ld%06ld",now.tv_sec-tm->tm_gmtoff,(long int)now.tv_usec);
akmhoqueffacaa82012-09-13 17:48:30 -050082}
akmhoque53f64222012-09-05 13:57:51 -050083
akmhoqueffacaa82012-09-13 17:48:30 -050084
85long int
akmhoqueda5b6832012-09-13 22:33:55 -050086get_time_diff(const char *time1, const char *time2)
akmhoqueffacaa82012-09-13 17:48:30 -050087{
88 long int diff_secs;
89
90 long int time1_in_sec, time2_in_sec;
91
92 char *time1_sec=(char *)malloc(strlen(time1)-6+1);
93 memset(time1_sec,0,strlen(time1)-6+1);
94 memcpy(time1_sec,time1,strlen(time1)-6);
95
96 char *time2_sec=(char *)malloc(strlen(time2)-6+1);
97 memset(time2_sec,0,strlen(time2)-6+1);
98 memcpy(time2_sec,time2,strlen(time2)-6);
99
100 time1_in_sec=strtol(time1_sec,NULL,10);
101 time2_in_sec=strtol(time2_sec,NULL,10);
102
103 diff_secs=time1_in_sec-time2_in_sec;
104
105 free(time1_sec);
106 free(time2_sec);
107
108 return diff_secs;
akmhoque53f64222012-09-05 13:57:51 -0500109}
110
akmhoquebfefef22012-09-26 10:09:34 -0500111
112void
113startLogging(char *loggingDir)
114{
115 struct passwd pd;
116 struct passwd* pwdptr=&pd;
117 struct passwd* tempPwdPtr;
118 char *pwdbuffer;
119 int pwdlinelen = 200;
120 char *logDir;
121 char *logFileName;
122 char *ret;
123 char *logExt;
124 char *defaultLogDir;
125 int status;
126 struct stat st;
127 int isLogDirExists=0;
128 char *time=getLocalTimeStamp();
129
130 pwdbuffer=(char *)malloc(sizeof(char)*200);
131 memset(pwdbuffer,0,200);
132 logDir=(char *)malloc(sizeof(char)*200);
133 memset(logDir,0,200);
134 logFileName=(char *)malloc(sizeof(char)*200);
135 memset(logFileName,0,200);
136 logExt=(char *)malloc(sizeof(char)*5);
137 memset(logExt,0,5);
138 defaultLogDir=(char *)malloc(sizeof(char)*10);
139 memset(defaultLogDir,0,10);
140
141 memcpy(logExt,".log",4);
142 logExt[4]='\0';
143 memcpy(defaultLogDir,"/nlsrLog",9);
144 defaultLogDir[9]='\0';
145
146 if(loggingDir!=NULL)
147 {
148 if( stat( loggingDir, &st)==0)
149 {
150 if ( st.st_mode & S_IFDIR )
151 {
152 if( st.st_mode & S_IWUSR)
153 {
154 isLogDirExists=1;
155 memcpy(logDir,loggingDir,strlen(loggingDir)+1);
156 }
157 else printf("User do not have write permission to %s \n",loggingDir);
158 }
159 else printf("Provided path for %s is not a directory!!\n",loggingDir);
160 }
161 else printf("Log directory: %s does not exists\n",loggingDir);
162 }
163
164 if(isLogDirExists == 0)
165 {
166 if ((getpwuid_r(getuid(),pwdptr,pwdbuffer,pwdlinelen,&tempPwdPtr))!=0)
167 perror("getpwuid_r() error.");
168 else
169 {
170 memcpy(logDir,pd.pw_dir,strlen(pd.pw_dir)+1);
171 memcpy(logDir+strlen(logDir),defaultLogDir,strlen(defaultLogDir)+1);
172 if(stat(logDir,&st) != 0)
173 status = mkdir(logDir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
174 }
175 }
176 memcpy(logFileName,logDir,strlen(logDir)+1);
177 if( logDir[strlen(logDir)-1]!='/')
178 {
179 memcpy(logFileName+strlen(logFileName),"/",1);
180 memcpy(logFileName+strlen(logFileName),"\0",1);
181 }
182 memcpy(logFileName+strlen(logFileName),time,strlen(time)+1);
183 memcpy(logFileName+strlen(logFileName),logExt,strlen(logExt)+1);
184 ret=(char *)malloc(strlen(logFileName)+1);
185 memset(ret,0,strlen(logFileName)+1);
186 memcpy(ret,logFileName,strlen(logFileName)+1);
187
188 setenv("NLSR_LOG_FILE",ret,1);
189
190 free(time);
191 free(logDir);
192 free(logFileName);
193 free(pwdbuffer);
194 free(logExt);
195 free(defaultLogDir);
196 free(ret);
197}
198
199
200void
201writeLogg(const char *source_file, const char *function, const int line, const char *format, ...)
202{
203 char *file=getenv("NLSR_LOG_FILE");
204 if (file != NULL)
205 {
206 FILE *fp = fopen(file, "a");
207
208 if (fp != NULL)
209 {
210 struct timeval t;
211 gettimeofday(&t, NULL);
akmhoque9e9fc722012-09-26 14:03:25 -0500212 fprintf(fp,"%ld.%06u - %s, %s, %d :",(long)t.tv_sec , (unsigned)t.tv_usec , source_file , function , line);
akmhoquebfefef22012-09-26 10:09:34 -0500213 va_list args;
214 va_start(args, format);
215 vfprintf(fp, format, args);
216 fclose(fp);
217 va_end(args);
218 }
219 }
220}
221