OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 10 matching lines...) Expand all Loading... |
21 namespace { | 21 namespace { |
22 | 22 |
23 // Class whose instances logs various method calls (constructor, destructor, | 23 // Class whose instances logs various method calls (constructor, destructor, |
24 // etc.). Each instance has a unique ID (a simple global sequence number) and | 24 // etc.). Each instance has a unique ID (a simple global sequence number) and |
25 // an origin ID. When a copy is made, the new object gets a fresh ID but copies | 25 // an origin ID. When a copy is made, the new object gets a fresh ID but copies |
26 // the origin ID from the original. When a new Logger is created from scratch, | 26 // the origin ID from the original. When a new Logger is created from scratch, |
27 // it gets a fresh ID, and the origin ID is the same as the ID (default | 27 // it gets a fresh ID, and the origin ID is the same as the ID (default |
28 // constructor) or given as an argument (explicit constructor). | 28 // constructor) or given as an argument (explicit constructor). |
29 class Logger { | 29 class Logger { |
30 public: | 30 public: |
31 Logger() : id_(next_id_++), origin_(id_) { Log("default constructor"); } | 31 Logger() : id_(g_next_id++), origin_(id_) { Log("default constructor"); } |
32 explicit Logger(int origin) : id_(next_id_++), origin_(origin) { | 32 explicit Logger(int origin) : id_(g_next_id++), origin_(origin) { |
33 Log("explicit constructor"); | 33 Log("explicit constructor"); |
34 } | 34 } |
35 Logger(const Logger& other) : id_(next_id_++), origin_(other.origin_) { | 35 Logger(const Logger& other) : id_(g_next_id++), origin_(other.origin_) { |
36 LogFrom("copy constructor", other); | 36 LogFrom("copy constructor", other); |
37 } | 37 } |
38 Logger(Logger&& other) : id_(next_id_++), origin_(other.origin_) { | 38 Logger(Logger&& other) : id_(g_next_id++), origin_(other.origin_) { |
39 LogFrom("move constructor", other); | 39 LogFrom("move constructor", other); |
40 } | 40 } |
41 ~Logger() { Log("destructor"); } | 41 ~Logger() { Log("destructor"); } |
42 Logger& operator=(const Logger& other) { | 42 Logger& operator=(const Logger& other) { |
43 origin_ = other.origin_; | 43 origin_ = other.origin_; |
44 LogFrom("operator= copy", other); | 44 LogFrom("operator= copy", other); |
45 return *this; | 45 return *this; |
46 } | 46 } |
47 Logger& operator=(Logger&& other) { | 47 Logger& operator=(Logger&& other) { |
48 origin_ = other.origin_; | 48 origin_ = other.origin_; |
(...skipping 10 matching lines...) Expand all Loading... |
59 return a.origin_ == b.origin_; | 59 return a.origin_ == b.origin_; |
60 } | 60 } |
61 friend bool operator!=(const Logger& a, const Logger& b) { | 61 friend bool operator!=(const Logger& a, const Logger& b) { |
62 Log2("operator!=", a, b); | 62 Log2("operator!=", a, b); |
63 return a.origin_ != b.origin_; | 63 return a.origin_ != b.origin_; |
64 } | 64 } |
65 void Foo() { Log("Foo()"); } | 65 void Foo() { Log("Foo()"); } |
66 void Foo() const { Log("Foo() const"); } | 66 void Foo() const { Log("Foo() const"); } |
67 static rtc::scoped_ptr<std::vector<std::string>> Setup() { | 67 static rtc::scoped_ptr<std::vector<std::string>> Setup() { |
68 auto s = rtc_make_scoped_ptr(new std::vector<std::string>); | 68 auto s = rtc_make_scoped_ptr(new std::vector<std::string>); |
69 Logger::log_ = s.get(); | 69 g_log = s.get(); |
70 Logger::next_id_ = 0; | 70 g_next_id = 0; |
71 return s; | 71 return s; |
72 } | 72 } |
73 | 73 |
74 private: | 74 private: |
75 int id_; | 75 int id_; |
76 int origin_; | 76 int origin_; |
77 static std::vector<std::string>* log_; | 77 static std::vector<std::string>* g_log; |
78 static int next_id_; | 78 static int g_next_id; |
79 void Log(const char* msg) const { | 79 void Log(const char* msg) const { |
80 std::ostringstream oss; | 80 std::ostringstream oss; |
81 oss << id_ << ':' << origin_ << ". " << msg; | 81 oss << id_ << ':' << origin_ << ". " << msg; |
82 log_->push_back(oss.str()); | 82 g_log->push_back(oss.str()); |
83 } | 83 } |
84 void LogFrom(const char* msg, const Logger& other) const { | 84 void LogFrom(const char* msg, const Logger& other) const { |
85 std::ostringstream oss; | 85 std::ostringstream oss; |
86 oss << id_ << ':' << origin_ << ". " << msg << " (from " << other.id_ << ':' | 86 oss << id_ << ':' << origin_ << ". " << msg << " (from " << other.id_ << ':' |
87 << other.origin_ << ")"; | 87 << other.origin_ << ")"; |
88 log_->push_back(oss.str()); | 88 g_log->push_back(oss.str()); |
89 } | 89 } |
90 static void Log2(const char* msg, const Logger& a, const Logger& b) { | 90 static void Log2(const char* msg, const Logger& a, const Logger& b) { |
91 std::ostringstream oss; | 91 std::ostringstream oss; |
92 oss << msg << ' ' << a.id_ << ':' << a.origin_ << ", " << b.id_ << ':' | 92 oss << msg << ' ' << a.id_ << ':' << a.origin_ << ", " << b.id_ << ':' |
93 << b.origin_; | 93 << b.origin_; |
94 log_->push_back(oss.str()); | 94 g_log->push_back(oss.str()); |
95 } | 95 } |
96 }; | 96 }; |
97 | 97 |
98 std::vector<std::string>* Logger::log_ = nullptr; | 98 std::vector<std::string>* Logger::g_log = nullptr; |
99 int Logger::next_id_ = 0; | 99 int Logger::g_next_id = 0; |
100 | 100 |
101 // Append all the other args to the vector pointed to by the first arg. | 101 // Append all the other args to the vector pointed to by the first arg. |
102 template <typename T> | 102 template <typename T> |
103 void VectorAppend(std::vector<T>* v) {} | 103 void VectorAppend(std::vector<T>* v) {} |
104 template <typename T, typename... Ts> | 104 template <typename T, typename... Ts> |
105 void VectorAppend(std::vector<T>* v, const T& e, Ts... es) { | 105 void VectorAppend(std::vector<T>* v, const T& e, Ts... es) { |
106 v->push_back(e); | 106 v->push_back(e); |
107 VectorAppend(v, es...); | 107 VectorAppend(v, es...); |
108 } | 108 } |
109 | 109 |
(...skipping 370 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
480 "4:17. copy constructor (from 0:17)", "5:5. default constructor", | 480 "4:17. copy constructor (from 0:17)", "5:5. default constructor", |
481 "6:6. default constructor", "7:7. default constructor", "---", | 481 "6:6. default constructor", "7:7. default constructor", "---", |
482 "swap 2:42, 3:17", "swap 4:5, 5:17", "swap 6:7, 7:6", "---", | 482 "swap 2:42, 3:17", "swap 4:5, 5:17", "swap 6:7, 7:6", "---", |
483 "7:6. destructor", "6:7. destructor", "5:17. destructor", | 483 "7:6. destructor", "6:7. destructor", "5:17. destructor", |
484 "4:5. destructor", "3:17. destructor", "2:42. destructor", | 484 "4:5. destructor", "3:17. destructor", "2:42. destructor", |
485 "1:42. destructor", "0:17. destructor"), | 485 "1:42. destructor", "0:17. destructor"), |
486 *log); | 486 *log); |
487 } | 487 } |
488 | 488 |
489 } // namespace rtc | 489 } // namespace rtc |
OLD | NEW |