| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/device_event_log/device_event_log_impl.h" | 5 #include "components/device_event_log/device_event_log_impl.h" |
| 6 | 6 |
| 7 #include <cmath> | 7 #include <cmath> |
| 8 #include <list> | 8 #include <list> |
| 9 #include <set> | 9 #include <set> |
| 10 | 10 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 | 22 |
| 23 namespace device_event_log { | 23 namespace device_event_log { |
| 24 | 24 |
| 25 namespace { | 25 namespace { |
| 26 | 26 |
| 27 const char* kLogLevelName[] = {"Error", "User", "Event", "Debug"}; | 27 const char* kLogLevelName[] = {"Error", "User", "Event", "Debug"}; |
| 28 | 28 |
| 29 const char* kLogTypeNetworkDesc = "Network"; | 29 const char* kLogTypeNetworkDesc = "Network"; |
| 30 const char* kLogTypePowerDesc = "Power"; | 30 const char* kLogTypePowerDesc = "Power"; |
| 31 const char* kLogTypeLoginDesc = "Login"; | 31 const char* kLogTypeLoginDesc = "Login"; |
| 32 const char* kLogTypeBluetoothDesc = "Bluetooth"; |
| 32 const char* kLogTypeUsbDesc = "USB"; | 33 const char* kLogTypeUsbDesc = "USB"; |
| 33 const char* kLogTypeHidDesc = "HID"; | 34 const char* kLogTypeHidDesc = "HID"; |
| 34 | 35 |
| 35 std::string GetLogTypeString(LogType type) { | 36 std::string GetLogTypeString(LogType type) { |
| 36 switch (type) { | 37 switch (type) { |
| 37 case LOG_TYPE_NETWORK: | 38 case LOG_TYPE_NETWORK: |
| 38 return kLogTypeNetworkDesc; | 39 return kLogTypeNetworkDesc; |
| 39 case LOG_TYPE_POWER: | 40 case LOG_TYPE_POWER: |
| 40 return kLogTypePowerDesc; | 41 return kLogTypePowerDesc; |
| 41 case LOG_TYPE_LOGIN: | 42 case LOG_TYPE_LOGIN: |
| 42 return kLogTypeLoginDesc; | 43 return kLogTypeLoginDesc; |
| 44 case LOG_TYPE_BLUETOOTH: |
| 45 return kLogTypeBluetoothDesc; |
| 43 case LOG_TYPE_USB: | 46 case LOG_TYPE_USB: |
| 44 return kLogTypeUsbDesc; | 47 return kLogTypeUsbDesc; |
| 45 case LOG_TYPE_HID: | 48 case LOG_TYPE_HID: |
| 46 return kLogTypeHidDesc; | 49 return kLogTypeHidDesc; |
| 47 default: | 50 case LOG_TYPE_UNKNOWN: |
| 48 NOTREACHED(); | 51 break; |
| 49 return "Unknown"; | |
| 50 } | 52 } |
| 53 NOTREACHED(); |
| 54 return "Unknown"; |
| 55 } |
| 56 |
| 57 LogType GetLogTypeFromString(const std::string& desc) { |
| 58 std::string desc_lc = base::ToLowerASCII(desc); |
| 59 for (int i = 0; i < LOG_TYPE_UNKNOWN; ++i) { |
| 60 auto type = static_cast<LogType>(i); |
| 61 std::string log_desc_lc = base::ToLowerASCII(GetLogTypeString(type)); |
| 62 if (desc_lc == log_desc_lc) |
| 63 return type; |
| 64 } |
| 65 NOTREACHED() << "Unrecogized LogType: " << desc; |
| 66 return LOG_TYPE_UNKNOWN; |
| 51 } | 67 } |
| 52 | 68 |
| 53 std::string DateAndTimeWithMicroseconds(const base::Time& time) { | 69 std::string DateAndTimeWithMicroseconds(const base::Time& time) { |
| 54 base::Time::Exploded exploded; | 70 base::Time::Exploded exploded; |
| 55 time.LocalExplode(&exploded); | 71 time.LocalExplode(&exploded); |
| 56 // base::Time::Exploded does not include microseconds, but sometimes we need | 72 // base::Time::Exploded does not include microseconds, but sometimes we need |
| 57 // microseconds, so append '.' + usecs to the end of the formatted string. | 73 // microseconds, so append '.' + usecs to the end of the formatted string. |
| 58 int usecs = static_cast<int>(fmod(time.ToDoubleT() * 1000000, 1000000)); | 74 int usecs = static_cast<int>(fmod(time.ToDoubleT() * 1000000, 1000000)); |
| 59 return base::StringPrintf("%04d/%02d/%02d %02d:%02d:%02d.%06d", exploded.year, | 75 return base::StringPrintf("%04d/%02d/%02d %02d:%02d:%02d.%06d", exploded.year, |
| 60 exploded.month, exploded.day_of_month, | 76 exploded.month, exploded.day_of_month, |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 *show_file = true; | 194 *show_file = true; |
| 179 if (tok == "type") | 195 if (tok == "type") |
| 180 *show_type = true; | 196 *show_type = true; |
| 181 if (tok == "level") | 197 if (tok == "level") |
| 182 *show_level = true; | 198 *show_level = true; |
| 183 if (tok == "json") | 199 if (tok == "json") |
| 184 *format_json = true; | 200 *format_json = true; |
| 185 } | 201 } |
| 186 } | 202 } |
| 187 | 203 |
| 188 LogType LogTypeFromString(const std::string& desc) { | |
| 189 std::string desc_lc = base::ToLowerASCII(desc); | |
| 190 if (desc_lc == "network") | |
| 191 return LOG_TYPE_NETWORK; | |
| 192 if (desc_lc == "power") | |
| 193 return LOG_TYPE_POWER; | |
| 194 if (desc_lc == "login") | |
| 195 return LOG_TYPE_LOGIN; | |
| 196 NOTREACHED() << "Unrecogized LogType: " << desc; | |
| 197 return LOG_TYPE_UNKNOWN; | |
| 198 } | |
| 199 | |
| 200 void GetLogTypes(const std::string& types, | 204 void GetLogTypes(const std::string& types, |
| 201 std::set<LogType>* include_types, | 205 std::set<LogType>* include_types, |
| 202 std::set<LogType>* exclude_types) { | 206 std::set<LogType>* exclude_types) { |
| 203 base::StringTokenizer tokens(types, ","); | 207 base::StringTokenizer tokens(types, ","); |
| 204 while (tokens.GetNext()) { | 208 while (tokens.GetNext()) { |
| 205 std::string tok(tokens.token()); | 209 std::string tok(tokens.token()); |
| 206 if (tok.substr(0, 4) == "non-") { | 210 if (tok.substr(0, 4) == "non-") { |
| 207 LogType type = LogTypeFromString(tok.substr(4)); | 211 LogType type = GetLogTypeFromString(tok.substr(4)); |
| 208 if (type != LOG_TYPE_UNKNOWN) | 212 if (type != LOG_TYPE_UNKNOWN) |
| 209 exclude_types->insert(type); | 213 exclude_types->insert(type); |
| 210 } else { | 214 } else { |
| 211 LogType type = LogTypeFromString(tok); | 215 LogType type = GetLogTypeFromString(tok); |
| 212 if (type != LOG_TYPE_UNKNOWN) | 216 if (type != LOG_TYPE_UNKNOWN) |
| 213 include_types->insert(type); | 217 include_types->insert(type); |
| 214 } | 218 } |
| 215 } | 219 } |
| 216 } | 220 } |
| 217 | 221 |
| 218 // Update count and time for identical events to avoid log spam. | 222 // Update count and time for identical events to avoid log spam. |
| 219 void IncreaseLogEntryCount(const DeviceEventLogImpl::LogEntry& new_entry, | 223 void IncreaseLogEntryCount(const DeviceEventLogImpl::LogEntry& new_entry, |
| 220 DeviceEventLogImpl::LogEntry* cur_entry) { | 224 DeviceEventLogImpl::LogEntry* cur_entry) { |
| 221 ++cur_entry->count; | 225 ++cur_entry->count; |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 391 size_t last_slash_pos = file.find_last_of("\\/"); | 395 size_t last_slash_pos = file.find_last_of("\\/"); |
| 392 if (last_slash_pos != std::string::npos) { | 396 if (last_slash_pos != std::string::npos) { |
| 393 file.erase(0, last_slash_pos + 1); | 397 file.erase(0, last_slash_pos + 1); |
| 394 } | 398 } |
| 395 } | 399 } |
| 396 } | 400 } |
| 397 | 401 |
| 398 DeviceEventLogImpl::LogEntry::LogEntry(const LogEntry& other) = default; | 402 DeviceEventLogImpl::LogEntry::LogEntry(const LogEntry& other) = default; |
| 399 | 403 |
| 400 } // namespace device_event_log | 404 } // namespace device_event_log |
| OLD | NEW |