OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "remoting/base/tracer.h" |
| 6 |
| 7 #include <list> |
| 8 |
| 9 #include "base/basictypes.h" |
| 10 #include "base/condition_variable.h" |
| 11 #include "base/message_loop.h" |
| 12 #include "base/rand_util.h" |
| 13 #include "base/ref_counted.h" |
| 14 #include "base/stl_util-inl.h" |
| 15 #include "base/thread.h" |
| 16 #include "base/time.h" |
| 17 |
| 18 namespace remoting { |
| 19 |
| 20 namespace { |
| 21 |
| 22 class OutputLogger { |
| 23 public: |
| 24 OutputLogger() |
| 25 : thread_("logging_thread"), |
| 26 stopped_(false), |
| 27 wake_(&lock_) { |
| 28 thread_.Start(); |
| 29 thread_.message_loop()->PostTask( |
| 30 FROM_HERE, |
| 31 NewRunnableMethod(this, &OutputLogger::PrintLogs)); |
| 32 } |
| 33 |
| 34 void OutputTrace(TraceBuffer* buffer) { |
| 35 scoped_ptr<TraceBuffer> buffer_ref_(buffer); |
| 36 AutoLock l(lock_); |
| 37 |
| 38 // Drop messages if we're overwhelming the logger. |
| 39 if (buffers_.size() < 10) { |
| 40 buffers_.push_front(buffer_ref_.release()); |
| 41 wake_.Signal(); |
| 42 } else { |
| 43 // TODO(ajwong): Remove this cause it just overwhelms the logging more. |
| 44 LOG(WARNING) << "Message dropped."; |
| 45 } |
| 46 } |
| 47 |
| 48 void LogOneTrace(TraceBuffer* buffer) { |
| 49 LOG(INFO) << "Trace: " << buffer->name(); |
| 50 base::Time last_timestamp; |
| 51 for (int i = 0; i < buffer->record_size(); ++i) { |
| 52 const TraceRecord& record = buffer->record(i); |
| 53 base::Time timestamp = base::Time::FromInternalValue(record.timestamp()); |
| 54 if (i == 0) { |
| 55 LOG(INFO) << " TS: " << record.timestamp() |
| 56 << " msg: " << record.annotation(); |
| 57 } else { |
| 58 base::TimeDelta delta = timestamp - last_timestamp; |
| 59 LOG(INFO) << " TS: " << record.timestamp() |
| 60 << " msg: " << record.annotation() |
| 61 << " [ " << delta.InMilliseconds() << "ms ]"; |
| 62 } |
| 63 last_timestamp = timestamp; |
| 64 } |
| 65 } |
| 66 |
| 67 void PrintLogs() { |
| 68 while(!stopped_) { |
| 69 TraceBuffer* buffer = NULL; |
| 70 { |
| 71 AutoLock l(lock_); |
| 72 if (buffers_.size() == 0) { |
| 73 wake_.Wait(); |
| 74 } |
| 75 // Check again since we might have woken for a stop signal. |
| 76 if (buffers_.size() != 0) { |
| 77 buffer = buffers_.back(); |
| 78 buffers_.pop_back(); |
| 79 } |
| 80 } |
| 81 |
| 82 if (buffer) { |
| 83 LogOneTrace(buffer); |
| 84 delete buffer; |
| 85 } |
| 86 } |
| 87 } |
| 88 |
| 89 private: |
| 90 friend struct DefaultSingletonTraits<OutputLogger>; |
| 91 |
| 92 ~OutputLogger() { |
| 93 { |
| 94 AutoLock l(lock_); |
| 95 stopped_ = true; |
| 96 wake_.Signal(); |
| 97 } |
| 98 |
| 99 thread_.Stop(); |
| 100 STLDeleteElements(&buffers_); |
| 101 } |
| 102 |
| 103 Lock lock_; |
| 104 base::Thread thread_; |
| 105 bool stopped_; |
| 106 ConditionVariable wake_; |
| 107 std::list<TraceBuffer*> buffers_; |
| 108 }; |
| 109 |
| 110 } // namespace |
| 111 |
| 112 Tracer::Tracer(const std::string& name, double sample_percent) { |
| 113 if (sample_percent > base::RandDouble()) { |
| 114 buffer_.reset(new TraceBuffer()); |
| 115 buffer_->set_name(name); |
| 116 } |
| 117 } |
| 118 |
| 119 void Tracer::PrintString(const std::string& s) { |
| 120 AutoLock l(lock_); |
| 121 if (!buffer_.get()) { |
| 122 return; |
| 123 } |
| 124 |
| 125 TraceRecord* record = buffer_->add_record(); |
| 126 record->set_annotation(s); |
| 127 record->set_timestamp(base::Time::Now().ToInternalValue()); |
| 128 |
| 129 // Take the pointer for the current messageloop as identifying for the |
| 130 // current thread. |
| 131 record->set_thread_id(static_cast<uint64>(PlatformThread::CurrentId())); |
| 132 } |
| 133 |
| 134 Tracer::~Tracer() { |
| 135 AutoLock l(lock_); |
| 136 |
| 137 if (buffer_.get()) { |
| 138 Singleton<OutputLogger>::get()->OutputTrace(buffer_.release()); |
| 139 } |
| 140 } |
| 141 |
| 142 } // namespace remoting |
| 143 |
| 144 DISABLE_RUNNABLE_METHOD_REFCOUNT(remoting::OutputLogger); |
OLD | NEW |