Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(429)

Side by Side Diff: chromecast/crash/linux/dump_info.cc

Issue 1154383006: Adding crash utilities to chromecast/crash. (Closed) Base URL: https://eureka-internal.googlesource.com/chromium/src@master
Patch Set: Linux-specific utils moved to linux/ Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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"
5
6 #include <stdlib.h>
7
8 #include <sstream>
9
10 #include "base/logging.h"
11 #include "base/strings/string_split.h"
12
13 namespace chromecast {
14
15 namespace {
16
17 const char kDumpTimeFormat[] = "%Y-%m-%d %H:%M:%S";
18 const unsigned kDumpTimeMaxLen = 255;
19
20 const int kNumRequiredParams = 5;
21 } // namespace
22
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();
27 }
28 }
29
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
43
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;
51 }
52 entry_ = GetEntryAsString();
53 valid_ = true;
54 }
55
56 DumpInfo::~DumpInfo() {
57 }
58
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);
64
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();
74 }
75
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;
86 }
87
88 // Extract the process name, which must be non-empty.
89 if (fields[0].empty()) {
90 LOG(INFO) << "Invalid entry: Process name is empty.";
91 return false;
92 }
93 params_.process_name = fields[0];
94
95 // Extract the time.
96 if (!SetDumpTimeFromString(fields[1]))
97 return false;
98
99 // Extract all other fields.
100 for (size_t i = 2; i < fields.size(); ++i) {
101 const std::string& temp = fields[i];
102 switch (i) {
103 case 2: // Required field: crashed process dump
104 crashed_process_dump_ = temp;
105 break;
106 case 3: // Required field: process uptime
107 params_.process_uptime = atoll(temp.c_str());
108 break;
109 case 4: // Required field: log file path
110 logfile_ = temp;
111 break;
112 case 5: // Optional field: suffix
113 params_.suffix = temp;
114 break;
115 case 6: // Optional field: prev_app_name
116 params_.previous_app_name = temp;
117 break;
118 case 7: // Optional field: current_app_name
119 params_.current_app_name = temp;
120 break;
121 case 8: // Optional field: last_launched_app_name
122 params_.last_app_name = temp;
123 break;
124 case 9: // extract an optional cast release version
125 params_.cast_release_version = temp;
126 break;
127 case 10: // extract an optional cast build number
128 params_.cast_build_number = temp;
129 break;
130 default:
131 LOG(INFO) << "Entry has too many fields invalid";
132 return false;
133 }
134 }
135 valid_ = true;
136 return true;
137 }
138
139 bool DumpInfo::SetDumpTimeFromString(const std::string& timestr) {
140 struct tm tm;
141 char* text = strptime(timestr.c_str(), kDumpTimeFormat, &tm);
142 dump_time_ = mktime(&tm);
143 if (!text || dump_time_ < 0) {
144 LOG(INFO) << "Failed to convert dump time invalid";
145 return false;
146 }
147 return true;
148 }
149
150 } // namespace chromecast
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698