Advertisement
Petrovi4

log_duration.h

Jun 23rd, 2022 (edited)
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.05 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <chrono>
  4. #include <iostream>
  5.  
  6. #define PROFILE_CONCAT_INTERNAL(X, Y) X##Y
  7. #define PROFILE_CONCAT(X, Y) PROFILE_CONCAT_INTERNAL(X, Y)
  8. #define UNIQUE_VAR_NAME_PROFILE PROFILE_CONCAT(profileGuard, __LINE__)
  9. #define LOG_DURATION(x,y) LogDuration UNIQUE_VAR_NAME_PROFILE(x,y)
  10. #define LOG_DURATION_STREAM(x,y) LOG_DURATION(x,y)
  11.  
  12. class LogDuration {
  13. public:
  14.     // заменим имя типа std::chrono::steady_clock
  15.     // с помощью using для удобства
  16.     using Clock = std::chrono::steady_clock;
  17.  
  18.     LogDuration(const std::string& id, std::ostream& out) :
  19.     id_(id), out_(out) {
  20.     }
  21.  
  22.     ~LogDuration() {
  23.         using namespace std::chrono;
  24.         using namespace std::literals;
  25.  
  26.         const auto end_time = Clock::now();
  27.         const auto dur = end_time - start_time_;
  28.         out_ << id_ << ": "s << duration_cast<milliseconds>(dur).count() << " ms"s << std::endl;
  29.     }
  30.  
  31. private:
  32.     const std::string id_;
  33.     const Clock::time_point start_time_ = Clock::now();
  34.     std::ostream& out_;
  35. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement