c++ FILE * expression:(fh>=0&&(unsigned)fh<(unsigned)_nhandle),父类

img


请问这是怎么回事,

我的子类是

class Data_list:private Logger {
public:
    Data_list();
    Data_list(const char*loggerpath_chrone, int Length, Init_Para* init_para);
    ~Data_list();
void InitMyMarketData();
}

void Data_list::InitMyMarketData() {
    ostringstream s_s;
    s_s.fill('0');
    s_s << " s_insert:";
    TraceInfo(s_s.str());
}
//父类是这样的
class Logger
{
public:
    //默认构造函数
    Logger();
    //构造函数
    Logger(const char * strLogPath, EnumLogLevel nLogLevel = EnumLogLevel::LogLevelNormal);
    //析构函数
    virtual ~Logger();
public:
    friend class Data_list;

    
    
    //写关键信息
    void TraceKeyInfo(const char * strInfo, ...);
    //写错误信息
    void TraceError(const char* strInfo, ...);
    //写警告信息
    void TraceWarning(const char * strInfo, ...);
    //写一般信息
    //void TraceInfo(const char * strInfo, ...);
    void TraceInfo(const string strInfo, ...);
    //设置写日志级别
    void SetLogLevel(EnumLogLevel nLevel);
private:
    //写文件操作
    void Trace(const char * strInfo);
    //void TraceInfo(const string & strInfo, ...);

     
    //获取当前系统时间
    char * GetCurrentTime();
    //创建日志文件名称
    void GenerateLogName();
    //创建日志路径
    void CreateLogPath();
    string  GetCurrentTimeStamp(int time_stamp_type);
private:
    //写日志文件流
    FILE * m_pFileStream;
    //写日志级别
    EnumLogLevel m_nLogLevel;
    //日志的路径
    char m_strLogPath[MAX_STR_LEN];
    //日志的名称
    char m_strCurLogName[MAX_STR_LEN];
    //线程同步的临界区变量
    CRITICAL_SECTION m_cs1;
    //时间戳类型
    int time_stamp_type;
};

//写一般信息
void Logger::TraceInfo(const string strInfo, ...)
{
    //判断当前的写日志级别,若设置只写错误和警告信息则函数返回
    if (m_nLogLevel >= EnumLogLevel::LogLevelMid)
        return;
    if (!strInfo.c_str())
        return;
    char pTemp[MAX_STR_LEN] = { 0 };
    //strcpy(pTemp, GetCurrentTime());
    strcpy(pTemp, GetCurrentTimeStamp(time_stamp_type).c_str());
    strcat(pTemp, INFOPREFIX);
    va_list arg_ptr = NULL;
    va_start(arg_ptr, strInfo);
    vsprintf(pTemp + strlen(pTemp), strInfo.c_str(), arg_ptr);
    va_end(arg_ptr);
    Trace(pTemp);
    arg_ptr = NULL;
}

//写文件操作
void Logger::Trace(const char * strInfo)
{
    if (!strInfo)
        return;
    try
    {
        //进入临界区
        EnterCriticalSection(&m_cs1);
        //若文件流没有打开,则重新打开
        if (!m_pFileStream)
        {
            char temp[1024] = { 0 };
            strcat(temp, m_strLogPath);
            strcat(temp, m_strCurLogName);
            m_pFileStream = fopen(temp, "a+");
            if (!m_pFileStream)
            {
                return;
            }
        }
        //写日志信息到文件流
        fprintf(m_pFileStream, "%s\n", strInfo);//<<<-----------------------------这一行出现了问题
        fflush(m_pFileStream);
        //离开临界区
        LeaveCriticalSection(&m_cs1);
    }
    //若发生异常,则先离开临界区,防止死锁
    catch (...)
    {
        LeaveCriticalSection(&m_cs1);
    }
}

找了半天,不知道问题出在哪,帮忙解决一下,谢谢!

问题解决了。不能直接insert。因为直接insert,那么类Data_list就是函数insert的参数。当insert被调用时,类Data_list的复制构造函数将被调用。而我没有写复制构造函数,因此程序会调用默认的复制构造函数,而默认的复制构造函数并没有初始化FILE*m_pFileStream,因此就出现了程序崩溃的情况。

对照我这款经典土造Mylog,找找差距:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _MSC_VER
    #pragma warning(disable:4996)
    #include <windows.h>
    #include <io.h>
#else
    #include <unistd.h>
    #include <sys/time.h>
    #include <pthread.h>
    #define  CRITICAL_SECTION   pthread_mutex_t
    #define  _vsnprintf         vsnprintf
#endif
//Log{
#define MAXLOGSIZE 20000000
#define MAXLINSIZE 16000
#include <time.h>
#include <sys/timeb.h>
#include <stdarg.h>
char logfilename1[]="MyLog1.log";
char logfilename2[]="MyLog2.log";
static char logstr[MAXLINSIZE+1];
char datestr[16];
char timestr[16];
char mss[4];
CRITICAL_SECTION cs_log;
FILE *flog;
#ifdef _MSC_VER
void Lock(CRITICAL_SECTION *l) {
    EnterCriticalSection(l);
}
void Unlock(CRITICAL_SECTION *l) {
    LeaveCriticalSection(l);
}
#else
void Lock(CRITICAL_SECTION *l) {
    pthread_mutex_lock(l);
}
void Unlock(CRITICAL_SECTION *l) {
    pthread_mutex_unlock(l);
}
#endif
void LogV(const char *pszFmt,va_list argp) {
    struct tm *now;
    struct timeb tb;

    if (NULL==pszFmt||0==pszFmt[0]) return;
    _vsnprintf(logstr,MAXLINSIZE,pszFmt,argp);
    ftime(&tb);
    now=localtime(&tb.time);
    sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday);
    sprintf(timestr,"%02d:%02d:%02d",now->tm_hour     ,now->tm_min  ,now->tm_sec );
    sprintf(mss,"%03d",tb.millitm);
    printf("%s %s.%s %s",datestr,timestr,mss,logstr);
    flog=fopen(logfilename1,"a");
    if (NULL!=flog) {
        fprintf(flog,"%s %s.%s %s",datestr,timestr,mss,logstr);
        if (ftell(flog)>MAXLOGSIZE) {
            fclose(flog);
            if (rename(logfilename1,logfilename2)) {
                remove(logfilename2);
                rename(logfilename1,logfilename2);
            }
        } else {
            fclose(flog);
        }
    }
}
void Log(const char *pszFmt,...) {
    va_list argp;

    Lock(&cs_log);
    va_start(argp,pszFmt);
    LogV(pszFmt,argp);
    va_end(argp);
    Unlock(&cs_log);
}
//Log}
int main(int argc,char * argv[]) {
    int i;
#ifdef _MSC_VER
    InitializeCriticalSection(&cs_log);
#else
    pthread_mutex_init(&cs_log,NULL);
#endif
    for (i=0;i<10000;i++) {
        Log("This is a Log %04d from FILE:%s LINE:%d\n",i, __FILE__, __LINE__);
    }
#ifdef _MSC_VER
    DeleteCriticalSection(&cs_log);
#else
    pthread_mutex_destroy(&cs_log);
#endif
    return 0;
}
//1-79行添加到你带main的.c或.cpp的那个文件的最前面
//82-86行添加到你的main函数开头
//90-94行添加到你的main函数结束前
//在要写LOG的地方仿照第88行的写法写LOG到文件MyLog1.log中