| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "chrome/common/metrics_helpers.h" | 5 #include "chrome/common/metrics_helpers.h" |
| 6 | 6 |
| 7 #if defined(USE_SYSTEM_LIBBZ2) | 7 #if defined(USE_SYSTEM_LIBBZ2) |
| 8 #include <bzlib.h> | 8 #include <bzlib.h> |
| 9 #else | 9 #else |
| 10 #include "third_party/bzip2/bzlib.h" | 10 #include "third_party/bzip2/bzlib.h" |
| 11 #endif | 11 #endif |
| 12 | 12 |
| 13 #include "base/base64.h" | 13 #include "base/base64.h" |
| 14 #include "base/time.h" | 14 #include "base/time.h" |
| 15 #include "base/basictypes.h" | 15 #include "base/basictypes.h" |
| 16 #include "base/file_util.h" | 16 #include "base/file_util.h" |
| 17 #include "base/md5.h" | 17 #include "base/md5.h" |
| 18 #include "base/perftimer.h" |
| 18 #include "base/scoped_ptr.h" | 19 #include "base/scoped_ptr.h" |
| 19 #include "base/string_util.h" | 20 #include "base/string_util.h" |
| 20 #include "base/sys_info.h" | 21 #include "base/sys_info.h" |
| 21 #include "base/utf_string_conversions.h" | 22 #include "base/utf_string_conversions.h" |
| 22 #include "base/third_party/nspr/prtime.h" | 23 #include "base/third_party/nspr/prtime.h" |
| 23 #include "chrome/common/logging_chrome.h" | 24 #include "chrome/common/logging_chrome.h" |
| 24 #include "googleurl/src/gurl.h" | 25 #include "googleurl/src/gurl.h" |
| 25 | 26 |
| 26 #define OPEN_ELEMENT_FOR_SCOPE(name) ScopedElement scoped_element(this, name) | 27 #define OPEN_ELEMENT_FOR_SCOPE(name) ScopedElement scoped_element(this, name) |
| 27 | 28 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 40 inline const unsigned char* UnsignedChar(const char* input) { | 41 inline const unsigned char* UnsignedChar(const char* input) { |
| 41 return reinterpret_cast<const unsigned char*>(input); | 42 return reinterpret_cast<const unsigned char*>(input); |
| 42 } | 43 } |
| 43 | 44 |
| 44 MetricsLogBase::MetricsLogBase(const std::string& client_id, int session_id, | 45 MetricsLogBase::MetricsLogBase(const std::string& client_id, int session_id, |
| 45 const std::string& version_string) | 46 const std::string& version_string) |
| 46 : start_time_(Time::Now()), | 47 : start_time_(Time::Now()), |
| 47 client_id_(client_id), | 48 client_id_(client_id), |
| 48 session_id_(IntToString(session_id)), | 49 session_id_(IntToString(session_id)), |
| 49 locked_(false), | 50 locked_(false), |
| 51 doc_(NULL), |
| 50 buffer_(NULL), | 52 buffer_(NULL), |
| 51 writer_(NULL), | 53 writer_(NULL), |
| 52 num_events_(0) { | 54 num_events_(0) { |
| 53 | 55 |
| 54 buffer_ = xmlBufferCreate(); | 56 buffer_ = xmlBufferCreate(); |
| 55 DCHECK(buffer_); | 57 DCHECK(buffer_); |
| 56 | 58 |
| 57 writer_ = xmlNewTextWriterMemory(buffer_, 0); | 59 #if defined(OS_CHROMEOS) |
| 60 writer_ = xmlNewTextWriterDoc(&doc_, /* compression */ 0); |
| 61 #else |
| 62 writer_ = xmlNewTextWriterMemory(buffer_, /* compression */ 0); |
| 63 #endif // OS_CHROMEOS |
| 58 DCHECK(writer_); | 64 DCHECK(writer_); |
| 59 | 65 |
| 60 int result = xmlTextWriterSetIndent(writer_, 2); | 66 int result = xmlTextWriterSetIndent(writer_, 2); |
| 61 DCHECK_EQ(0, result); | 67 DCHECK_EQ(0, result); |
| 62 | 68 |
| 63 StartElement("log"); | 69 StartElement("log"); |
| 64 WriteAttribute("clientid", client_id_); | 70 WriteAttribute("clientid", client_id_); |
| 65 WriteInt64Attribute("buildtime", GetBuildTime()); | 71 WriteInt64Attribute("buildtime", GetBuildTime()); |
| 66 WriteAttribute("appversion", version_string); | 72 WriteAttribute("appversion", version_string); |
| 67 | 73 |
| 68 DCHECK_GE(result, 0); | 74 DCHECK_GE(result, 0); |
| 69 } | 75 } |
| 70 | 76 |
| 71 MetricsLogBase::~MetricsLogBase() { | 77 MetricsLogBase::~MetricsLogBase() { |
| 78 FreeDocWriter(); |
| 79 |
| 72 if (writer_) | 80 if (writer_) |
| 73 xmlFreeTextWriter(writer_); | 81 xmlFreeTextWriter(writer_); |
| 74 | 82 |
| 75 if (buffer_) | 83 if (buffer_) { |
| 76 xmlBufferFree(buffer_); | 84 xmlBufferFree(buffer_); |
| 85 buffer_ = NULL; |
| 86 } |
| 77 } | 87 } |
| 78 | 88 |
| 79 void MetricsLogBase::CloseLog() { | 89 void MetricsLogBase::CloseLog() { |
| 80 DCHECK(!locked_); | 90 DCHECK(!locked_); |
| 81 locked_ = true; | 91 locked_ = true; |
| 82 | 92 |
| 83 int result = xmlTextWriterEndDocument(writer_); | 93 int result = xmlTextWriterEndDocument(writer_); |
| 84 DCHECK_GE(result, 0); | 94 DCHECK_GE(result, 0); |
| 85 | 95 |
| 86 result = xmlTextWriterFlush(writer_); | 96 result = xmlTextWriterFlush(writer_); |
| 87 DCHECK_GE(result, 0); | 97 DCHECK_GE(result, 0); |
| 98 |
| 99 #if defined(OS_CHROMEOS) |
| 100 xmlNodePtr root = xmlDocGetRootElement(doc_); |
| 101 if (!hardware_class_.empty()) { |
| 102 // The hardware class is determined after the first ongoing log is |
| 103 // constructed, so this adds the root element's "hardwareclass" |
| 104 // attribute when the log is closed instead. |
| 105 xmlNewProp(root, UnsignedChar("hardwareclass"), |
| 106 UnsignedChar(hardware_class_.c_str())); |
| 107 } |
| 108 |
| 109 // Flattens the XML tree into a character buffer. |
| 110 PerfTimer dump_timer; |
| 111 result = xmlNodeDump(buffer_, doc_, root, /* level */ 0, /* format */ 1); |
| 112 DCHECK_GE(result, 0); |
| 113 UMA_HISTOGRAM_TIMES("UMA.XMLNodeDumpTime", dump_timer.Elapsed()); |
| 114 |
| 115 PerfTimer free_timer; |
| 116 FreeDocWriter(); |
| 117 UMA_HISTOGRAM_TIMES("UMA.XMLWriterDestructionTime", free_timer.Elapsed()); |
| 118 #endif // OS_CHROMEOS |
| 88 } | 119 } |
| 89 | 120 |
| 90 int MetricsLogBase::GetEncodedLogSize() { | 121 int MetricsLogBase::GetEncodedLogSize() { |
| 91 DCHECK(locked_); | 122 DCHECK(locked_); |
| 92 return buffer_->use; | 123 return buffer_->use; |
| 93 } | 124 } |
| 94 | 125 |
| 95 bool MetricsLogBase::GetEncodedLog(char* buffer, int buffer_size) { | 126 bool MetricsLogBase::GetEncodedLog(char* buffer, int buffer_size) { |
| 96 DCHECK(locked_); | 127 DCHECK(locked_); |
| 97 if (buffer_size < GetEncodedLogSize()) | 128 if (buffer_size < GetEncodedLogSize()) |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 279 case WINDOW_CLOSE: return "close"; | 310 case WINDOW_CLOSE: return "close"; |
| 280 case WINDOW_DESTROY: return "destroy"; | 311 case WINDOW_DESTROY: return "destroy"; |
| 281 | 312 |
| 282 default: | 313 default: |
| 283 NOTREACHED(); | 314 NOTREACHED(); |
| 284 return "unknown"; // Can't return NULL as this is used in a required | 315 return "unknown"; // Can't return NULL as this is used in a required |
| 285 // attribute. | 316 // attribute. |
| 286 } | 317 } |
| 287 } | 318 } |
| 288 | 319 |
| 320 void MetricsLogBase::FreeDocWriter() { |
| 321 if (writer_) { |
| 322 xmlFreeTextWriter(writer_); |
| 323 writer_ = NULL; |
| 324 } |
| 325 |
| 326 if (doc_) { |
| 327 xmlFreeDoc(doc_); |
| 328 doc_ = NULL; |
| 329 } |
| 330 } |
| 331 |
| 289 void MetricsLogBase::StartElement(const char* name) { | 332 void MetricsLogBase::StartElement(const char* name) { |
| 290 DCHECK(!locked_); | 333 DCHECK(!locked_); |
| 291 DCHECK(name); | 334 DCHECK(name); |
| 292 | 335 |
| 293 int result = xmlTextWriterStartElement(writer_, UnsignedChar(name)); | 336 int result = xmlTextWriterStartElement(writer_, UnsignedChar(name)); |
| 294 DCHECK_GE(result, 0); | 337 DCHECK_GE(result, 0); |
| 295 } | 338 } |
| 296 | 339 |
| 297 void MetricsLogBase::EndElement() { | 340 void MetricsLogBase::EndElement() { |
| 298 DCHECK(!locked_); | 341 DCHECK(!locked_); |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 448 } | 491 } |
| 449 | 492 |
| 450 void MetricsServiceBase::DiscardPendingLog() { | 493 void MetricsServiceBase::DiscardPendingLog() { |
| 451 if (pending_log_) { // Shutdown might have deleted it! | 494 if (pending_log_) { // Shutdown might have deleted it! |
| 452 delete pending_log_; | 495 delete pending_log_; |
| 453 pending_log_ = NULL; | 496 pending_log_ = NULL; |
| 454 } | 497 } |
| 455 pending_log_text_.clear(); | 498 pending_log_text_.clear(); |
| 456 } | 499 } |
| 457 | 500 |
| OLD | NEW |