Add ICU message format support
[chromium-blink-merge.git] / chromecast / crash / linux / dump_info.cc
blob6dca890fb0e6275cb2d79c06a371fbca7a15bcae
1 // Copyright 2015 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 #include "chromecast/crash/linux/dump_info.h"
6 #include <stdlib.h>
8 #include "base/logging.h"
9 #include "base/values.h"
11 namespace chromecast {
13 namespace {
15 const char kDumpTimeFormat[] = "%Y-%m-%d %H:%M:%S";
16 const unsigned kDumpTimeMaxLen = 255;
18 const int kNumRequiredParams = 5;
20 const char kNameKey[] = "name";
21 const char kDumpTimeKey[] = "dump_time";
22 const char kDumpKey[] = "dump";
23 const char kUptimeKey[] = "uptime";
24 const char kLogfileKey[] = "logfile";
25 const char kSuffixKey[] = "suffix";
26 const char kPrevAppNameKey[] = "prev_app_name";
27 const char kCurAppNameKey[] = "cur_app_name";
28 const char kLastAppNameKey[] = "last_app_name";
29 const char kReleaseVersionKey[] = "release_version";
30 const char kBuildNumberKey[] = "build_number";
32 } // namespace
34 DumpInfo::DumpInfo(const base::Value* entry) : valid_(ParseEntry(entry)) {
37 DumpInfo::DumpInfo(const std::string& crashed_process_dump,
38 const std::string& logfile,
39 const time_t& dump_time,
40 const MinidumpParams& params)
41 : crashed_process_dump_(crashed_process_dump),
42 logfile_(logfile),
43 dump_time_(dump_time),
44 params_(params),
45 valid_(false) {
47 // Validate the time passed in.
48 struct tm* tm = gmtime(&dump_time);
49 char buf[kDumpTimeMaxLen];
50 int n = strftime(buf, kDumpTimeMaxLen, kDumpTimeFormat, tm);
51 if (n <= 0) {
52 LOG(INFO) << "strftime failed";
53 return;
56 valid_ = true;
59 DumpInfo::~DumpInfo() {
62 scoped_ptr<base::Value> DumpInfo::GetAsValue() const {
63 scoped_ptr<base::Value> result = make_scoped_ptr(new base::DictionaryValue());
64 base::DictionaryValue* entry;
65 result->GetAsDictionary(&entry);
66 entry->SetString(kNameKey, params_.process_name);
68 struct tm* tm = gmtime(&dump_time_);
69 char buf[kDumpTimeMaxLen];
70 int n = strftime(buf, kDumpTimeMaxLen, kDumpTimeFormat, tm);
71 DCHECK_GT(n, 0);
72 std::string dump_time(buf);
73 entry->SetString(kDumpTimeKey, dump_time);
75 entry->SetString(kDumpKey, crashed_process_dump_);
76 std::string uptime = std::to_string(params_.process_uptime);
77 entry->SetString(kUptimeKey, uptime);
78 entry->SetString(kLogfileKey, logfile_);
79 entry->SetString(kSuffixKey, params_.suffix);
80 entry->SetString(kPrevAppNameKey, params_.previous_app_name);
81 entry->SetString(kCurAppNameKey, params_.current_app_name);
82 entry->SetString(kLastAppNameKey, params_.last_app_name);
83 entry->SetString(kReleaseVersionKey, params_.cast_release_version);
84 entry->SetString(kBuildNumberKey, params_.cast_build_number);
86 return result;
89 bool DumpInfo::ParseEntry(const base::Value* entry) {
90 valid_ = false;
92 if (!entry) {
93 return false;
96 const base::DictionaryValue* dict;
97 if (!entry->GetAsDictionary(&dict)) {
98 return false;
101 // Extract required fields.
102 if (!dict->GetString(kNameKey, &params_.process_name)) {
103 return false;
106 std::string dump_time;
107 if (!dict->GetString(kDumpTimeKey, &dump_time)) {
108 return false;
110 if (!SetDumpTimeFromString(dump_time)) {
111 return false;
114 if (!dict->GetString(kDumpKey, &crashed_process_dump_)) {
115 return false;
118 std::string uptime;
119 if (!dict->GetString(kUptimeKey, &uptime)) {
120 return false;
122 errno = 0;
123 params_.process_uptime = strtoull(uptime.c_str(), nullptr, 0);
124 if (errno != 0) {
125 return false;
128 if (!dict->GetString(kLogfileKey, &logfile_)) {
129 return false;
131 size_t num_params = kNumRequiredParams;
133 // Extract all other optional fields.
134 if (dict->GetString(kSuffixKey, &params_.suffix)) {
135 ++num_params;
137 if (dict->GetString(kPrevAppNameKey, &params_.previous_app_name)) {
138 ++num_params;
140 if (dict->GetString(kCurAppNameKey, &params_.current_app_name)) {
141 ++num_params;
143 if (dict->GetString(kLastAppNameKey, &params_.last_app_name)) {
144 ++num_params;
146 if (dict->GetString(kReleaseVersionKey, &params_.cast_release_version)) {
147 ++num_params;
149 if (dict->GetString(kBuildNumberKey, &params_.cast_build_number)) {
150 ++num_params;
153 // Disallow extraneous params
154 if (dict->size() != num_params) {
155 return false;
158 valid_ = true;
159 return true;
162 bool DumpInfo::SetDumpTimeFromString(const std::string& timestr) {
163 struct tm tm = {0};
164 char* text = strptime(timestr.c_str(), kDumpTimeFormat, &tm);
165 dump_time_ = mktime(&tm);
166 if (!text || dump_time_ < 0) {
167 LOG(INFO) << "Failed to convert dump time invalid";
168 return false;
170 return true;
173 } // namespace chromecast