[Solved] Logging in Datei im Release Mode mit C (io.h)



  • Hallo,

    ich benutze eine Logging-Lib (rlog), die in einer Klasse mittels write() (io.h) Log-Meldungen in eine Datei schreibt.

    Die Logging-Datei erzeuge ich in einer anderen Klasse so:

    class App
    {
        App::App()
        {
            [...]
            #ifdef RLOG
            time_t curTime;
            time(&curTime);
            struct tm* ptm = localtime(&curTime);
            char buf[25];
            sprintf(buf, "Log_%02d%02d%4d%02d%02d%02d.txt", ptm->tm_mday,
                (ptm->tm_mon)+1, (ptm->tm_year) + 1900, ptm->tm_hour,
                ptm->tm_min, ptm->tm_sec);
            mLogFile = std::fopen(buf, "w+");
            if (mLogFile == NULL) {
                qDebug() << "LogFile could not be created! Exiting...";
                exit(1);
            }
            mLogNode = new StdioNode(_fileno(mLogFile));
            [...]
    
        }
    
        [...]
    
        App::~App()
        {
        #ifdef RLOG
            fclose(mLogFile);
            delete mLogFile;
        #endif   
        }
    }
    

    Das eigentliche Logging passiert in einer Methode von StdioNode:

    [...]
    
    void StdioNode::publish(const RLogData& data)
    {
        char timeStamp[32];
        time_t errTime = data.time;
        tm currentTime;
    
    #ifdef HAVE_LOCALTIME_R
        localtime_r( &errTime, &currentTime );
    #else
        currentTime = *localtime( &errTime );
    #endif
    
        const char *color = NULL;
        if(colorize)
        {
    	sprintf(timeStamp, "%s%02i:%02i:%02i%s ",
    		kGreenColor,
    		currentTime.tm_hour,
    		currentTime.tm_min,
    		currentTime.tm_sec,
    		kNormalColor);
    
    	string channel = data.publisher->channel->name();
    	LogLevel level = data.publisher->channel->logLevel();
    
    	switch(level)
    	{
    	case Log_Critical:
    	case Log_Error:
    	    color = kRedColor;
    	    break;
    	case Log_Warning:
    	    color = kYellowColor;
    	    break;
    	case Log_Notice:
    	case Log_Info:
    	case Log_Debug:
    	case Log_Undef:
    	    break;
    	}
        } else
        {
    	sprintf(timeStamp, "%02i:%02i:%02i ",
    		currentTime.tm_hour,
    		currentTime.tm_min,
    		currentTime.tm_sec);
        }
    
    #ifdef USE_STRSTREAM
        ostrstream ss;
    #else
        ostringstream ss;
    #endif
    
        ss << timeStamp;
        if (outputChannel) {
            ss << '[' << data.publisher->channel->name() << "] ";
        }
        if (outputContext) {
        ss << "(" << data.publisher->fileName << ':'
    	<< data.publisher->lineNum << ") ";
        }
    #ifndef _WIN32
        if (outputThreadId) {
            char tid[16] = "";
            snprintf(tid,15,"%lu",pthread_self());
            ss << "[tid:" << tid << "] ";
        }
    #endif
    
        if(color)
    	ss << color;
    
        ss << data.msg;
    
        if(color)
    	ss << kNormalColor;
    
        ss << '\n';
    
        string out = ss.str();
        write( fdOut, out.c_str(), out.length() );
    }
    

    Im Debug-Modus fkt. auch alles, die Log-Meldungen werden in die angelegte Datei geschrieben. Im Release-Modus aber wrid die Datei nur angelegt und hat keinen Inhalt, d.h es wird nichts geschrieben.

    Ich vermute mal, dass es an meinen Build-Einstellungen liegt. Ich benutze die Standard-Debug und Release-Einstellungen von Visual Studio 2010.
    Oder hat es was mit Buffering im Release Modus zu tun?

    Ist ein Qt-Projekt, und in der Projektdatei definiere ich das Makro, wenn Logging verwendet werden soll:

    TEMPLATE = app
    DEFINES += RLOG
    
    CONFIG(debug, debug|release) {
        TARGET = App
        win32 {
            CONFIG += console
            [...]
            contains(DEFINES, RLOG) {
                LIBS += ../../ThirdParty/rlog-1.4/win32/Debug/rlogd.lib
            }
        }
        [...]
    } else {
        win32 {
            contains(DEFINES, RLOG) {
                LIBS += ../../ThirdParty/rlog-1.4/win32/Release/rlog.lib
            }
        }
    }
    [...]
    

    Danke für alle Hinweise

    Edit: Ok, war mein Fehler, hat sich noch ein Makro versteckt... 🙂


Anmelden zum Antworten