//------------------------------------------------------------------------- void Log::logIt(int32 target, LogLevel level, const char *msg) { // check whenever the coming message is allowed to be logged if (level > _logLevel) return; // get messages const char *szBuf = msg; // Get time & date string time_t _time = time(NULL); tm* time = localtime(&_time); // create the string before the message char timeDate[255]; std::string sLevel = getLevelStr(level); if (time != NULL) sprintf(timeDate, "%02d:%02d:%02d - %s", time->tm_hour, time->tm_min, time->tm_sec, sLevel.c_str()); else sprintf(timeDate, "%s", sLevel.c_str()); // Check whereever to log if((target & LOG_APP) && _appLog.is_open()){ _appLog << timeDate << ": " << szBuf << "\n"; _appLog.flush(); } if((target & LOG_CLIENT) && _clientLog.is_open()){ _clientLog << timeDate << ": " << szBuf << "\n"; _clientLog.flush(); } if((target & LOG_SERVER) && _serverLog.is_open()){ _serverLog << timeDate << ": " << szBuf << "\n"; _serverLog.flush(); } if((target & LOG_KERNEL) && _kernelLog.is_open()){ _kernelLog << timeDate << ": " << szBuf << "\n"; _kernelLog.flush(); } if((target & LOG_ENGINE) && _engineLog.is_open()){ _engineLog << timeDate << ": " << szBuf << "\n"; _engineLog.flush(); } if((target & LOG_PLUGIN) && _pluginLog.is_open()){ _pluginLog << timeDate << ": " << szBuf << "\n"; _pluginLog.flush(); } if (target & LOG_CONSOLE){ std::cout << timeDate << ": " << szBuf << "\n"; std::cout.flush(); } }
/** * Routine to form and print logger message */ static void logMessage(int iLevel, const char* pszFormat, va_list va) { char pszMsg[SAKHADB_LOG_BUF_SIZE*3]; int n = vsnprintf(pszMsg, sizeof(pszMsg), pszFormat, va); assert(n < sizeof(pszMsg)); struct timeval tm; gettimeofday(&tm, 0); fprintf(stderr, "[%s][%ld.%d] %s\n", getLevelStr(iLevel), tm.tv_sec, tm.tv_usec, pszMsg); }
/*==================================================================== * 函数名 : _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; }
void nata_Log(logLevelT lv, int debugLevel, const char *file, int line, const char *func, const char *fmt, ...) { if (lv != log_Debug || (lv == log_Debug && debugLevel <= dbgLevel)) { va_list args; char dateBuf[32]; char msg[4096]; size_t hdrLen; size_t leftLen; char thdInfoBuf[1024]; uint32_t tId; const char *thdName = NULL; if (doDate == true) { getDateStr(dateBuf, sizeof(dateBuf)); } else { dateBuf[0] = '\0'; } #if defined(NATA_API_POSIX) tId = (unsigned int)pthread_self(); #elif defined(NATA_API_WIN32API) tId = (unsigned int)GetCurrentThreadId(); #else #error Unknown/Non-supported API. #endif // NATA_API_POSIX, NATA_API_WIN32API thdName = Thread::findThreadName(tId); #ifdef NATA_API_POSIX #ifdef NATA_OS_LINUX tId = Thread::getLinuxThreadId(); #endif // NATA_OS_LINUX #endif // NATA_API_POSIX if (isValidString(thdName) == true) { snprintf(thdInfoBuf, sizeof(thdInfoBuf), "[%u:%u:%s]", (unsigned int)getpid(), (unsigned int)tId, thdName); } else { snprintf(thdInfoBuf, sizeof(thdInfoBuf), "[%u:%u]", (unsigned int)getpid(), (unsigned int)tId); } va_start(args, fmt); va_end(args); hdrLen = snprintf(msg, sizeof(msg), "%s%s%s:%s:%d:%s: ", dateBuf, getLevelStr(lv), thdInfoBuf, file, line, func); /* * hdrLen indicates the buffer length WITHOUT '\0'. */ leftLen = sizeof(msg) - hdrLen; if (leftLen > 1) { (void)vsnprintf(msg + hdrLen, leftLen -1, fmt, args); } doLog(lv, msg); } }