blob: 50cbfdaa819369e8efcfc540210ad6fa725730eb [file] [log] [blame]
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/common/child_process_logging.h"
#import <Foundation/Foundation.h>
#include "base/command_line.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/common/metrics/variations/variations_util.h"
#include "chrome/installer/util/google_update_settings.h"
namespace child_process_logging {
using base::debug::SetCrashKeyValueFuncT;
using base::debug::ClearCrashKeyValueFuncT;
using base::debug::SetCrashKeyValue;
using base::debug::ClearCrashKey;
const char* kGuidParamName = "guid";
const char* kNumberOfViews = "num-views";
const char* kNumExtensionsName = "num-extensions";
const char* kExtensionNameFormat = "extension-%zu";
const char* kPrinterInfoNameFormat = "prn-info-%zu";
// Account for the terminating null character.
static const size_t kClientIdSize = 32 + 1;
static char g_client_id[kClientIdSize];
void SetClientIdImpl(const std::string& client_id,
SetCrashKeyValueFuncT set_key_func) {
set_key_func(kGuidParamName, client_id);
}
void SetClientId(const std::string& client_id) {
std::string str(client_id);
ReplaceSubstringsAfterOffset(&str, 0, "-", "");
base::strlcpy(g_client_id, str.c_str(), kClientIdSize);
SetClientIdImpl(str, SetCrashKeyValue);
std::wstring wstr = ASCIIToWide(str);
GoogleUpdateSettings::SetMetricsId(wstr);
}
std::string GetClientId() {
return std::string(g_client_id);
}
void SetActiveExtensions(const std::set<std::string>& extension_ids) {
// Log the count separately to track heavy users.
const int count = static_cast<int>(extension_ids.size());
SetCrashKeyValue(kNumExtensionsName, base::IntToString(count));
// Record up to |kMaxReportedActiveExtensions| extensions, clearing
// keys if there aren't that many.
std::set<std::string>::const_iterator iter = extension_ids.begin();
for (size_t i = 0; i < kMaxReportedActiveExtensions; ++i) {
std::string key = base::StringPrintf(kExtensionNameFormat, i);
if (iter != extension_ids.end()) {
SetCrashKeyValue(key, *iter);
++iter;
} else {
ClearCrashKey(key);
}
}
}
void SetPrinterInfo(const char* printer_info) {
std::vector<std::string> info;
base::SplitString(printer_info, ';', &info);
info.resize(kMaxReportedPrinterRecords);
for (size_t i = 0; i < info.size(); ++i) {
std::string key = base::StringPrintf(kPrinterInfoNameFormat, i);
if (!info[i].empty()) {
SetCrashKeyValue(key, info[i]);
} else {
ClearCrashKey(key);
}
}
}
void SetNumberOfViewsImpl(int number_of_views,
SetCrashKeyValueFuncT set_key_func) {
std::string value = base::IntToString(number_of_views);
set_key_func(kNumberOfViews, value);
}
void SetNumberOfViews(int number_of_views) {
SetNumberOfViewsImpl(number_of_views, SetCrashKeyValue);
}
void SetCommandLine(const CommandLine* command_line) {
DCHECK(command_line);
if (!command_line)
return;
// These should match the corresponding strings in breakpad_win.cc.
const char* kNumSwitchesKey = "num-switches";
const char* kSwitchKeyFormat = "switch-%zu"; // 1-based.
// Note the total number of switches, not including the exec path.
const CommandLine::StringVector& argv = command_line->argv();
SetCrashKeyValue(kNumSwitchesKey,
base::StringPrintf("%zu", argv.size() - 1));
size_t key_i = 0;
for (size_t i = 1; i < argv.size() && key_i < kMaxSwitches; ++i, ++key_i) {
// TODO(shess): Skip boring switches.
std::string key = base::StringPrintf(kSwitchKeyFormat, key_i + 1);
SetCrashKeyValue(key, argv[i]);
}
// Clear out any stale keys.
for (; key_i < kMaxSwitches; ++key_i) {
std::string key = base::StringPrintf(kSwitchKeyFormat, key_i + 1);
ClearCrashKey(key);
}
}
void SetExperimentList(const std::vector<string16>& experiments) {
// These should match the corresponding strings in breakpad_win.cc.
const char* kNumExperimentsKey = "num-experiments";
const char* kExperimentChunkFormat = "experiment-chunk-%zu"; // 1-based
std::vector<string16> chunks;
chrome_variations::GenerateVariationChunks(experiments, &chunks);
// Store up to |kMaxReportedVariationChunks| chunks.
for (size_t i = 0; i < kMaxReportedVariationChunks; ++i) {
std::string key = base::StringPrintf(kExperimentChunkFormat, i + 1);
if (i < chunks.size()) {
std::string value = UTF16ToUTF8(chunks[i]);
SetCrashKeyValue(key, value);
} else {
ClearCrashKey(key);
}
}
// Make note of the total number of experiments, which may be greater than
// what was able to fit in |kMaxReportedVariationChunks|. This is useful when
// correlating stability with the number of experiments running
// simultaneously.
SetCrashKeyValue(kNumExperimentsKey,
base::StringPrintf("%zu", experiments.size()));
}
void SetChannel(const std::string& channel) {
// This should match the corresponding string in breakpad_win.cc.
const std::string kChannelKey = "channel";
SetCrashKeyValue(kChannelKey, channel);
}
} // namespace child_process_logging