| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "remoting/base/tracer.h" | 5 #include "remoting/base/tracer.h" |
| 6 | 6 |
| 7 #include <list> | 7 #include <list> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/condition_variable.h" | 10 #include "base/condition_variable.h" |
| 11 #include "base/lazy_instance.h" |
| 11 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
| 12 #include "base/rand_util.h" | 13 #include "base/rand_util.h" |
| 13 #include "base/ref_counted.h" | 14 #include "base/ref_counted.h" |
| 14 #include "base/stl_util-inl.h" | 15 #include "base/stl_util-inl.h" |
| 15 #include "base/thread.h" | 16 #include "base/thread.h" |
| 16 #include "base/thread_local.h" | 17 #include "base/thread_local.h" |
| 17 #include "base/time.h" | 18 #include "base/time.h" |
| 18 | 19 |
| 19 namespace remoting { | 20 namespace remoting { |
| 20 | 21 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 } | 82 } |
| 82 | 83 |
| 83 if (buffer) { | 84 if (buffer) { |
| 84 LogOneTrace(buffer); | 85 LogOneTrace(buffer); |
| 85 delete buffer; | 86 delete buffer; |
| 86 } | 87 } |
| 87 } | 88 } |
| 88 } | 89 } |
| 89 | 90 |
| 90 private: | 91 private: |
| 91 friend struct DefaultSingletonTraits<OutputLogger>; | 92 friend struct base::DefaultLazyInstanceTraits<OutputLogger>; |
| 92 | 93 |
| 93 ~OutputLogger() { | 94 ~OutputLogger() { |
| 94 { | 95 { |
| 95 AutoLock l(lock_); | 96 AutoLock l(lock_); |
| 96 stopped_ = true; | 97 stopped_ = true; |
| 97 wake_.Signal(); | 98 wake_.Signal(); |
| 98 } | 99 } |
| 99 | 100 |
| 100 thread_.Stop(); | 101 thread_.Stop(); |
| 101 STLDeleteElements(&buffers_); | 102 STLDeleteElements(&buffers_); |
| 102 } | 103 } |
| 103 | 104 |
| 104 Lock lock_; | 105 Lock lock_; |
| 105 base::Thread thread_; | 106 base::Thread thread_; |
| 106 bool stopped_; | 107 bool stopped_; |
| 107 ConditionVariable wake_; | 108 ConditionVariable wake_; |
| 108 std::list<TraceBuffer*> buffers_; | 109 std::list<TraceBuffer*> buffers_; |
| 109 }; | 110 }; |
| 110 | 111 |
| 112 static base::LazyInstance<OutputLogger> g_output_logger( |
| 113 base::LINKER_INITIALIZED); |
| 114 static base::LazyInstance<base::ThreadLocalPointer<TraceContext> > |
| 115 g_thread_local_trace_context(base::LINKER_INITIALIZED); |
| 116 |
| 111 } // namespace | 117 } // namespace |
| 112 | 118 |
| 113 Tracer::Tracer(const std::string& name, double sample_percent) { | 119 Tracer::Tracer(const std::string& name, double sample_percent) { |
| 114 if (sample_percent > base::RandDouble()) { | 120 if (sample_percent > base::RandDouble()) { |
| 115 buffer_.reset(new TraceBuffer()); | 121 buffer_.reset(new TraceBuffer()); |
| 116 buffer_->set_name(name); | 122 buffer_->set_name(name); |
| 117 } | 123 } |
| 118 } | 124 } |
| 119 | 125 |
| 120 void Tracer::PrintString(const std::string& s) { | 126 void Tracer::PrintString(const std::string& s) { |
| 121 AutoLock l(lock_); | 127 AutoLock l(lock_); |
| 122 if (!buffer_.get()) { | 128 if (!buffer_.get()) { |
| 123 return; | 129 return; |
| 124 } | 130 } |
| 125 | 131 |
| 126 TraceRecord* record = buffer_->add_record(); | 132 TraceRecord* record = buffer_->add_record(); |
| 127 record->set_annotation(s); | 133 record->set_annotation(s); |
| 128 record->set_timestamp(base::Time::Now().ToInternalValue()); | 134 record->set_timestamp(base::Time::Now().ToInternalValue()); |
| 129 | 135 |
| 130 // Take the pointer for the current messageloop as identifying for the | 136 // Take the pointer for the current messageloop as identifying for the |
| 131 // current thread. | 137 // current thread. |
| 132 record->set_thread_id(static_cast<uint64>(PlatformThread::CurrentId())); | 138 record->set_thread_id(static_cast<uint64>(PlatformThread::CurrentId())); |
| 133 } | 139 } |
| 134 | 140 |
| 135 Tracer::~Tracer() { | 141 Tracer::~Tracer() { |
| 136 AutoLock l(lock_); | 142 AutoLock l(lock_); |
| 137 | 143 |
| 138 if (buffer_.get()) { | 144 if (buffer_.get()) { |
| 139 Singleton<OutputLogger>::get()->OutputTrace(buffer_.release()); | 145 g_output_logger.Get().OutputTrace(buffer_.release()); |
| 140 } | 146 } |
| 141 } | 147 } |
| 142 | 148 |
| 143 // static | 149 // static |
| 144 Tracer* TraceContext::tracer() { | 150 Tracer* TraceContext::tracer() { |
| 145 return Get()->GetTracerInternal(); | 151 return Get()->GetTracerInternal(); |
| 146 } | 152 } |
| 147 | 153 |
| 148 // static | 154 // static |
| 149 void TraceContext::PushTracer(Tracer* tracer) { | 155 void TraceContext::PushTracer(Tracer* tracer) { |
| 150 Get()->PushTracerInternal(tracer); | 156 Get()->PushTracerInternal(tracer); |
| 151 } | 157 } |
| 152 | 158 |
| 153 // static | 159 // static |
| 154 void TraceContext::PopTracer() { | 160 void TraceContext::PopTracer() { |
| 155 Get()->PopTracerInternal(); | 161 Get()->PopTracerInternal(); |
| 156 } | 162 } |
| 157 | 163 |
| 158 // static | 164 // static |
| 159 TraceContext* TraceContext::Get() { | 165 TraceContext* TraceContext::Get() { |
| 160 TraceContext* context = | 166 TraceContext* context = |
| 161 Singleton<base::ThreadLocalPointer<TraceContext> >::get()->Get(); | 167 g_thread_local_trace_context.Get().Get(); |
| 162 if (context == NULL) { | 168 if (context == NULL) { |
| 163 context = new TraceContext(); | 169 context = new TraceContext(); |
| 164 context->PushTracerInternal(new Tracer("default", 0.0)); | 170 context->PushTracerInternal(new Tracer("default", 0.0)); |
| 165 Singleton<base::ThreadLocalPointer<TraceContext> >::get()->Set(context); | 171 g_thread_local_trace_context.Get().Set(context); |
| 166 } | 172 } |
| 167 return context; | 173 return context; |
| 168 } | 174 } |
| 169 | 175 |
| 170 TraceContext::TraceContext() {} | 176 TraceContext::TraceContext() {} |
| 171 | 177 |
| 172 TraceContext::~TraceContext() {} | 178 TraceContext::~TraceContext() {} |
| 173 | 179 |
| 174 void TraceContext::PushTracerInternal(Tracer* tracer) { | 180 void TraceContext::PushTracerInternal(Tracer* tracer) { |
| 175 tracers_.push_back(make_scoped_refptr(tracer)); | 181 tracers_.push_back(make_scoped_refptr(tracer)); |
| 176 } | 182 } |
| 177 | 183 |
| 178 void TraceContext::PopTracerInternal() { tracers_.pop_back(); } | 184 void TraceContext::PopTracerInternal() { tracers_.pop_back(); } |
| 179 | 185 |
| 180 Tracer* TraceContext::GetTracerInternal() { return tracers_.back(); } | 186 Tracer* TraceContext::GetTracerInternal() { return tracers_.back(); } |
| 181 | 187 |
| 182 | 188 |
| 183 } // namespace remoting | 189 } // namespace remoting |
| 184 | 190 |
| 185 DISABLE_RUNNABLE_METHOD_REFCOUNT(remoting::OutputLogger); | 191 DISABLE_RUNNABLE_METHOD_REFCOUNT(remoting::OutputLogger); |
| OLD | NEW |