void CTonyXiaoLog::MakeFileName(void) //创造一个新文件名 { char szTemp[LOG_ITEM_LENGTH_MAX]; //临时缓冲区 MakeATimeString(szTemp, LOG_ITEM_LENGTH_MAX); //获得时间戳字符串 FixFileInfo(); //维护文件总个数不超标(默认72 个) int nLen = SafePrintf( //注意看这句,利用构造函数中的种子名字 m_szFileName, //加上时间戳,后面再加上“.log”后缀 FILENAME_STRING_LENGTH * 2, //生成日志文件名 "%s_%s.log", m_szFilePath, szTemp); nLen++; //习惯,长度+1,保留最后’\0’的位置 //将新的文件名添加到队列 int nAddLastRet = m_pFileInfoQueue->AddLast(m_szFileName, nLen); if (0 >= nAddLastRet) { //这是一个特殊的防护,如果队列满了(内存不够用),删除最开始三个文件名 //释放内存空间,这是预防服务器业务太繁忙,导致内存不够用,队列无法添加的 //规避措施,这也体现非关键模块为关键业务模块让路的思维 DeleteFirstFile(); DeleteFirstFile(); DeleteFirstFile(); //删除三个之后,重新尝试添加 nAddLastRet = m_pFileInfoQueue->AddLast(m_szFileName, nLen); //如果此时添加仍然失败,投降,日志发生一点错乱没有关系。 } m_nFileSize = 0; //新文件创建,文件长度为0 //下面逻辑,新创建一个文件,在文件头先打印一点文件名相关信息,帮助以后的跟踪查找 time (&m_tFileNameMake); { //由于这是非业务打印,因此不希望输出到屏幕,这里临时将屏幕开关关闭 bool bPrint2Scr = m_bPrintf2ScrFlag; m_bPrintf2ScrFlag = false; _Printf("Tony.Xiao. base libeary log file %s\n", m_szFileName); _Printf("-----------------------------------------------\n"); m_bPrintf2ScrFlag = bPrint2Scr; //输出完毕,屏幕开关恢复原值 } }
void CNEOLog::MakeFileName(void) { char szTemp[LOG_ITEM_LENGTH_MAX] = "\0"; MakeATimeString(szTemp,LOG_ITEM_LENGTH_MAX); //获得时间戳字符串 FixFileInfo(); //维护文件总个数(默认72个) int nLen=SafePrintf(m_szFileName,(FILENAME_STRING_LENGTH*2),"%s_%s.log",m_szFilePath,szTemp);//生成日志文件名 nLen++; //将新的文件名添加到队列 int nAddLastRet=m_pFileInfoQueue->AddLast(m_szFileName,nLen); if(0>=nAddLastRet) { //为防止队列满了,删除对头的三个名字 DeleteFirstFile(); DeleteFirstFile(); DeleteFirstFile(); //重新添加 nAddLastRet=m_pFileInfoQueue->AddLast(m_szFileName,nLen); } m_nFileSize=0; //新建文件,长度为0 //新建文件,在文件头打印一些信息 time(&m_tFileNameMake); { bool bPrintToScr=m_bPrintfToScrFlag; m_bPrintfToScrFlag=false; _Printf("NEO.base lib log file %s\n",m_szFileName); _Printf("____________________________________\n"); m_bPrintfToScrFlag=bPrintToScr;//输出完毕,恢复标志 } }
void ReadLine_ShowNormalPrompt ( ReadLiner * rl ) { //_ReadLine_ShowStringWithCursor ( rl, ( byte* ) "", rl->NormalPrompt ) ; _ReadLine_PrintfClearTerminalLine ( ) ; _Printf ( "\r%s", rl->NormalPrompt ) ; rl->EndPosition = 0 ; rl->InputLine [ 0 ] = 0 ; }
int kprintf(const char *f, ...) { va_list ap; int ret; va_start(ap, f); ret = _Printf(knserputs, NULL, f, ap); va_end(ap); return ret; }
int (sprintf)(char *s, const char *fmt, ...) { /* print formatted to string */ int ans; va_list ap; va_start(ap, fmt); ans = _Printf(&prout, s, fmt, ap); if (0 <= ans) s[ans] = '\0'; va_end(ap); return (ans); }
void _ReadLine_MoveInputStartToLineStart ( int32 fromPosition ) { // nb. this is *necessary* when user scrolls up with scrollbar in eg. konsole and then hits up/down arrow int32 n, columns = GetTerminalWidth ( ) ; if ( fromPosition && columns ) { n = ( fromPosition ) / ( columns ) ; if ( ( fromPosition % columns ) < 2 ) n -- ; // nb : ?? -- i don't understand this but it works if ( n ) Cursor_Up ( n ) ;//_Printf ( "\r%c[%dA", ESC, n ) ; // move n lines up } else _Printf ( "\r" ) ; // nb -- a workaround : ?? second sequence ( clear 2 eol ) not necessary but seems to reset things to work -- ?? //_Printf ( "\r%c[2K", ESC ) ; // nb -- a workaround : ?? second sequence ( clear 2 eol ) not necessary but seems to reset things to work -- ?? }
void _Mem_ChunkFree ( MemChunk * mchunk ) { int32 size = mchunk->S_ChunkSize ; if ( _Q_ ) { _Q_->MmapMemoryAllocated -= size ; if ( ( _Q_->Verbosity > 2 ) && ( size > 10000000 ) ) { Symbol * sym = (Symbol *) ( mchunk + 1 ) ; _Printf ( "\nFree : %s : 0x%lld : %d, ", (int) ( sym->S_Name ) > 0x80000000 ? (char*) sym->S_Name : "(null)", mchunk->S_AType, mchunk->S_ChunkSize ) ; } } DLNode_Remove ( (DLNode*) mchunk ) ; munmap ( mchunk, size ) ; }
void _MemChunk_Account ( MemChunk * mchunk, int32 flag ) { if ( _Q_ ) { if ( flag ) _Q_->Mmap_TotalMemoryAllocated += mchunk->S_ChunkSize ; else _Q_->Mmap_TotalMemoryAllocated -= mchunk->S_ChunkSize ; #if 0 if ( ( _Q_->Verbosity > 2 ) && ( mchunk->S_ChunkSize >= 10 * M ) ) { Symbol * sym = ( Symbol * ) ( mchunk + 1 ) ; _Printf ( ( byte* ) "\n%s : %s : 0x%lld : %d, ", flag ? "Alloc" : "Free", ( int ) ( sym->S_Name ) > 0x80000000 ? ( char* ) sym->S_Name : "(null)", mchunk->S_AProperty, mchunk->S_ChunkSize ) ; } #endif } }
int32 _MemList_GetCurrentMemAllocated ( DLList * list, int32 flag ) { DLNode * node, *nodeNext ; int32 memAllocated = 0 ; if ( flag ) Printf ( c_dd ( "\nformat :: Type Name or Chunk Pointer : Type : Size, ...\n" ) ) ; for ( node = DLList_First ( list ) ; node ; node = nodeNext ) { MemChunk * mchunk = (MemChunk*) node ; nodeNext = DLNode_Next ( node ) ; if ( mchunk->S_ChunkSize ) { memAllocated += mchunk->S_ChunkSize ; if ( flag ) _Printf ( "0x%08x : 0x%08llx : %d, ", (uint) mchunk, mchunk->S_AType, mchunk->S_ChunkSize ) ; } } return memAllocated ; }
void _MemChunk_CheckAndInit ( MemChunk * mchunk, int32 size, uint64 type ) { if ( ( mchunk == (MemChunk*) MAP_FAILED ) ) { perror ( "mmap" ) ; OpenVmTil_Throw ( ( byte* ) "Memory Allocation Error", FULL_RESTART ) ; } //Mem_Clear ( ( byte* ) mchunk, size ) ; //?!? not necessary MAP_ANONYMOUS comes cleared mchunk->S_AType = type ; mchunk->S_ChunkSize = size ; // S_ChunkSize is the total size of the chunk already including any prepended booking structure in that total if ( _Q_ ) { _Q_->MmapMemoryAllocated += size ; // added here only for ovt allocation if ( ( _Q_->Verbosity > 2 ) && ( size > 10000000 ) ) { _Printf ( "\nAllocate : %s : 0x%lld : %d, ", ( (NamedByteArray*) ( mchunk->S_pb_Data ) )->Name, mchunk->S_AType, mchunk->S_ChunkSize ) ; } } mchunk->S_Chunk = (byte*) ( mchunk + 1 ) ; // nb. ptr arithmetic }
int CTonyXiaoLog::_XGDebug3(char *szFormat, ...) { //这段比较经典,变参函数处理模块,不再赘述 char szBuf[LOG_ITEM_LENGTH_MAX]; int nMaxLength = LOG_ITEM_LENGTH_MAX; int nListCount = 0; va_list pArgList; va_start(pArgList, szFormat); nListCount += Linux_Win_vsnprintf(szBuf + nListCount, nMaxLength - nListCount, szFormat, pArgList); va_end(pArgList); if (nListCount > (nMaxLength - 1)) nListCount = nMaxLength - 1; *(szBuf + nListCount) = '\0'; if (m_bDebug3Flag) //如果开关打开 { m_Lock.Lock(); //加锁 { _Printf("%s", szBuf); //真实执行打印; } m_Lock.Unlock(); //解锁 } return nListCount; //返回长度 }
int CNEOLog::_XGDebug3(const char *szFormat,...) { char szBuf[LOG_ITEM_LENGTH_MAX] = "\0"; int nMaxLength=LOG_ITEM_LENGTH_MAX; int nListCount=0; va_list pArgList; va_start(pArgList,szFormat); nListCount+=WIN_LINUX_vsnprintf(szBuf+nListCount,nMaxLength-nListCount,szFormat,pArgList); va_end(pArgList); if(nListCount>(nMaxLength-1)) nListCount=nMaxLength-1; *(szBuf+nListCount)='\0'; if(m_bDebug3Flag) { m_Lock.Lock(); { _Printf("%s",szBuf); } m_Lock.UnLock(); } return nListCount; }
void _ReadLine_Show ( ReadLiner * rl, byte * prompt ) { _Printf ( "\r%s%s", prompt, rl->InputLine ) ; }
void _ReadLine_PrintfClearTerminalLine ( ) { _Printf ( "\r%c[J", ESC ) ; // clear from cursor to end of screen -- important if we have (mistakenly) gone up an extra line }