blob: ddc32c94795dc5f214f195abd81d15fa520e93b3 [file] [log] [blame]
Christopher Wileyb691efd2012-08-09 13:51:51 -07001// Copyright (c) 2012 The Chromium OS 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 SHILL_LOGGING_H_
6#define SHILL_LOGGING_H_
7
8#include <base/logging.h>
9
10#include "shill/memory_log.h"
11#include "shill/scope_logger.h"
12
13// How to use:
14//
15// The SLOG macro and its variants are similar to the VLOG macros
16// defined in base/logging.h, except that the SLOG macros take an additional
17// |scope| argument to enable logging only if |scope| is enabled. In addition,
18// messages sent to SLOG[_IF] are sent to the MemoryLog, whether or not
19// that scope is enabled.
20//
21// Like VLOG, SLOG macros internally map verbosity to LOG severity using
22// negative values, i.e. SLOG(scope, 1) corresponds to LOG(-1).
23//
24// Example usages:
25// SLOG(Service, 1) << "Printed when the 'service' scope is enabled and "
26// "the verbose level is greater than or equal to 1";
27//
28// SLOG_IF(Service, 1, (size > 1024))
29// << "Printed when the 'service' scope is enabled, the verbose level "
30// "is greater than or equal to 1, and size is more than 1024";
31//
32// Similarly, MLOG and MLOG_IF are equivalents to LOG and LOG_IF that send
33// their messages through the MemoryLog on the way to the normal logging system.
34//
35// Example usages:
36// MLOG(ERROR) << "Message logged at ERROR level";
37//
38// MLOG_IF(INFO, tacos < enough) << "Such a sad day";
39//
40//
41
42#define SLOG_IS_ON(scope, verbose_level) \
43 ::shill::ScopeLogger::GetInstance()->IsLogEnabled( \
44 ::shill::ScopeLogger::k##scope, verbose_level)
45
46#define MLOG_SCOPE_STREAM(scope, verbose_level) \
47 ::shill::MemoryLogMessage(__FILE__, __LINE__, \
48 -verbose_level, \
49 SLOG_IS_ON(scope, verbose_level)).stream()
50
51#define MLOG_SEVERITY_STREAM(severity) \
52 ::shill::MemoryLogMessage(__FILE__, __LINE__, \
53 logging::LOG_ ## severity, \
54 LOG_IS_ON(severity)).stream()
55
56#define SLOG(scope, verbose_level) MLOG_SCOPE_STREAM(scope, verbose_level)
57
58#define SLOG_IF(scope, verbose_level, condition) \
59 LAZY_STREAM(MLOG_SCOPE_STREAM(scope, verbose_level), condition)
60
61// TODO(wiley) Add SLOG macros (crosbug.com/33416)
62
Christopher Wileyd7783522012-08-10 14:21:47 -070063// Redefine these macros to route messages through the MemoryLog
64#undef LOG
65#undef LOG_IF
Christopher Wiley6e8c54b2012-08-15 16:12:30 -070066#undef LOG_STREAM
Christopher Wileyb691efd2012-08-09 13:51:51 -070067
Christopher Wileyd7783522012-08-10 14:21:47 -070068#define LOG(severity) MLOG_SEVERITY_STREAM(severity)
69
70#define LOG_IF(severity, condition) \
Christopher Wileyb691efd2012-08-09 13:51:51 -070071 LAZY_STREAM(MLOG_SEVERITY_STREAM(severity), condition)
72
Christopher Wiley6e8c54b2012-08-15 16:12:30 -070073// This lets us catch things like CHECK
74#define LOG_STREAM(severity) MLOG_SEVERITY_STREAM(severity)
75
Christopher Wileyb691efd2012-08-09 13:51:51 -070076// TODO(wiley) Add MPLOG macros (crosbug.com/33416)
77
78#endif // SHILL_LOGGING_H_