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"
10 #include "base/logging.h"
11 #include "base/strings/string_split.h"
13 namespace chromecast
{
17 const char kDumpTimeFormat
[] = "%Y-%m-%d %H:%M:%S";
18 const unsigned kDumpTimeMaxLen
= 255;
20 const int kNumRequiredParams
= 5;
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
),
36 dump_time_(dump_time
),
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
);
49 LOG(INFO
) << "strftime failed";
52 entry_
= GetEntryAsString();
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
);
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
) {
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.";
88 // Extract required fields.
89 params_
.process_name
= fields
[0];
90 if (!SetDumpTimeFromString(fields
[1]))
92 crashed_process_dump_
= fields
[2];
93 params_
.process_uptime
= atoll(fields
[3].c_str());
96 // Extract all other optional fields.
97 for (size_t i
= 5; i
< fields
.size(); ++i
) {
98 const std::string
& temp
= fields
[i
];
100 case 5: // Optional field: suffix
101 params_
.suffix
= temp
;
103 case 6: // Optional field: prev_app_name
104 params_
.previous_app_name
= temp
;
106 case 7: // Optional field: current_app_name
107 params_
.current_app_name
= temp
;
109 case 8: // Optional field: last_launched_app_name
110 params_
.last_app_name
= temp
;
112 case 9: // extract an optional cast release version
113 params_
.cast_release_version
= temp
;
115 case 10: // extract an optional cast build number
116 params_
.cast_build_number
= temp
;
119 LOG(INFO
) << "Entry has too many fields invalid";
127 bool DumpInfo::SetDumpTimeFromString(const std::string
& timestr
) {
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";
138 } // namespace chromecast