1 // Copyright 2013 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.
5 #include "chrome/browser/extensions/api/log_private/syslog_parser.h"
10 #include "base/logging.h"
11 #include "base/memory/linked_ptr.h"
12 #include "base/memory/singleton.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_split.h"
15 #include "base/strings/string_tokenizer.h"
16 #include "base/time/time.h"
17 #include "chrome/browser/extensions/api/log_private/filter_handler.h"
18 #include "chrome/browser/extensions/api/log_private/log_parser.h"
19 #include "chrome/browser/extensions/api/log_private/log_private_api.h"
20 #include "chrome/common/extensions/api/log_private.h"
22 namespace extensions
{
26 const char kProcessInfoDelimiters
[] = "[]:";
30 SyslogParser::SyslogParser() {}
32 SyslogParser::~SyslogParser() {}
34 SyslogParser::Error
SyslogParser::ParseEntry(
35 const std::string
& input
,
36 std::vector
<linked_ptr
<api::log_private::LogEntry
> >* output
,
37 FilterHandler
* filter_handler
) const {
38 linked_ptr
<api::log_private::LogEntry
> entry(new api::log_private::LogEntry
);
40 base::StringTokenizer
tokenizer(input
, " ");
41 if (!tokenizer
.GetNext()) {
43 << "Error when parsing data. Expect: At least 2 tokens. Actual: 0";
44 return TOKENIZE_ERROR
;
46 std::string time
= tokenizer
.token();
47 if (ParseTime(time
, &(entry
->timestamp
)) != SyslogParser::SUCCESS
) {
48 return SyslogParser::PARSE_ERROR
;
50 if (!tokenizer
.GetNext()) {
52 << "Error when parsing data. Expect: At least 2 tokens. Actual: 1";
53 return TOKENIZE_ERROR
;
55 ParseProcess(tokenizer
.token(), entry
.get());
56 ParseLevel(input
, entry
.get());
57 entry
->full_entry
= input
;
59 if (filter_handler
->IsValidLogEntry(*(entry
.get()))) {
60 output
->push_back(entry
);
63 return SyslogParser::SUCCESS
;
66 SyslogParser::Error
SyslogParser::ParseTime(const std::string
& input
,
67 double* output
) const {
68 base::Time parsed_time
;
69 if (!base::Time::FromString(input
.c_str(), &parsed_time
)) {
70 LOG(ERROR
) << "Error when parsing time";
71 return SyslogParser::PARSE_ERROR
;
74 *output
= parsed_time
.ToJsTime();
75 return SyslogParser::SUCCESS
;
78 SyslogParser::Error
SyslogParser::ParseProcess(
79 const std::string
& input
,
80 api::log_private::LogEntry
* entry
) const {
81 base::StringTokenizer
tokenizer(input
, kProcessInfoDelimiters
);
82 if (!tokenizer
.GetNext()) {
84 << "Error when parsing data. Expect: At least 1 token. Actual: 0";
85 return SyslogParser::PARSE_ERROR
;
87 entry
->process
= tokenizer
.token();
88 entry
->process_id
= "unknown";
89 if (tokenizer
.GetNext()) {
90 std::string token
= tokenizer
.token();
92 if (base::StringToInt(token
, &tmp
)) {
93 entry
->process_id
= token
;
96 return SyslogParser::SUCCESS
;
99 void SyslogParser::ParseLevel(const std::string
& input
,
100 api::log_private::LogEntry
* entry
) const {
101 if (input
.find("ERROR") != std::string::npos
) {
102 entry
->level
= "error";
103 } else if (input
.find("WARN") != std::string::npos
) {
104 entry
->level
= "warning";
105 } else if (input
.find("INFO") != std::string::npos
) {
106 entry
->level
= "info";
108 entry
->level
= "unknown";
112 } // namespace extensions