STDMETHODIMP CLogFactory::Create(LONG FileType, BSTR AppName, BSTR Configue, BSTR Section, BSTR* LogName, BSTR* XFile) { //MessageBox(NULL, _T("CLogFactory::Create"), _T("CLogFactory::Create"), MB_OK); ATLASSERT(NULL != AppName); ATLASSERT(NULL != Configue); ATLASSERT(NULL != Section); if (NULL == AppName || NULL == Configue || NULL == Section || NULL == LogName) { CString strError = _T("AppName(or Configue? Section? LogName?) is NULL. CLogFactory::Create return E_FAIL."); LOGGER_WRITE(strError.GetBuffer()); LogEvent(strError); return E_FAIL; } CString strLogName; if (!CreateLogName(AppName, Configue, Section, FileType, strLogName)) return E_FAIL; int iSerIndex = -1; if (!FindObject(strLogName, iSerIndex)) { if (!CreateObject(strLogName, FileType, AppName, Configue, Section, iSerIndex)) return E_FAIL; } ATLASSERT(-1 != iSerIndex); LOGSERVER &LogServer = m_sLogServer[iSerIndex]; LogServer.oClientName.Add(CString(AppName)); LONG Count = LockServer(); LOGGER_WRITE(_T("增加客户端, 服务计数:") << Count); *LogName = strLogName.AllocSysString(); TCHAR Buffer[MAX_PATH] = { 0 }; GetModuleFileName(GetModuleHandle(NULL), Buffer, MAX_PATH - 1); CString ModuleFile(Buffer); *XFile = ModuleFile.AllocSysString(); return S_OK; }
BOOL RKLOG_API RKLogVB(int lvl, LPCTSTR msg) { static bool s_firstCall = true; static char szLvl[] = {'T', 'W', 'E', 'F', 'I'}; if (s_firstCall) { s_firstCall = false; RKLClearLog(); } if (lvl < 0 || lvl > 4) { return FALSE; } if (!CreateFolder(g_szLogPath)) { return FALSE; } BOOL ret; char fname[MAX_PATH]; struct tm ltm; FILE *fp; WaitForSingleObject(g_hmtx, 500); fp = fopen(CreateLogName(fname, <m), "a+"); if (fp) { fprintf(fp, "%c %02d:%02d:%02d <%s> %s\n", szLvl[lvl], ltm.tm_hour, ltm.tm_min, ltm.tm_sec, g_szLogModule, msg); fclose(fp); ret = TRUE; } else { ret = FALSE; } ReleaseMutex(g_hmtx); return ret; }
void Log::ToFile(const std::stringstream &logstream) { // Make sure we have the log file this->filename = Config ? CreateLogName(Config->LogFile, starttime) : ""; Flux::string LogColor = Config ? Config->LogColor : "\033[0m"; // Log to terminal if((type != LOG_SILENT || type != LOG_CRITICAL) && InTerm()) std::cout << (nocolor ? NoTermColor(logstream.str()) : logstream.str()) << std::endl; if(type == LOG_CRITICAL && InTerm()) // Log to stderr instead of stdout std::cerr << (nocolor ? NoTermColor(logstream.str()) : logstream.str()) << std::endl; if(this->filename.empty()) { std::cerr << "\033[22;31m" << TimeStamp() << " [CRITICAL] Cannot open log file!" << LogColor << std::endl; return; // Exit if there's no file to log to } try { CheckLogDelete(this); log.open(this->filename.c_str(), std::fstream::out | std::fstream::app); if(!log.is_open()) throw LogException(Config->LogFile.empty() ? "Cannot open Log File." : Flux::string("Failed to open Log File " + this->filename + ": " + strerror(errno)).c_str()); log << NoTermColor(logstream.str()) << std::endl; if(log.is_open()) log.close(); } catch(LogException &e) { if(InTerm()) std::cerr << "Log Exception Caught: " << e.GetReason() << std::endl; } }
CAppLog::CAppLog() { first = true; CreateLogName(); }
/* Allows printf()-like interface to file descriptors without the * complications that arise from mixing stdio and low level calls * FIXME: Needs date and time before logfile entries. */ int lprintf( log_t *log, unsigned int level, char *fmt, ... ) { int rc = -1; int cnt = 0; int linecnt = 0; //char date[32] = {0}; //char threadnum[10] = {0}; char line[LOGLINE_MAX] = {0}; struct tm tTemp = {0}; struct timeval tvTemp = {0}; va_list ap; static const char *levels[6] = { "[TRACE] ", "[DEBUG] ", "[INFO] ", "[WARN] ", "[ERROR] ", "[FATAL] "}; if(!log) return -1; /* //If this is debug info, and we're not logging it, return if( !(log->flags&LOG_DEBUG) && level == DEBUG ) return 0; // Prepare the date string if( !(log->flags&LOG_NODATE) ) { now=time(NULL); strcpy(date,ctime(&now)); date[strlen(date)-6]=' '; date[strlen(date)-5]='\0'; } // thread num if( !(log->flags&LOG_NOTID) ) { sprintf(threadnum, "(%lu) ", pthread_self()); } //Format layout cnt = snprintf(line, sizeof(line), "%s%s%s", log->flags&LOG_NODATE ? "" : date, log->flags&LOG_NOLVL ? "" : (level > FATAL ? levels[0] : levels[level]), log->flags&LOG_NOTID ? "" : threadnum); */ /* format datetime + level */ gettimeofday(&tvTemp, NULL); localtime_r(&tvTemp.tv_sec, &tTemp); if (log->flags&LOG_TID) { cnt = sprintf(line, "[%04d-%02d-%02d %02d:%02d:%02d.%03ld][%lu]%s", tTemp.tm_year + 1900, tTemp.tm_mon + 1, tTemp.tm_mday, tTemp.tm_hour, tTemp.tm_min, tTemp.tm_sec, tvTemp.tv_usec/1000, pthread_self(), levels[level]); } else { cnt = sprintf(line, "[%04d-%02d-%02d %02d:%02d:%02d.%03ld]%s", tTemp.tm_year + 1900, tTemp.tm_mon + 1, tTemp.tm_mday, tTemp.tm_hour, tTemp.tm_min, tTemp.tm_sec, tvTemp.tv_usec/1000, levels[level]); } va_start(ap, fmt); linecnt = vsnprintf(line+cnt, sizeof(line)-cnt, fmt, ap); /*如果输入的日志过长会自动截取*/ va_end(ap); //line[sizeof(line)-1] = '\0'; /* if( !(log->flags&LOG_NOLF) ) { //chomp(line); //strcpy(line+strlen(line), "\n"); } */ /* check date or size */ if (log->flags&LOG_DAILY) { sem_wait(&log->sem); if (log->cur_day != tTemp.tm_mday) { if (CreateLogName(log) < 0) return -1; log->cur_day = tTemp.tm_mday; } rc = write(log->fd, line, linecnt + cnt); sem_post(&log->sem); } else { sem_wait(&log->sem); log->cur_size = log->cur_size + linecnt + cnt; if (log->cur_size > log->max_size) { if (CreateLogName(log) < 0) return -1; log->cur_size = linecnt + cnt; } rc = write(log->fd, line, linecnt + cnt); sem_post(&log->sem); } if( !rc ) errno = 0; return rc; }