network_event_log.h revision eb525c5499e34cc9c4b825d6d9e75bb07cc06ace
1// Copyright (c) 2012 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#ifndef CHROMEOS_NETWORK_NETWORK_EVENT_LOG_H_ 6#define CHROMEOS_NETWORK_NETWORK_EVENT_LOG_H_ 7 8#include <deque> 9#include <string> 10 11#include "base/basictypes.h" 12#include "base/strings/stringprintf.h" 13#include "base/time/time.h" 14#include "chromeos/chromeos_export.h" 15 16namespace base { 17class Value; 18} 19 20namespace chromeos { 21 22// Namespace for functions for logging network events. 23namespace network_event_log { 24 25// Used to determine which order to output event entries in GetAsString. 26enum StringOrder { 27 OLDEST_FIRST, 28 NEWEST_FIRST 29}; 30 31// Used to set the detail level for logging. 32enum LogLevel { 33 LOG_LEVEL_ERROR = 0, 34 LOG_LEVEL_USER = 1, 35 LOG_LEVEL_EVENT = 2, 36 LOG_LEVEL_DEBUG = 3 37}; 38 39// Default log level. 40CHROMEOS_EXPORT extern const LogLevel kDefaultLogLevel; 41 42// Initializes / shuts down network event logging. Calling Initialize more than 43// once will reset the log. 44CHROMEOS_EXPORT void Initialize(); 45CHROMEOS_EXPORT void Shutdown(); 46 47// Returns true if network event logging has been initialized. 48CHROMEOS_EXPORT bool IsInitialized(); 49 50namespace internal { 51 52// Gets the maximum number of log entries. 53CHROMEOS_EXPORT size_t GetMaxLogEntries(); 54 55// Sets the maximum number of entries to something other than the default. If 56// |max_entries| is less than the current maximum number of entries, this will 57// delete any existing entries in excess of |max_entries|. 58CHROMEOS_EXPORT void SetMaxLogEntries(size_t max_entries); 59 60// Adds an entry to the event log at level specified by |log_level|. 61// A maximum number of events are recorded after which new events replace 62// old ones. Error events are prioritized such that error events will only be 63// deleted if more than least half of the entries are errors (at which point 64// the oldest error entry will be replaced). Does nothing unless Initialize() 65// has been called. NOTE: Generally use NET_LOG instead. 66CHROMEOS_EXPORT void AddEntry(const char* file, 67 int file_line, 68 LogLevel log_level, 69 const std::string& event, 70 const std::string& description); 71 72} // namespace internal 73 74// Outputs the log to a formatted string. 75// |order| determines which order to output the events. 76// |format| is a string that determines which elements to show. Elements 77// must be comma-separated, e.g. "time,desc". 78// Note: order of the format strings does not affect the output. 79// "time" - Include a timestamp. 80// "file" - Include file and line number. 81// "desc" - Include the description. 82// "html" - Include html tags. 83// Only events with |log_level| <= |max_level| are included in the output. 84// If |max_events| > 0, limits how many events are output. 85CHROMEOS_EXPORT std::string GetAsString(StringOrder order, 86 const std::string& format, 87 LogLevel max_level, 88 size_t max_events); 89 90// Helper function for displaying a value as a string. 91CHROMEOS_EXPORT std::string ValueAsString(const base::Value& value); 92 93// Errors 94#define NET_LOG_ERROR(event, desc) NET_LOG_LEVEL( \ 95 ::chromeos::network_event_log::LOG_LEVEL_ERROR, event, desc) 96 97// Chrome initiated events, e.g. connection requests, scan requests, 98// configuration removal (either from the UI or from ONC policy application). 99#define NET_LOG_USER(event, desc) NET_LOG_LEVEL( \ 100 ::chromeos::network_event_log::LOG_LEVEL_USER, event, desc) 101 102// Important events, e.g. state updates 103#define NET_LOG_EVENT(event, desc) NET_LOG_LEVEL( \ 104 ::chromeos::network_event_log::LOG_LEVEL_EVENT, event, desc) 105 106// Non-essential debugging events 107#define NET_LOG_DEBUG(event, desc) NET_LOG_LEVEL( \ 108 ::chromeos::network_event_log::LOG_LEVEL_DEBUG, event, desc) 109 110// Macro to include file and line number info in the event log. 111#define NET_LOG_LEVEL(log_level, event, description) \ 112 ::chromeos::network_event_log::internal::AddEntry( \ 113 __FILE__, __LINE__, log_level, event, description) 114 115} // namespace network_event_log 116 117} // namespace chromeos 118 119#endif // CHROMEOS_NETWORK_NETWORK_EVENT_LOG_H_ 120