| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chromeos/device_event_log_impl.h" | |
| 6 | |
| 7 #include <cmath> | |
| 8 #include <list> | |
| 9 #include <set> | |
| 10 | |
| 11 #include "base/files/file_path.h" | |
| 12 #include "base/json/json_string_value_serializer.h" | |
| 13 #include "base/json/json_writer.h" | |
| 14 #include "base/logging.h" | |
| 15 #include "base/memory/scoped_ptr.h" | |
| 16 #include "base/strings/string_tokenizer.h" | |
| 17 #include "base/strings/string_util.h" | |
| 18 #include "base/strings/stringprintf.h" | |
| 19 #include "base/strings/utf_string_conversions.h" | |
| 20 #include "base/values.h" | |
| 21 #include "net/base/escape.h" | |
| 22 | |
| 23 namespace chromeos { | |
| 24 | |
| 25 namespace device_event_log { | |
| 26 | |
| 27 namespace { | |
| 28 | |
| 29 const char* kLogLevelName[] = {"Error", "User", "Event", "Debug"}; | |
| 30 | |
| 31 const char* kLogTypeNetworkDesc = "Network"; | |
| 32 const char* kLogTypePowerDesc = "Power"; | |
| 33 const char* kLogTypeLoginDesc = "Login"; | |
| 34 | |
| 35 std::string GetLogTypeString(LogType type) { | |
| 36 if (type == LOG_TYPE_NETWORK) | |
| 37 return kLogTypeNetworkDesc; | |
| 38 if (type == LOG_TYPE_POWER) | |
| 39 return kLogTypePowerDesc; | |
| 40 if (type == LOG_TYPE_LOGIN) | |
| 41 return kLogTypeLoginDesc; | |
| 42 NOTREACHED(); | |
| 43 return "Unknown"; | |
| 44 } | |
| 45 | |
| 46 std::string DateAndTimeWithMicroseconds(const base::Time& time) { | |
| 47 base::Time::Exploded exploded; | |
| 48 time.LocalExplode(&exploded); | |
| 49 // base::Time::Exploded does not include microseconds, but sometimes we need | |
| 50 // microseconds, so append '.' + usecs to the end of the formatted string. | |
| 51 int usecs = static_cast<int>(fmod(time.ToDoubleT() * 1000000, 1000000)); | |
| 52 return base::StringPrintf("%04d/%02d/%02d %02d:%02d:%02d.%06d", exploded.year, | |
| 53 exploded.month, exploded.day_of_month, | |
| 54 exploded.hour, exploded.minute, exploded.second, | |
| 55 usecs); | |
| 56 } | |
| 57 | |
| 58 std::string TimeWithSeconds(const base::Time& time) { | |
| 59 base::Time::Exploded exploded; | |
| 60 time.LocalExplode(&exploded); | |
| 61 return base::StringPrintf("%02d:%02d:%02d", exploded.hour, exploded.minute, | |
| 62 exploded.second); | |
| 63 } | |
| 64 | |
| 65 std::string TimeWithMillieconds(const base::Time& time) { | |
| 66 base::Time::Exploded exploded; | |
| 67 time.LocalExplode(&exploded); | |
| 68 return base::StringPrintf("%02d:%02d:%02d.%03d", exploded.hour, | |
| 69 exploded.minute, exploded.second, | |
| 70 exploded.millisecond); | |
| 71 } | |
| 72 | |
| 73 // Defined below for easier review. TODO(stevenjb): Move implementation here. | |
| 74 std::string GetHtmlText(LogLevel log_level, const std::string& event); | |
| 75 | |
| 76 std::string LogEntryToString(const DeviceEventLogImpl::LogEntry& log_entry, | |
| 77 bool show_time, | |
| 78 bool show_file, | |
| 79 bool show_type, | |
| 80 bool show_level, | |
| 81 bool format_html) { | |
| 82 std::string line; | |
| 83 if (show_time) | |
| 84 line += "[" + TimeWithMillieconds(log_entry.time) + "] "; | |
| 85 if (show_type) | |
| 86 line += GetLogTypeString(log_entry.log_type) + ": "; | |
| 87 if (show_level) { | |
| 88 const char* kLevelDesc[] = {"ERROR", "USER", "EVENT", "DEBUG"}; | |
| 89 line += base::StringPrintf("%s: ", kLevelDesc[log_entry.log_level]); | |
| 90 } | |
| 91 if (show_file) { | |
| 92 std::string filestr = | |
| 93 format_html ? net::EscapeForHTML(log_entry.file) : log_entry.file; | |
| 94 line += base::StringPrintf("%s:%d ", log_entry.file.c_str(), | |
| 95 log_entry.file_line); | |
| 96 } | |
| 97 line += format_html ? GetHtmlText(log_entry.log_level, log_entry.event) | |
| 98 : log_entry.event; | |
| 99 if (log_entry.count > 1) | |
| 100 line += base::StringPrintf(" (%d)", log_entry.count); | |
| 101 return line; | |
| 102 } | |
| 103 | |
| 104 void LogEntryToDictionary(const DeviceEventLogImpl::LogEntry& log_entry, | |
| 105 base::DictionaryValue* output) { | |
| 106 output->SetString("timestamp", DateAndTimeWithMicroseconds(log_entry.time)); | |
| 107 output->SetString("timestampshort", TimeWithSeconds(log_entry.time)); | |
| 108 output->SetString("level", kLogLevelName[log_entry.log_level]); | |
| 109 output->SetString("type", GetLogTypeString(log_entry.log_type)); | |
| 110 output->SetString("file", base::StringPrintf("%s:%d ", log_entry.file.c_str(), | |
| 111 log_entry.file_line)); | |
| 112 output->SetString("event", log_entry.event); | |
| 113 } | |
| 114 | |
| 115 std::string LogEntryAsJSON(const DeviceEventLogImpl::LogEntry& log_entry) { | |
| 116 base::DictionaryValue entry_dict; | |
| 117 LogEntryToDictionary(log_entry, &entry_dict); | |
| 118 std::string json; | |
| 119 JSONStringValueSerializer serializer(&json); | |
| 120 if (!serializer.Serialize(entry_dict)) { | |
| 121 LOG(ERROR) << "Failed to serialize to JSON"; | |
| 122 } | |
| 123 return json; | |
| 124 } | |
| 125 | |
| 126 std::string GetHtmlText(LogLevel log_level, const std::string& event) { | |
| 127 std::string text; | |
| 128 if (log_level == LOG_LEVEL_DEBUG) | |
| 129 text += "<i>"; | |
| 130 else if (log_level == LOG_LEVEL_USER) | |
| 131 text += "<b>"; | |
| 132 else if (log_level == LOG_LEVEL_ERROR) | |
| 133 text += "<b><i>"; | |
| 134 | |
| 135 text += net::EscapeForHTML(event); | |
| 136 | |
| 137 if (log_level == LOG_LEVEL_DEBUG) | |
| 138 text += "</i>"; | |
| 139 else if (log_level == LOG_LEVEL_USER) | |
| 140 text += "</b>"; | |
| 141 else if (log_level == LOG_LEVEL_ERROR) | |
| 142 text += "</i></b>"; | |
| 143 return text; | |
| 144 } | |
| 145 | |
| 146 void SendLogEntryToVLogOrErrorLog( | |
| 147 const DeviceEventLogImpl::LogEntry& log_entry) { | |
| 148 if (log_entry.log_level != LOG_LEVEL_ERROR && !VLOG_IS_ON(1)) | |
| 149 return; | |
| 150 const bool show_time = true; | |
| 151 const bool show_file = true; | |
| 152 const bool show_type = true; | |
| 153 const bool show_level = false; | |
| 154 const bool format_html = false; | |
| 155 std::string output = LogEntryToString(log_entry, show_time, show_file, | |
| 156 show_type, show_level, format_html); | |
| 157 if (log_entry.log_level == LOG_LEVEL_ERROR) | |
| 158 LOG(ERROR) << output; | |
| 159 else | |
| 160 VLOG(1) << output; | |
| 161 } | |
| 162 | |
| 163 bool LogEntryMatches(const DeviceEventLogImpl::LogEntry& first, | |
| 164 const DeviceEventLogImpl::LogEntry& second) { | |
| 165 return first.file == second.file && first.file_line == second.file_line && | |
| 166 first.log_level == second.log_level && | |
| 167 first.log_type == second.log_type && first.event == second.event; | |
| 168 } | |
| 169 | |
| 170 bool LogEntryMatchesTypes(const DeviceEventLogImpl::LogEntry& entry, | |
| 171 const std::set<LogType>& include_types, | |
| 172 const std::set<LogType>& exclude_types) { | |
| 173 if (include_types.empty() && exclude_types.empty()) | |
| 174 return true; | |
| 175 if (!include_types.empty() && include_types.count(entry.log_type)) | |
| 176 return true; | |
| 177 if (!exclude_types.empty() && !exclude_types.count(entry.log_type)) | |
| 178 return true; | |
| 179 return false; | |
| 180 } | |
| 181 | |
| 182 void GetFormat(const std::string& format_string, | |
| 183 bool* show_time, | |
| 184 bool* show_file, | |
| 185 bool* show_type, | |
| 186 bool* show_level, | |
| 187 bool* format_html, | |
| 188 bool* format_json) { | |
| 189 base::StringTokenizer tokens(format_string, ","); | |
| 190 *show_time = false; | |
| 191 *show_file = false; | |
| 192 *show_type = false; | |
| 193 *show_level = false; | |
| 194 *format_html = false; | |
| 195 *format_json = false; | |
| 196 while (tokens.GetNext()) { | |
| 197 std::string tok(tokens.token()); | |
| 198 if (tok == "time") | |
| 199 *show_time = true; | |
| 200 if (tok == "file") | |
| 201 *show_file = true; | |
| 202 if (tok == "type") | |
| 203 *show_type = true; | |
| 204 if (tok == "level") | |
| 205 *show_level = true; | |
| 206 if (tok == "html") | |
| 207 *format_html = true; | |
| 208 if (tok == "json") | |
| 209 *format_json = true; | |
| 210 } | |
| 211 } | |
| 212 | |
| 213 LogType LogTypeFromString(const std::string& desc) { | |
| 214 std::string desc_lc = base::StringToLowerASCII(desc); | |
| 215 if (desc_lc == "network") | |
| 216 return LOG_TYPE_NETWORK; | |
| 217 if (desc_lc == "power") | |
| 218 return LOG_TYPE_POWER; | |
| 219 if (desc_lc == "login") | |
| 220 return LOG_TYPE_LOGIN; | |
| 221 NOTREACHED() << "Unrecogized LogType: " << desc; | |
| 222 return LOG_TYPE_UNKNOWN; | |
| 223 } | |
| 224 | |
| 225 void GetLogTypes(const std::string& types, | |
| 226 std::set<LogType>* include_types, | |
| 227 std::set<LogType>* exclude_types) { | |
| 228 base::StringTokenizer tokens(types, ","); | |
| 229 while (tokens.GetNext()) { | |
| 230 std::string tok(tokens.token()); | |
| 231 if (tok.substr(0, 4) == "non-") { | |
| 232 LogType type = LogTypeFromString(tok.substr(4)); | |
| 233 if (type != LOG_TYPE_UNKNOWN) | |
| 234 exclude_types->insert(type); | |
| 235 } else { | |
| 236 LogType type = LogTypeFromString(tok); | |
| 237 if (type != LOG_TYPE_UNKNOWN) | |
| 238 include_types->insert(type); | |
| 239 } | |
| 240 } | |
| 241 } | |
| 242 | |
| 243 } // namespace | |
| 244 | |
| 245 // static | |
| 246 void DeviceEventLogImpl::SendToVLogOrErrorLog(const char* file, | |
| 247 int file_line, | |
| 248 LogType log_type, | |
| 249 LogLevel log_level, | |
| 250 const std::string& event) { | |
| 251 LogEntry entry(file, file_line, log_type, log_level, event); | |
| 252 SendLogEntryToVLogOrErrorLog(entry); | |
| 253 } | |
| 254 | |
| 255 DeviceEventLogImpl::DeviceEventLogImpl(size_t max_entries) | |
| 256 : max_entries_(max_entries) { | |
| 257 } | |
| 258 | |
| 259 DeviceEventLogImpl::~DeviceEventLogImpl() { | |
| 260 } | |
| 261 | |
| 262 void DeviceEventLogImpl::AddEntry(const char* file, | |
| 263 int file_line, | |
| 264 LogType log_type, | |
| 265 LogLevel log_level, | |
| 266 const std::string& event) { | |
| 267 LogEntry entry(file, file_line, log_type, log_level, event); | |
| 268 AddLogEntry(entry); | |
| 269 } | |
| 270 | |
| 271 void DeviceEventLogImpl::AddLogEntry(const LogEntry& entry) { | |
| 272 if (!entries_.empty()) { | |
| 273 LogEntry& last = entries_.back(); | |
| 274 if (LogEntryMatches(last, entry)) { | |
| 275 // Update count and time for identical events to avoid log spam. | |
| 276 ++last.count; | |
| 277 last.log_level = std::min(last.log_level, entry.log_level); | |
| 278 last.time = base::Time::Now(); | |
| 279 return; | |
| 280 } | |
| 281 } | |
| 282 if (entries_.size() >= max_entries_) { | |
| 283 const size_t max_error_entries = max_entries_ / 2; | |
| 284 // Remove the first (oldest) non-error entry, or the oldest entry if more | |
| 285 // than half the entries are errors. | |
| 286 size_t error_count = 0; | |
| 287 for (LogEntryList::iterator iter = entries_.begin(); iter != entries_.end(); | |
| 288 ++iter) { | |
| 289 if (iter->log_level != LOG_LEVEL_ERROR) { | |
| 290 entries_.erase(iter); | |
| 291 break; | |
| 292 } | |
| 293 if (++error_count > max_error_entries) { | |
| 294 // Too many error entries, remove the oldest entry. | |
| 295 entries_.pop_front(); | |
| 296 break; | |
| 297 } | |
| 298 } | |
| 299 } | |
| 300 entries_.push_back(entry); | |
| 301 SendLogEntryToVLogOrErrorLog(entry); | |
| 302 } | |
| 303 | |
| 304 std::string DeviceEventLogImpl::GetAsString(StringOrder order, | |
| 305 const std::string& format, | |
| 306 const std::string& types, | |
| 307 LogLevel max_level, | |
| 308 size_t max_events) { | |
| 309 if (entries_.empty()) | |
| 310 return "No Log Entries."; | |
| 311 | |
| 312 bool show_time, show_file, show_type, show_level, format_html, format_json; | |
| 313 GetFormat(format, &show_time, &show_file, &show_type, &show_level, | |
| 314 &format_html, &format_json); | |
| 315 | |
| 316 std::set<LogType> include_types, exclude_types; | |
| 317 GetLogTypes(types, &include_types, &exclude_types); | |
| 318 | |
| 319 std::string result; | |
| 320 base::ListValue log_entries; | |
| 321 if (order == OLDEST_FIRST) { | |
| 322 size_t offset = 0; | |
| 323 if (max_events > 0 && max_events < entries_.size()) { | |
| 324 // Iterate backwards through the list skipping uninteresting entries to | |
| 325 // determine the first entry to include. | |
| 326 size_t shown_events = 0; | |
| 327 size_t num_entries = 0; | |
| 328 for (LogEntryList::const_reverse_iterator riter = entries_.rbegin(); | |
| 329 riter != entries_.rend(); ++riter) { | |
| 330 ++num_entries; | |
| 331 if (!LogEntryMatchesTypes(*riter, include_types, exclude_types)) | |
| 332 continue; | |
| 333 if (riter->log_level > max_level) | |
| 334 continue; | |
| 335 if (++shown_events >= max_events) | |
| 336 break; | |
| 337 } | |
| 338 offset = entries_.size() - num_entries; | |
| 339 } | |
| 340 for (LogEntryList::const_iterator iter = entries_.begin(); | |
| 341 iter != entries_.end(); ++iter) { | |
| 342 if (offset > 0) { | |
| 343 --offset; | |
| 344 continue; | |
| 345 } | |
| 346 if (!LogEntryMatchesTypes(*iter, include_types, exclude_types)) | |
| 347 continue; | |
| 348 if (iter->log_level > max_level) | |
| 349 continue; | |
| 350 if (format_json) { | |
| 351 log_entries.AppendString(LogEntryAsJSON(*iter)); | |
| 352 } else { | |
| 353 result += LogEntryToString(*iter, show_time, show_file, show_type, | |
| 354 show_level, format_html); | |
| 355 result += "\n"; | |
| 356 } | |
| 357 } | |
| 358 } else { | |
| 359 size_t nlines = 0; | |
| 360 // Iterate backwards through the list to show the most recent entries first. | |
| 361 for (LogEntryList::const_reverse_iterator riter = entries_.rbegin(); | |
| 362 riter != entries_.rend(); ++riter) { | |
| 363 if (!LogEntryMatchesTypes(*riter, include_types, exclude_types)) | |
| 364 continue; | |
| 365 if (riter->log_level > max_level) | |
| 366 continue; | |
| 367 if (format_json) { | |
| 368 log_entries.AppendString(LogEntryAsJSON(*riter)); | |
| 369 } else { | |
| 370 result += LogEntryToString(*riter, show_time, show_file, show_type, | |
| 371 show_level, format_html); | |
| 372 result += "\n"; | |
| 373 } | |
| 374 if (max_events > 0 && ++nlines >= max_events) | |
| 375 break; | |
| 376 } | |
| 377 } | |
| 378 if (format_json) { | |
| 379 JSONStringValueSerializer serializer(&result); | |
| 380 serializer.Serialize(log_entries); | |
| 381 } | |
| 382 | |
| 383 return result; | |
| 384 } | |
| 385 | |
| 386 DeviceEventLogImpl::LogEntry::LogEntry(const char* filedesc, | |
| 387 int file_line, | |
| 388 LogType log_type, | |
| 389 LogLevel log_level, | |
| 390 const std::string& event) | |
| 391 : file_line(file_line), | |
| 392 log_type(log_type), | |
| 393 log_level(log_level), | |
| 394 event(event), | |
| 395 time(base::Time::Now()), | |
| 396 count(1) { | |
| 397 if (filedesc) | |
| 398 file = base::FilePath(std::string(filedesc)).BaseName().value(); | |
| 399 } | |
| 400 | |
| 401 } // namespace device_event_log | |
| 402 | |
| 403 } // namespace chromeos | |
| OLD | NEW |