Advertisement
Ardente

Debug Namespace

Jul 15th, 2020
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.13 KB | None | 0 0
  1. // DEBUG.HPP
  2.  
  3. #ifndef debug_hpp
  4. #define debug_hpp
  5.  
  6. #include <fstream>
  7. #include <stdio.h>
  8. #include <string>
  9.  
  10. #include "application.hpp"
  11.  
  12. #ifndef NDEBUG
  13. #define ON_DEBUG(x) do { } while (0);
  14. #else
  15. #define ON_DEBUG(x) do { x } while (0);
  16. #endif
  17.  
  18. enum MessageSeverity
  19. {
  20.     Information,
  21.     Warning,
  22.     SevereWarning,
  23.     Error,
  24.     SevereError,
  25.     IrrecoverableError
  26. };
  27.  
  28. namespace
  29. {
  30.     // Application pointer in case application termination is neccessary
  31.     Application* mApplication = nullptr;
  32.  
  33.     // Stores the cout buffer before switching between file and console output mode
  34.     std::streambuf* mBaseCoutBuffer = nullptr;
  35.    
  36.     // Stores the console output file
  37.     std::fstream mOutputFile;
  38. }
  39.  
  40. namespace Debug
  41. {
  42.     // Gives the application pointer for use with debugging utility functions
  43.     void attachDebugger(Application &application);
  44.  
  45.     // Cleans up debug namespace variables
  46.     void detachDebugger();
  47.  
  48.     // Utility function that starts console output logging
  49.     void startLoggingToFile();
  50.    
  51.     // Utility function that ends console output logging
  52.     void stopLoggingToFile();
  53.    
  54.     // Utility function for logging messages to the console from outside the application class
  55.     void logMessage(std::string message, MessageSeverity severity);
  56. }
  57.  
  58. #endif
  59.  
  60. // DEBUG.CPP
  61.  
  62. #include "debug.hpp"
  63.  
  64. #include <iostream>
  65.  
  66. namespace Debug
  67. {
  68.     void attachDebugger(Application &application)
  69.     {
  70.         mApplication = &application;
  71.     }
  72.  
  73.     void detachDebugger()
  74.     {
  75.         mApplication = nullptr;
  76.         mBaseCoutBuffer = nullptr;
  77.     }
  78.  
  79.     void startLoggingToFile()
  80.     {
  81.         mOutputFile.open(__TIMESTAMP__, std::fstream::out | std::fstream::app);
  82.        
  83.         std::streambuf* tempbuf = mOutputFile.rdbuf();
  84.         mBaseCoutBuffer = std::cout.rdbuf(tempbuf);
  85.        
  86.         std::cout << '[' <<  __TIMESTAMP__ << "]\n";
  87.        
  88.         tempbuf = nullptr;
  89.     }
  90.  
  91.     void stopLoggingToFile()
  92.     {
  93.         std::cout.rdbuf(mBaseCoutBuffer);
  94.        
  95.         mOutputFile.close();
  96.     }
  97.  
  98.     void logMessage(std::string message, MessageSeverity severity)
  99.     {
  100.         assert(mApplication != nullptr && "IRRECOVERABLE ERROR: Application not attached to debugger!");
  101.        
  102.         switch (severity)
  103.         {
  104.             case Information:
  105.                 std::cout << "Information: ";
  106.                 break;
  107.                
  108.             case Warning:
  109.                 std::cout << "Warning: ";
  110.                 break;
  111.                
  112.             case SevereWarning:
  113.                 std::cout << "SEVERE WARNING: ";
  114.                 break;
  115.                
  116.             case Error:
  117.                 std::cout << "Error: ";
  118.                 break;
  119.                
  120.             case SevereError:
  121.                 std::cout << "SEVERE ERROR: ";
  122.                 break;
  123.                
  124.             case IrrecoverableError:
  125.                 std::cout << "PROGRAM SHOULD BE TERMINATED!!\nIRRECOVERABLE ERROR: ";
  126.                 mApplication->stopNextIteration();
  127.                 break;
  128.         }
  129.        
  130.         std::cout << message << '\n';
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement