| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "chrome/browser/extensions/api/log_private/syslog_parser.h" | |
| 6 | |
| 7 #include <string> | |
| 8 #include <vector> | |
| 9 | |
| 10 #include "base/logging.h" | |
| 11 #include "base/memory/singleton.h" | |
| 12 #include "base/strings/string_number_conversions.h" | |
| 13 #include "base/strings/string_split.h" | |
| 14 #include "base/strings/string_tokenizer.h" | |
| 15 #include "base/time/time.h" | |
| 16 #include "chrome/browser/extensions/api/log_private/filter_handler.h" | |
| 17 #include "chrome/browser/extensions/api/log_private/log_parser.h" | |
| 18 #include "chrome/browser/extensions/api/log_private/log_private_api.h" | |
| 19 #include "chrome/common/extensions/api/log_private.h" | |
| 20 | |
| 21 namespace extensions { | |
| 22 | |
| 23 namespace { | |
| 24 | |
| 25 const char kProcessInfoDelimiters[] = "[]:"; | |
| 26 | |
| 27 } // namespace | |
| 28 | |
| 29 SyslogParser::SyslogParser() {} | |
| 30 | |
| 31 SyslogParser::~SyslogParser() {} | |
| 32 | |
| 33 SyslogParser::Error SyslogParser::ParseEntry( | |
| 34 const std::string& input, | |
| 35 std::vector<api::log_private::LogEntry>* output, | |
| 36 FilterHandler* filter_handler) const { | |
| 37 base::StringTokenizer tokenizer(input, " "); | |
| 38 if (!tokenizer.GetNext()) { | |
| 39 LOG(ERROR) | |
| 40 << "Error when parsing data. Expect: At least 2 tokens. Actual: 0"; | |
| 41 return TOKENIZE_ERROR; | |
| 42 } | |
| 43 std::string time = tokenizer.token(); | |
| 44 api::log_private::LogEntry entry; | |
| 45 if (ParseTime(time, &(entry.timestamp)) != SyslogParser::SUCCESS) { | |
| 46 return SyslogParser::PARSE_ERROR; | |
| 47 } | |
| 48 if (!tokenizer.GetNext()) { | |
| 49 LOG(ERROR) | |
| 50 << "Error when parsing data. Expect: At least 2 tokens. Actual: 1"; | |
| 51 return TOKENIZE_ERROR; | |
| 52 } | |
| 53 ParseProcess(tokenizer.token(), &entry); | |
| 54 ParseLevel(input, &entry); | |
| 55 entry.full_entry = input; | |
| 56 | |
| 57 if (filter_handler->IsValidLogEntry(entry)) { | |
| 58 output->push_back(std::move(entry)); | |
| 59 } | |
| 60 | |
| 61 return SyslogParser::SUCCESS; | |
| 62 } | |
| 63 | |
| 64 SyslogParser::Error SyslogParser::ParseTime(const std::string& input, | |
| 65 double* output) const { | |
| 66 base::Time parsed_time; | |
| 67 if (!base::Time::FromString(input.c_str(), &parsed_time)) { | |
| 68 LOG(ERROR) << "Error when parsing time"; | |
| 69 return SyslogParser::PARSE_ERROR; | |
| 70 } | |
| 71 | |
| 72 *output = parsed_time.ToJsTime(); | |
| 73 return SyslogParser::SUCCESS; | |
| 74 } | |
| 75 | |
| 76 SyslogParser::Error SyslogParser::ParseProcess( | |
| 77 const std::string& input, | |
| 78 api::log_private::LogEntry* entry) const { | |
| 79 base::StringTokenizer tokenizer(input, kProcessInfoDelimiters); | |
| 80 if (!tokenizer.GetNext()) { | |
| 81 LOG(ERROR) | |
| 82 << "Error when parsing data. Expect: At least 1 token. Actual: 0"; | |
| 83 return SyslogParser::PARSE_ERROR; | |
| 84 } | |
| 85 entry->process = tokenizer.token(); | |
| 86 entry->process_id = "unknown"; | |
| 87 if (tokenizer.GetNext()) { | |
| 88 std::string token = tokenizer.token(); | |
| 89 int tmp; | |
| 90 if (base::StringToInt(token, &tmp)) { | |
| 91 entry->process_id = token; | |
| 92 } | |
| 93 } | |
| 94 return SyslogParser::SUCCESS; | |
| 95 } | |
| 96 | |
| 97 void SyslogParser::ParseLevel(const std::string& input, | |
| 98 api::log_private::LogEntry* entry) const { | |
| 99 if (input.find("ERROR") != std::string::npos) { | |
| 100 entry->level = "error"; | |
| 101 } else if (input.find("WARN") != std::string::npos) { | |
| 102 entry->level = "warning"; | |
| 103 } else if (input.find("INFO") != std::string::npos) { | |
| 104 entry->level = "info"; | |
| 105 } else { | |
| 106 entry->level = "unknown"; | |
| 107 } | |
| 108 } | |
| 109 | |
| 110 } // namespace extensions | |
| OLD | NEW |