blob: cf38b705f9c115ff9a4cb081d74db823338ef35e [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
Christopher Wileyb691efd2012-08-09 13:51:51 -070010#include "shill/scope_logger.h"
11
12// How to use:
13//
14// The SLOG macro and its variants are similar to the VLOG macros
15// defined in base/logging.h, except that the SLOG macros take an additional
Christopher Wiley5c77efa2014-06-19 17:22:41 -070016// |scope| argument to enable logging only if |scope| is enabled.
Christopher Wileyb691efd2012-08-09 13:51:51 -070017//
18// Like VLOG, SLOG macros internally map verbosity to LOG severity using
19// negative values, i.e. SLOG(scope, 1) corresponds to LOG(-1).
20//
21// Example usages:
22// SLOG(Service, 1) << "Printed when the 'service' scope is enabled and "
23// "the verbose level is greater than or equal to 1";
24//
25// SLOG_IF(Service, 1, (size > 1024))
26// << "Printed when the 'service' scope is enabled, the verbose level "
27// "is greater than or equal to 1, and size is more than 1024";
28//
Christopher Wileyb691efd2012-08-09 13:51:51 -070029
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070030#define GET_MACRO_OVERLOAD2(arg1, arg2, arg3, macro_name, ...) macro_name
31
Christopher Wileyb691efd2012-08-09 13:51:51 -070032#define SLOG_IS_ON(scope, verbose_level) \
33 ::shill::ScopeLogger::GetInstance()->IsLogEnabled( \
34 ::shill::ScopeLogger::k##scope, verbose_level)
35
Christopher Wiley5c77efa2014-06-19 17:22:41 -070036#define SLOG_STREAM(verbose_level) \
37 ::logging::LogMessage(__FILE__, __LINE__, -verbose_level).stream()
Christopher Wileyb691efd2012-08-09 13:51:51 -070038
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070039#define SLOG_2ARG(object, verbose_level) \
40 LAZY_STREAM(SLOG_STREAM(verbose_level), \
41 ::shill::ScopeLogger::GetInstance()->IsLogEnabled( \
42 Logging::kModuleLogScope, verbose_level)) \
43 << (object ? Logging::ObjectID(object) : "(anon)") << " "
44
45#define SLOG_3ARG(scope, object, verbose_level) \
46 LAZY_STREAM(SLOG_STREAM(verbose_level), \
47 ::shill::ScopeLogger::GetInstance()->IsLogEnabled( \
48 ::shill::ScopeLogger::k##scope, verbose_level)) \
49 << (object ? Logging::ObjectID(object) : "(anon)") << " "
50
51#define SLOG(...) \
52 GET_MACRO_OVERLOAD2(__VA_ARGS__, SLOG_3ARG, SLOG_2ARG)(__VA_ARGS__)
Christopher Wileyb691efd2012-08-09 13:51:51 -070053
54#define SLOG_IF(scope, verbose_level, condition) \
Christopher Wiley5c77efa2014-06-19 17:22:41 -070055 LAZY_STREAM(SLOG_STREAM(verbose_level), \
56 SLOG_IS_ON(scope, verbose_level) && (condition))
Christopher Wileyb691efd2012-08-09 13:51:51 -070057
Christopher Wiley5c77efa2014-06-19 17:22:41 -070058#define SPLOG_STREAM(verbose_level) \
59 ::logging::ErrnoLogMessage(__FILE__, __LINE__, -verbose_level, \
60 ::logging::GetLastSystemErrorCode()).stream()
Christopher Wileyb691efd2012-08-09 13:51:51 -070061
Christopher Wiley5c77efa2014-06-19 17:22:41 -070062#define SPLOG(scope, verbose_level) \
63 LAZY_STREAM(SPLOG_STREAM(verbose_level), SLOG_IS_ON(scope, verbose_level))
Christopher Wileyb691efd2012-08-09 13:51:51 -070064
Christopher Wiley5c77efa2014-06-19 17:22:41 -070065#define SPLOG_IF(scope, verbose_level, condition) \
66 LAZY_STREAM(SPLOG_STREAM(verbose_level), \
67 SLOG_IS_ON(scope, verbose_level) && (condition))
Christopher Wileyb691efd2012-08-09 13:51:51 -070068
Prathmesh Prabhuf002cdb2014-09-17 22:41:05 -070069namespace base {
70
71class CommandLine;
72
73} // namespace base
74
75namespace shill {
76
77namespace switches {
78
79// Command line switches used to setup logging.
80// Clients may use this to display useful help messages.
81
82// Logging level:
83// 0 = LOG(INFO), 1 = LOG(WARNING), 2 = LOG(ERROR),
84// -1 = SLOG(..., 1), -2 = SLOG(..., 2), etc.
85extern const char kLogLevel[];
86// Scopes to enable for SLOG()-based logging.
87extern const char kLogScopes[];
88
89} // namespace switches
90
91// Looks for the command line switches |kLogLevelSwitch| and |kLogScopesSwitch|
92// in |cl| and accordingly sets log scopes and levels.
93void SetLogLevelFromCommandLine(base::CommandLine *cl);
94
95} // namespace shill
96
Christopher Wileyb691efd2012-08-09 13:51:51 -070097#endif // SHILL_LOGGING_H_