sms

debug_timer

sms
Mar 31st, 2016
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. static const bool use_debug_timer = true;
  2. static quint32 last_id = 0;
  3. static qint64 warning_maximum = 1000;
  4.  
  5. struct DebugTimer {
  6.     DebugTimer(const QString& func_name, bool use_debug_timer, quint32& last_id) {
  7.         if (use_debug_timer) {
  8.             id = last_id + 1;
  9.             last_id = id;
  10.             this->func_name = func_name;
  11.             used = true;
  12.             timer.start();
  13.         }
  14.     }
  15.     void end(qint64 warning_minimum) {
  16.         if (used) {
  17.             qint64 elapsed = timer.elapsed();
  18.             if (elapsed > warning_minimum) {
  19.                 qDebug() << func_name << " [" << id << "] took " << timer.elapsed() << " msecs!";
  20.             }
  21.         }
  22.     }
  23.     QElapsedTimer timer;
  24.     quint32 id;
  25.     QString func_name;
  26.     bool used = false;
  27. };
  28.  
  29. void f() {
  30.     DebugTimer debug_timer(__func__, use_debug_timer, last_id);
  31.     do_stuff();
  32.     debug_timer.end(warning_maximum);
  33. }
  34.  
  35. qint32 b() {
  36.     DebugTimer debug_timer(__func__, use_debug_timer, last_id);
  37.     if (sth1 && sth2 && sth3) {
  38.         debug_timer.end(warning_maximum);
  39.         return 1;
  40.     }
  41.     else if (sth1 && (sth2 || sth3)) {
  42.         debug_timer.end(warning_maximum);
  43.         return 2;
  44.     }
  45.     debug_timer.end(warning_maximum);
  46.     return 3;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment