关于C++ 在linux下面文件流的问题 求助大牛
现在有一个CLog负责日志的写入工作代码如下
头文件如下
#ifndef __LOG_H__#define __LOG_H__#include<cstdarg>#include<string>#include"nfx.h"#include"type.h"#define CLOG_LEVEL_INFO 1#define CLOG_LEVEL_NOTICE 2#define CLOG_LEVEL_FAIL 4#define CLOG_LEVEL_DEBUG 8#define CLOG_LEVEL_CIRCULAR_BUFFER 16#define CLOG_LEVEL_MUTEX_LOCKER 32#define CLOG_LEVEL_ALL 63extern const U32 DefLogLevel;class CLog{ public: CLog( ); CLog(char * threadName); virtual ~CLog(); void Log(U32 level,const char * title,...); void Logif(U32 level,bool condition,const char * title,...); private: void init( void ); void formatPrint(const char *title,va_list args ); void getThreadName( char* threadName ); private: const U32 m_tid;};#endif#include "nfx.h"//#ifdef LINUX_PLATFORM#include <unistd.h>#include <pthread.h>#include <sys/types.h>#include <dirent.h>#include <sys/stat.h>#include <stdlib.h>//#endif#include <stdio.h>#include <string>#include <fstream>#include <iostream>#include <string.h>#include "Log.h"using namespace std;/******************************************************//**/const U32 DefLogLevel = CLOG_LEVEL_ALL;/******************************************************/const char* LogFileDir = "./Log";void CLog::init( void ){ }CLog::CLog( void ):m_tid(pthread_self( )){ this->init( );}void CLog::getThreadName( char* threadName ){ DIR* dp; struct dirent* entry; struct stat statbuf; if( ( dp = opendir( LogFileDir ) ) == NULL ) { fprintf( stderr,"cannot open directory \n"); return; } //chdir( dir ); while( ( entry = readdir( dp ) )!=NULL ) { lstat( entry->d_name,&statbuf ); if( !S_ISDIR( statbuf.st_mode ) ) { fstream logfile; logfile.open( entry->d_name,ifstream::in ); logfile.seekg(0,fstream::beg); string titlebuf; cout<<"===============filename :"<<entry->d_name<<"====================="<<endl; while(logfile>>titlebuf) { cout<<titlebuf<<endl; } fstream::iostate oldstate = logfile.rdstate( ); logfile.close(); } }}CLog::CLog( char * threadName ):m_tid(pthread_self( )){ this->init( ); getThreadName( threadName ); }CLog::~CLog(){ }void CLog::formatPrint(const char *title,va_list args ){ FILE * filestream = fopen("./Log/DebugLog","a"); fprintf(filestream,"\n<===FILE : %s LINE : %d DATA : %s TIME : %s===>\n",__FILE__,__LINE__,__DATE__,__TIME__); vfprintf(filestream,title,args); fprintf(filestream,"\n"); fclose(filestream);}void CLog::Log(U32 level,const char * title,...){ if(DefLogLevel & level) { va_list arg_ptr; va_start( arg_ptr,title ); this->formatPrint(title, arg_ptr ); va_end( arg_ptr ); }}void CLog::Logif( U32 level,bool condition,const char * title,... ){ if(condition && (DefLogLevel & level)) { va_list arg_ptr; va_start( arg_ptr,title ); this->formatPrint( title,arg_ptr ); va_end( arg_ptr ); }}