Update V8 to version 4.6.29.
[chromium-blink-merge.git] / chromecast / crash / linux / dump_info.cc
blobc98ddf948dd99ccd9a1660e9015a03a49af6f39e
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 <sstream>
10 #include "base/logging.h"
11 #include "base/strings/string_split.h"
13 namespace chromecast {
15 namespace {
17 const char kDumpTimeFormat[] = "%Y-%m-%d %H:%M:%S";
18 const unsigned kDumpTimeMaxLen = 255;
20 const int kNumRequiredParams = 5;
21 } // namespace
23 DumpInfo::DumpInfo(const std::string& entry) : valid_(false) {
24 // TODO(slan): This ctor is doing non-trivial work. Change this.
25 if ((valid_ = ParseEntry(entry)) == true) {
26 entry_ = GetEntryAsString();
30 DumpInfo::DumpInfo(const std::string& crashed_process_dump,
31 const std::string& logfile,
32 const time_t& dump_time,
33 const MinidumpParams& params)
34 : crashed_process_dump_(crashed_process_dump),
35 logfile_(logfile),
36 dump_time_(dump_time),
37 params_(params),
38 valid_(false) {
39 // The format is
40 // <name>|<dump time>|<dump>|<uptime>|<logfile>|<suffix>|<prev_app_name>|
41 // <curent_app name>|<last_app_name>
42 // <dump time> is in the format of kDumpTimeFormat
44 // Validate the time passed in.
45 struct tm* tm = gmtime(&dump_time);
46 char buf[kDumpTimeMaxLen];
47 int n = strftime(buf, kDumpTimeMaxLen, kDumpTimeFormat, tm);
48 if (n <= 0) {
49 LOG(INFO) << "strftime failed";
50 return;
52 entry_ = GetEntryAsString();
53 valid_ = true;
56 DumpInfo::~DumpInfo() {
59 std::string DumpInfo::GetEntryAsString() {
60 struct tm* tm = gmtime(&dump_time_);
61 char buf[kDumpTimeMaxLen];
62 int n = strftime(buf, kDumpTimeMaxLen, kDumpTimeFormat, tm);
63 DCHECK_GT(n, 0);
65 std::stringstream entrystream;
66 entrystream << params_.process_name << "|" << buf << "|"
67 << crashed_process_dump_ << "|" << params_.process_uptime << "|"
68 << logfile_ << "|" << params_.suffix << "|"
69 << params_.previous_app_name << "|" << params_.current_app_name
70 << "|" << params_.last_app_name << "|"
71 << params_.cast_release_version << "|"
72 << params_.cast_build_number << std::endl;
73 return entrystream.str();
76 bool DumpInfo::ParseEntry(const std::string& entry) {
77 // The format is
78 // <name>|<dump time>|<dump>|<uptime>|<logfile>{|<suffix>{|<prev_app_name>{
79 // |<current_app name>{|last_launched_app_name}}}}
80 // <dump time> is in the format |kDumpTimeFormat|
81 std::vector<std::string> fields;
82 base::SplitString(entry, '|', &fields);
83 if (fields.size() < kNumRequiredParams) {
84 LOG(INFO) << "Invalid entry: Too few fields.";
85 return false;
88 // Extract required fields.
89 params_.process_name = fields[0];
90 if (!SetDumpTimeFromString(fields[1]))
91 return false;
92 crashed_process_dump_ = fields[2];
93 params_.process_uptime = atoll(fields[3].c_str());
94 logfile_ = fields[4];
96 // Extract all other optional fields.
97 for (size_t i = 5; i < fields.size(); ++i) {
98 const std::string& temp = fields[i];
99 switch (i) {
100 case 5: // Optional field: suffix
101 params_.suffix = temp;
102 break;
103 case 6: // Optional field: prev_app_name
104 params_.previous_app_name = temp;
105 break;
106 case 7: // Optional field: current_app_name
107 params_.current_app_name = temp;
108 break;
109 case 8: // Optional field: last_launched_app_name
110 params_.last_app_name = temp;
111 break;
112 case 9: // extract an optional cast release version
113 params_.cast_release_version = temp;
114 break;
115 case 10: // extract an optional cast build number
116 params_.cast_build_number = temp;
117 break;
118 default:
119 LOG(INFO) << "Entry has too many fields invalid";
120 return false;
123 valid_ = true;
124 return true;
127 bool DumpInfo::SetDumpTimeFromString(const std::string& timestr) {
128 struct tm tm = {0};
129 char* text = strptime(timestr.c_str(), kDumpTimeFormat, &tm);
130 dump_time_ = mktime(&tm);
131 if (!text || dump_time_ < 0) {
132 LOG(INFO) << "Failed to convert dump time invalid";
133 return false;
135 return true;
138 } // namespace chromecast