/* main ===================================================================== */ int main (int argc, char **argv) { vLogInit (LOG_UPTO (TEMPLATE_LOG_LEVEL)); // Read arguments from the command line setting = gxPLSettingFromCommandArgs (argc, argv, gxPLConnectViaHub); if (setting == NULL) { PERROR ("Unable to get settings from command line"); exit (EXIT_FAILURE); } vLogSetMask (LOG_UPTO (setting->log)); // Now we detach (daemonize ourself) if (setting->nodaemon == 0) { vLogDaemonize (true); // Fork ourselves switch (fork()) { case 0: // child // Close standard I/O and become our own process group close (fileno (stdin)); close (fileno (stdout)); close (fileno (stderr)); setpgrp(); // Start the daemon and keep it running prvDaemon (setting); break; default: // parent break; case -1: // error PERROR ("unable to spawn daemon supervisor, %s (%d)", strerror (errno), errno); exit (EXIT_FAILURE); } } else { // When running non-detached, start the main task vMain (setting); } return 0; }
/*==================================================================== * 函数名 : _vWriteLog * 功能 : 将日志信息写入文件中 * 算法实现 : 打开文件,按一定格式写入日志信息 * 参数说明 : vspFileName 产生日志信息的文件名 * vLine 代码行数 * vspFileName 函数名 * vLevel 日志级别,以EL_开头的宏定义 * vspLogName 写入日志的文件名 * vspFmt 格式 * 返回值说明: 无 * ---------------------------------------------------------------------- * 修改记录: * 日 期 版本 修改人 走读人 修改记录 * 2014/12/20 v1.0 YLI YLI * ====================================================================*/ void _vWriteLog(char *vspFileName,int vLine,const char *vspFunName, int vLevel,char *vspLogName,char *vspFmt,...) { va_list strAp; char vspLevelStr[10]; char sDateTime[20]={0}; char logPath[L_LOG_PATH_LEN_MAX]; FILE *fp = NULL; GetCurrentTime(sDateTime); getLevelStr(vLevel,vspLevelStr); if(strlen(vspLevelStr) == 0) { printf("error:log level error!\n"); return; } if(_gsInitFlag != F_LOG_INIT) { if(vLogInit() == LOGFAIL) { printf("error:vlogInit error!!!!\n"); return; } } if(CheckDirectory(_gstrLogCfg.logDir) == LOGFAIL) { printf("error:log path error!!!!\n"); return; } if(strlen(_gstrLogCfg.logDir) > L_LOG_PATH_LEN_MAX) { printf("error:log path name too long!!!!\n"); return; } sprintf(logPath,"%s/%s",_gstrLogCfg.logDir,vspLogName); CheckFileStat(logPath,_gstrLogCfg.logFileMaxSize); fp = fopen(logPath,"a+"); if(fp == NULL) { printf("error:[%s] [%d] log file open fail!!! [%d]\n",vspFileName,vLine,errno); return; } va_start(strAp,vspFmt); if (vLine == 0) { fprintf(fp,"[%s] ",sDateTime); } else { fprintf(fp,"[%s] [%u] [%s] [%d] [%s] %s:", sDateTime,getpid(), vspFileName,vLine, vspFunName,vspLevelStr); } vfprintf(fp,vspFmt,strAp); fprintf(fp,"\n"); va_end(strAp); fflush(fp); fclose(fp); fp = NULL; return; }