Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // DEBUG.HPP
- #ifndef debug_hpp
- #define debug_hpp
- #include <fstream>
- #include <stdio.h>
- #include <string>
- #include "application.hpp"
- #ifndef NDEBUG
- #define ON_DEBUG(x) do { } while (0);
- #else
- #define ON_DEBUG(x) do { x } while (0);
- #endif
- enum MessageSeverity
- {
- Information,
- Warning,
- SevereWarning,
- Error,
- SevereError,
- IrrecoverableError
- };
- namespace
- {
- // Application pointer in case application termination is neccessary
- Application* mApplication = nullptr;
- // Stores the cout buffer before switching between file and console output mode
- std::streambuf* mBaseCoutBuffer = nullptr;
- // Stores the console output file
- std::fstream mOutputFile;
- }
- namespace Debug
- {
- // Gives the application pointer for use with debugging utility functions
- void attachDebugger(Application &application);
- // Cleans up debug namespace variables
- void detachDebugger();
- // Utility function that starts console output logging
- void startLoggingToFile();
- // Utility function that ends console output logging
- void stopLoggingToFile();
- // Utility function for logging messages to the console from outside the application class
- void logMessage(std::string message, MessageSeverity severity);
- }
- #endif
- // DEBUG.CPP
- #include "debug.hpp"
- #include <iostream>
- namespace Debug
- {
- void attachDebugger(Application &application)
- {
- mApplication = &application;
- }
- void detachDebugger()
- {
- mApplication = nullptr;
- mBaseCoutBuffer = nullptr;
- }
- void startLoggingToFile()
- {
- mOutputFile.open(__TIMESTAMP__, std::fstream::out | std::fstream::app);
- std::streambuf* tempbuf = mOutputFile.rdbuf();
- mBaseCoutBuffer = std::cout.rdbuf(tempbuf);
- std::cout << '[' << __TIMESTAMP__ << "]\n";
- tempbuf = nullptr;
- }
- void stopLoggingToFile()
- {
- std::cout.rdbuf(mBaseCoutBuffer);
- mOutputFile.close();
- }
- void logMessage(std::string message, MessageSeverity severity)
- {
- assert(mApplication != nullptr && "IRRECOVERABLE ERROR: Application not attached to debugger!");
- switch (severity)
- {
- case Information:
- std::cout << "Information: ";
- break;
- case Warning:
- std::cout << "Warning: ";
- break;
- case SevereWarning:
- std::cout << "SEVERE WARNING: ";
- break;
- case Error:
- std::cout << "Error: ";
- break;
- case SevereError:
- std::cout << "SEVERE ERROR: ";
- break;
- case IrrecoverableError:
- std::cout << "PROGRAM SHOULD BE TERMINATED!!\nIRRECOVERABLE ERROR: ";
- mApplication->stopNextIteration();
- break;
- }
- std::cout << message << '\n';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement