公司用的trace记录

时间:2015-04-01 15:42:42
【文件属性】:

文件名称:公司用的trace记录

文件大小:2KB

文件格式:CPP

更新时间:2015-04-01 15:42:42

公司用的trace记录

unsigned int size; std::list< std::string > files; void initialize() { if( _access( "filelist.txt", 06 ) == 0 ) { // 如果文件存在则从文件中读取有效的文件名 std::ifstream file( "filelist.txt" ); std::string str; struct _stat buf; while( std::getline( file, str ) ) { if( !str.empty() ) { files.push_back( str ); if( 0 == _stat( str.c_str(), &buf; ) ) { size += buf.st_size; } } } } else { // 如果文件不存在则以默认的时间作为先后顺序 struct _finddata_t fileinfo; intptr_t hand = _findfirst( "E:\\source\\repos\\trace\\Debug\\*.tlog", &fileinfo; ); if( hand == -1 ) { return; } files.push_back( fileinfo.name ); while( _findnext( hand, &fileinfo; ) == 0 ) { files.push_back( fileinfo.name ); size += fileinfo.size; } _findclose( hand ); } files.sort(); } void DeleteOldTrace() { struct _stat buf; if( 0 == _stat( "cur.tmf", &buf; ) ) { size += buf.st_size; } // 删除最早的文件 std::string str; std::string path = "E:\\source\\repos\\trace\\Debug\\"; std::list< std::string >::iterator it = files.begin(); while( size >= 1*1024 && it != files.end() ) { str = path + it->c_str(); if( 0 == _stat( str.c_str(), &buf; ) ) { size -= buf.st_size; remove( str.c_str() ); it = files.erase( it ); } else { ++it; } } // 重新写入到文件中 str = path + "filelist.txt"; std::ofstream file( str.c_str() ); if( file.is_open() ) { for( it = files.begin(); it != files.end(); ++it ) { file << (*it) << "\n"; } file.close(); } }


网友评论