Advertisement
Guest User

DEBUG_CLASS

a guest
Jul 2nd, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.18 KB | None | 0 0
  1. #define ADD_DEBUG(world,id,name) \
  2. world->debug->events.push_back(new DebugEvent(id,name));
  3.  
  4. struct DebugEvent : public Shared
  5. {
  6.     int id;
  7.     std::string name;
  8.     std::string dtime;
  9.  
  10.     DebugEvent();
  11.     DebugEvent(int id_,std::string name_) : id(id_), name(name_), dtime(SetTime()) {}
  12.  
  13.     void SetId(int id)
  14.     {
  15.         this->id = id;
  16.     }
  17.  
  18.     void SetName(std::string name)
  19.     {
  20.         this->name = name;
  21.     }
  22.  
  23.     std::string SetTime()
  24.     {
  25.         time_t rawtime;
  26.         struct tm * timeinfo;
  27.         char buffer[80];
  28.  
  29.         time (&rawtime);
  30.         timeinfo = localtime(&rawtime);
  31.  
  32.         strftime(buffer,sizeof(buffer),"%d-%m-%Y %H:%M:%S",timeinfo);
  33.         std::string str(buffer);
  34.         return str;
  35.     }
  36. };
  37.  
  38. struct Debug : public Shared
  39. {
  40.      PtrVector<DebugEvent> events;
  41.      double dump_timer;
  42.      int next_event_id;
  43.  
  44.      Debug()
  45.      {
  46.          this->next_event_id = -1;
  47.          this->dump_timer = 5;
  48.          this->events.clear();
  49.      }
  50.  
  51.      void FlushEvents()
  52.      {
  53.          this->next_event_id = -1;
  54.          this->dump_timer = 5;
  55.          this->events.clear();
  56.      }
  57.  
  58.      void LogEvents(std::string file_name = "Debug.txt")
  59.      {
  60.          bool f_bool = false;
  61.          std::ofstream file;
  62.          file.open(file_name.c_str(),std::ios::out | std::ios::trunc);
  63.  
  64.          if (file.is_open())
  65.          {
  66.             UTIL_PTR_VECTOR_FOREACH(this->events,DebugEvent,event)
  67.             {
  68.                file << event->dtime + ": " << "ID: [" + event->id <<  "] Event Name: [" + event->name+"] \n";
  69.                f_bool = true;
  70.             }
  71.             file.close();
  72.         }
  73.      }
  74. };
  75.  
  76. //Timed action set to 1 second interval
  77. void handle_debug_dump(void *world_void)
  78. {
  79.     World *world = static_cast<World *>(world_void);
  80.  
  81.     if(world->debug->dump_timer > 0)
  82.     {
  83.         --world->debug->dump_timer;
  84.     }
  85.     else
  86.     {
  87.         world->debug->LogEvents("Debug.txt");
  88.         world->debug->FlushEvents();
  89.  
  90.         world->debug->dump_timer = 5;//NeeD CONFIG VALUE
  91.     }
  92. }
  93.  
  94.  
  95. //Usage
  96. void MyClass::Function()
  97. {
  98.     ADD_DEBUG(this->world,this->world->debug->next_event_id,"Function Name")
  99.  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement