IF DEBUG



  • Hey, ich habe eine kleine Log Klasse geschrieben. Ich wollte fragen ob es eine möglichkeit gibt System & Compiler unabhängig herauszufinden, ob der Compiler im Debug | Release Modus ist. Im Debugmodus soll er schreiben und im Release schweigen 🙂

    Hier der Code(freue mich auch über jeden Tipp und Verbesserungsvorschlag ):

    .H:

    #ifndef DEBUGLOG_H_INCLUDED
    #define DEBUGLOG_H_INCLUDED
    
    #include <string>
    #include <fstream>
    #include <stdexcept>
    
    class DebugLog
    {
    public:
        explicit DebugLog(const std::string& filePath);
    
        ~DebugLog();
    
        void entry(const std::string& what);
    
    private:
        std::ofstream logFile;
    };
    
    #endif // DEBUGLOG_H_INCLUDED
    

    .CPP

    #include "debuglog.h"
    
    DebugLog::DebugLog(const std::string& filePath)
        : logFile(filePath.c_str())
    {
        if(!logFile)
            throw std::runtime_error("can`t open log file!");
        else
            logFile << "START" << std::endl << std::endl;
    }
    
    DebugLog::~DebugLog()
    {
        logFile << "END" << std::endl;
    }
    
    void DebugLog::entry(const std::string& what)
    {
        if(!logFile)
            throw std::runtime_error("can`t write in log file!");
        else
            logFile << "\t" << what << std::endl << std::endl;
    }
    


  • Ehm.. na ja, abgesehen davon, dass ich einen ostream nutzen und die Klasse insgesamt völlig anders aufziehen würde.. guck dir mal das NDEBUG Makro an.



  • Wozu überhaupt eine Klasse? :p


Anmelden zum Antworten