NLSR Daemonized and Log Writing Enabled
diff --git a/utility.c b/utility.c
index 64e81a8..8326283 100644
--- a/utility.c
+++ b/utility.c
@@ -3,6 +3,12 @@
#include<stdlib.h>
#include <unistd.h>
#include <getopt.h>
+#include<ctype.h>
+#include<stdarg.h>
+#include <sys/types.h>
+#include <pwd.h>
+#include <errno.h>
+#include <sys/stat.h>
#include <sys/time.h>
#include <time.h>
#include <assert.h>
@@ -10,6 +16,7 @@
#include <config.h>
#endif
+
#include <ccn/ccn.h>
#include <ccn/uri.h>
#include <ccn/keystore.h>
@@ -17,6 +24,7 @@
#include <ccn/schedule.h>
#include <ccn/hashtb.h>
+
#include "utility.h"
@@ -95,3 +103,114 @@
return diff_secs;
}
+
+void
+startLogging(char *loggingDir)
+{
+ struct passwd pd;
+ struct passwd* pwdptr=&pd;
+ struct passwd* tempPwdPtr;
+ char *pwdbuffer;
+ int pwdlinelen = 200;
+ char *logDir;
+ char *logFileName;
+ char *ret;
+ char *logExt;
+ char *defaultLogDir;
+ int status;
+ struct stat st;
+ int isLogDirExists=0;
+ char *time=getLocalTimeStamp();
+
+ pwdbuffer=(char *)malloc(sizeof(char)*200);
+ memset(pwdbuffer,0,200);
+ logDir=(char *)malloc(sizeof(char)*200);
+ memset(logDir,0,200);
+ logFileName=(char *)malloc(sizeof(char)*200);
+ memset(logFileName,0,200);
+ logExt=(char *)malloc(sizeof(char)*5);
+ memset(logExt,0,5);
+ defaultLogDir=(char *)malloc(sizeof(char)*10);
+ memset(defaultLogDir,0,10);
+
+ memcpy(logExt,".log",4);
+ logExt[4]='\0';
+ memcpy(defaultLogDir,"/nlsrLog",9);
+ defaultLogDir[9]='\0';
+
+ if(loggingDir!=NULL)
+ {
+ if( stat( loggingDir, &st)==0)
+ {
+ if ( st.st_mode & S_IFDIR )
+ {
+ if( st.st_mode & S_IWUSR)
+ {
+ isLogDirExists=1;
+ memcpy(logDir,loggingDir,strlen(loggingDir)+1);
+ }
+ else printf("User do not have write permission to %s \n",loggingDir);
+ }
+ else printf("Provided path for %s is not a directory!!\n",loggingDir);
+ }
+ else printf("Log directory: %s does not exists\n",loggingDir);
+ }
+
+ if(isLogDirExists == 0)
+ {
+ if ((getpwuid_r(getuid(),pwdptr,pwdbuffer,pwdlinelen,&tempPwdPtr))!=0)
+ perror("getpwuid_r() error.");
+ else
+ {
+ memcpy(logDir,pd.pw_dir,strlen(pd.pw_dir)+1);
+ memcpy(logDir+strlen(logDir),defaultLogDir,strlen(defaultLogDir)+1);
+ if(stat(logDir,&st) != 0)
+ status = mkdir(logDir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
+ }
+ }
+ memcpy(logFileName,logDir,strlen(logDir)+1);
+ if( logDir[strlen(logDir)-1]!='/')
+ {
+ memcpy(logFileName+strlen(logFileName),"/",1);
+ memcpy(logFileName+strlen(logFileName),"\0",1);
+ }
+ memcpy(logFileName+strlen(logFileName),time,strlen(time)+1);
+ memcpy(logFileName+strlen(logFileName),logExt,strlen(logExt)+1);
+ ret=(char *)malloc(strlen(logFileName)+1);
+ memset(ret,0,strlen(logFileName)+1);
+ memcpy(ret,logFileName,strlen(logFileName)+1);
+
+ setenv("NLSR_LOG_FILE",ret,1);
+
+ free(time);
+ free(logDir);
+ free(logFileName);
+ free(pwdbuffer);
+ free(logExt);
+ free(defaultLogDir);
+ free(ret);
+}
+
+
+void
+writeLogg(const char *source_file, const char *function, const int line, const char *format, ...)
+{
+ char *file=getenv("NLSR_LOG_FILE");
+ if (file != NULL)
+ {
+ FILE *fp = fopen(file, "a");
+
+ if (fp != NULL)
+ {
+ struct timeval t;
+ gettimeofday(&t, NULL);
+ fprintf(fp,"%ld.%06u - %s, %s, %d:",(long)t.tv_sec , (unsigned)t.tv_usec , source_file , function , line);
+ va_list args;
+ va_start(args, format);
+ vfprintf(fp, format, args);
+ fclose(fp);
+ va_end(args);
+ }
+ }
+}
+