blob: b136c6739481bb744e21ce6e0028cac0495bb5dc [file] [log] [blame]
akmhoque8fdd6412012-12-04 15:05:33 -06001#include<stdio.h>
2#include<string.h>
3#include<stdlib.h>
4#include <unistd.h>
5#include <getopt.h>
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>
12#include <sys/time.h>
13#include <time.h>
14#include <assert.h>
15#ifdef HAVE_CONFIG_H
16#include <config.h>
17#endif
18
19
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
27
28#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
45char * 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
60
61
62long int
63get_current_time_sec(void)
64{
65 struct timeval now;
66 gettimeofday(&now,NULL);
67 return now.tv_sec;
68}
69
70
71void
72get_current_timestamp_micro(char * microSec)
73{
74 struct timeval now;
75 gettimeofday(&now, NULL);
76 sprintf(microSec,"%ld%06ld",now.tv_sec,(long int)now.tv_usec);
77}
78
79
80long int
81get_time_diff(const char *time1, const char *time2)
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;
104}
105
106
107void
108startLogging(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
195void
196writeLogg(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);
207 fprintf(fp,"%ld.%06u - %s, %s, %d :",(long)t.tv_sec , (unsigned)t.tv_usec , source_file , function , line);
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