CARLA
LifetimeProfiled.cpp
Go to the documentation of this file.
1 // Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma
2 // de Barcelona (UAB).
3 //
4 // This work is licensed under the terms of the MIT license.
5 // For a copy, see <https://opensource.org/licenses/MIT>.
6 
7 #ifndef LIBCARLA_ENABLE_LIFETIME_PROFILER
8 # define LIBCARLA_ENABLE_LIFETIME_PROFILER
9 #endif // LIBCARLA_ENABLE_LIFETIME_PROFILER
10 
11 #include "carla/Debug.h"
12 #include "carla/Logging.h"
14 
15 #include <mutex>
16 #include <unordered_map>
17 
18 namespace carla {
19 namespace profiler {
20 
21  template <typename ... Args>
22  static inline void log(Args && ... args) {
23  logging::write_to_stream(std::cerr, "LIFETIME:", std::forward<Args>(args) ..., '\n');
24  }
25 
27  public:
28 
30  std::lock_guard<std::mutex> lock(_mutex);
31  if (!_objects.empty()) {
32  log("WARNING! the following objects were not destructed.");
33  for (const auto &item : _objects) {
34  log(item.second, "still alive.");
35  }
37  }
38  }
39 
40  void Register(void *object, std::string display_name) {
41 #if LIBCARLA_LOG_LEVEL <= LIBCARLA_LOG_LEVEL_DEBUG
42  log('+', display_name);
43 #endif
44  std::lock_guard<std::mutex> lock(_mutex);
45  _objects.emplace(object, std::move(display_name));
46  }
47 
48  void Deregister(void *object) {
49  std::lock_guard<std::mutex> lock(_mutex);
50  auto it = _objects.find(object);
51  DEBUG_ASSERT(it != _objects.end());
52 #if LIBCARLA_LOG_LEVEL <= LIBCARLA_LOG_LEVEL_DEBUG
53  log('-', it->second);
54 #endif
55  _objects.erase(it);
56  }
57 
58  private:
59 
60  std::mutex _mutex;
61 
62  std::unordered_map<void *, std::string> _objects;
63  };
64 
66 
67  LifetimeProfiled::LifetimeProfiled(std::string display_name) {
68  PROFILER.Register(this, std::move(display_name));
69  }
70 
71  LifetimeProfiled::~LifetimeProfiled() {
72  PROFILER.Deregister(this);
73  }
74 
75 } // namespace profiler
76 } // namespace carla
static void log(Args &&... args)
#define DEBUG_ERROR
Definition: Debug.h:68
void Register(void *object, std::string display_name)
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
#define DEBUG_ASSERT(predicate)
Definition: Debug.h:66
static LIBCARLA_NOINLINE void write_to_stream(std::ostream &out, Arg &&arg, Args &&... args)
Definition: Logging.h:52
static LifetimeProfiler PROFILER
std::unordered_map< void *, std::string > _objects