| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/api/log_private/syslog_parser.h" | 5 #include "chrome/browser/extensions/api/log_private/syslog_parser.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/memory/linked_ptr.h" | 11 #include "base/memory/linked_ptr.h" |
| 12 #include "base/memory/singleton.h" | 12 #include "base/memory/singleton.h" |
| 13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/strings/string_split.h" | 14 #include "base/strings/string_split.h" |
| 15 #include "base/strings/string_tokenizer.h" | 15 #include "base/strings/string_tokenizer.h" |
| 16 #include "base/time/time.h" | 16 #include "base/time/time.h" |
| 17 #include "chrome/browser/extensions/api/log_private/filter_handler.h" | 17 #include "chrome/browser/extensions/api/log_private/filter_handler.h" |
| 18 #include "chrome/browser/extensions/api/log_private/log_parser.h" | 18 #include "chrome/browser/extensions/api/log_private/log_parser.h" |
| 19 #include "chrome/browser/extensions/api/log_private/log_private_api.h" | 19 #include "chrome/browser/extensions/api/log_private/log_private_api.h" |
| 20 #include "chrome/common/extensions/api/log_private.h" | 20 #include "chrome/common/extensions/api/log_private.h" |
| 21 | 21 |
| 22 namespace extensions { | 22 namespace extensions { |
| 23 | 23 |
| 24 namespace { | 24 namespace { |
| 25 | 25 |
| 26 const int kExpectedTimeTokenNum = 7; | |
| 27 const char kLogEntryDelimiters[] = "-:T"; | |
| 28 const char kProcessInfoDelimiters[] = "[]"; | 26 const char kProcessInfoDelimiters[] = "[]"; |
| 29 | 27 |
| 30 } // namespace | 28 } // namespace |
| 31 | 29 |
| 32 SyslogParser::SyslogParser() {} | 30 SyslogParser::SyslogParser() {} |
| 33 | 31 |
| 34 SyslogParser::~SyslogParser() {} | 32 SyslogParser::~SyslogParser() {} |
| 35 | 33 |
| 36 SyslogParser::Error SyslogParser::ParseEntry( | 34 SyslogParser::Error SyslogParser::ParseEntry( |
| 37 const std::string& input, | 35 const std::string& input, |
| (...skipping 26 matching lines...) Expand all Loading... |
| 64 ParseLevel(input, entry.get()); | 62 ParseLevel(input, entry.get()); |
| 65 entry->full_entry = input; | 63 entry->full_entry = input; |
| 66 | 64 |
| 67 if (filter_handler->IsValidLogEntry(*(entry.get()))) { | 65 if (filter_handler->IsValidLogEntry(*(entry.get()))) { |
| 68 output->push_back(entry); | 66 output->push_back(entry); |
| 69 } | 67 } |
| 70 | 68 |
| 71 return SyslogParser::SUCCESS; | 69 return SyslogParser::SUCCESS; |
| 72 } | 70 } |
| 73 | 71 |
| 74 SyslogParser::Error ParseTimeHelper(base::StringTokenizer* tokenizer, | |
| 75 std::string* output) { | |
| 76 if (!tokenizer->GetNext()) { | |
| 77 LOG(ERROR) << "Error when parsing time"; | |
| 78 return SyslogParser::PARSE_ERROR; | |
| 79 } | |
| 80 *output = tokenizer->token(); | |
| 81 return SyslogParser::SUCCESS; | |
| 82 } | |
| 83 | |
| 84 SyslogParser::Error SyslogParser::ParseTime(const std::string& input, | 72 SyslogParser::Error SyslogParser::ParseTime(const std::string& input, |
| 85 double* output) const { | 73 double* output) const { |
| 86 base::StringTokenizer tokenizer(input, kLogEntryDelimiters); | |
| 87 std::string tokens[kExpectedTimeTokenNum]; | |
| 88 for (int i = 0; i < kExpectedTimeTokenNum; i++) { | |
| 89 if (ParseTimeHelper(&tokenizer, &(tokens[i])) != SyslogParser::SUCCESS) | |
| 90 return SyslogParser::PARSE_ERROR; | |
| 91 } | |
| 92 | |
| 93 std::string buffer = tokens[1] + '-' + tokens[2] + '-' + tokens[0] + ' ' + | |
| 94 tokens[3] + ':' + tokens[4] + ":00"; | |
| 95 | |
| 96 base::Time parsed_time; | 74 base::Time parsed_time; |
| 97 if (!base::Time::FromString(buffer.c_str(), &parsed_time)) { | 75 if (!base::Time::FromString(input.c_str(), &parsed_time)) { |
| 98 LOG(ERROR) << "Error when parsing time"; | 76 LOG(ERROR) << "Error when parsing time"; |
| 99 return SyslogParser::PARSE_ERROR; | 77 return SyslogParser::PARSE_ERROR; |
| 100 } | 78 } |
| 101 | 79 |
| 102 double seconds; | 80 *output = parsed_time.ToJsTime(); |
| 103 base::StringToDouble(tokens[5], &seconds); | |
| 104 *output = parsed_time.ToJsTime() + | |
| 105 (seconds * base::Time::kMillisecondsPerSecond); | |
| 106 | |
| 107 return SyslogParser::SUCCESS; | 81 return SyslogParser::SUCCESS; |
| 108 } | 82 } |
| 109 | 83 |
| 110 SyslogParser::Error SyslogParser::ParseProcess( | 84 SyslogParser::Error SyslogParser::ParseProcess( |
| 111 const std::string& input, | 85 const std::string& input, |
| 112 api::log_private::LogEntry* entry) const { | 86 api::log_private::LogEntry* entry) const { |
| 113 base::StringTokenizer tokenizer(input, kProcessInfoDelimiters); | 87 base::StringTokenizer tokenizer(input, kProcessInfoDelimiters); |
| 114 if (!tokenizer.GetNext()) { | 88 if (!tokenizer.GetNext()) { |
| 115 LOG(ERROR) | 89 LOG(ERROR) |
| 116 << "Error when parsing data. Expect: At least 1 token. Actual: 0"; | 90 << "Error when parsing data. Expect: At least 1 token. Actual: 0"; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 135 } else if (input.find("WARN") != std::string::npos) { | 109 } else if (input.find("WARN") != std::string::npos) { |
| 136 entry->level = "warning"; | 110 entry->level = "warning"; |
| 137 } else if (input.find("INFO") != std::string::npos) { | 111 } else if (input.find("INFO") != std::string::npos) { |
| 138 entry->level = "info"; | 112 entry->level = "info"; |
| 139 } else { | 113 } else { |
| 140 entry->level = "unknown"; | 114 entry->level = "unknown"; |
| 141 } | 115 } |
| 142 } | 116 } |
| 143 | 117 |
| 144 } // namespace extensions | 118 } // namespace extensions |
| OLD | NEW |