summaryrefslogtreecommitdiffstats
path: root/chromeos/network/network_event_log.h
blob: 293144c5931d3617af074b234ba8f4fb0cecffa3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROMEOS_NETWORK_NETWORK_EVENT_LOG_H_
#define CHROMEOS_NETWORK_NETWORK_EVENT_LOG_H_

#include <deque>
#include <string>

#include "base/basictypes.h"
#include "base/stringprintf.h"
#include "base/time.h"
#include "chromeos/chromeos_export.h"

namespace chromeos {

// Namespace for functions for logging network events.
namespace network_event_log {

// Used to determine which order to output event entries in GetAsString.
enum StringOrder {
  OLDEST_FIRST,
  NEWEST_FIRST
};

// Maximum number of event log entries, exported for testing.
CHROMEOS_EXPORT extern const size_t kMaxNetworkEventLogEntries;

// Initializes / shuts down network event logging. Calling Initialize more than
// once will reset the log.
CHROMEOS_EXPORT void Initialize();
CHROMEOS_EXPORT void Shutdown();

// Returns true if network event logging has been initialized.
CHROMEOS_EXPORT bool IsInitialized();

// Adds an entry to the event log. A maximum number of events are recorded
// after which new events replace old ones. Does nothing unless Initialize()
// has been called.
CHROMEOS_EXPORT void AddEntry(const std::string& module,
                              const std::string& event,
                              const std::string& description);

// Outputs the log to a formatted string. |order| determines which order to
// output the events. If |max_events| > 0, limits how many events are output.
CHROMEOS_EXPORT std::string GetAsString(StringOrder order, size_t max_events);

// Macros to make logging format more consistent.
#define NET_LOG(module, message)                                             \
    ::chromeos::network_event_log::AddEntry(                                 \
        module,                                                              \
        std::string(__FILE__) + ":" + ::base::StringPrintf("%d",__LINE__) +  \
        " (" + std::string(__func__) + ")",                                  \
        message)

#define NET_LOG_WARNING(module, message)                                     \
    NET_LOG(module, std::string("WARNING:") + message)
#define NET_LOG_ERROR(module, message)                                       \
    NET_LOG(module, std::string("ERROR:") + message)

}  // namespace network_event_log

}  // namespace chromeos

#endif  // CHROMEOS_NETWORK_NETWORK_EVENT_LOG_H_