Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 1 | // 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 Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 10 | #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 Wiley | 5c77efa | 2014-06-19 17:22:41 -0700 | [diff] [blame] | 16 | // |scope| argument to enable logging only if |scope| is enabled. |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 17 | // |
| 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 Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 29 | |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 30 | #define GET_MACRO_OVERLOAD2(arg1, arg2, arg3, macro_name, ...) macro_name |
| 31 | |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 32 | #define SLOG_IS_ON(scope, verbose_level) \ |
| 33 | ::shill::ScopeLogger::GetInstance()->IsLogEnabled( \ |
| 34 | ::shill::ScopeLogger::k##scope, verbose_level) |
| 35 | |
Christopher Wiley | 5c77efa | 2014-06-19 17:22:41 -0700 | [diff] [blame] | 36 | #define SLOG_STREAM(verbose_level) \ |
| 37 | ::logging::LogMessage(__FILE__, __LINE__, -verbose_level).stream() |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 38 | |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 39 | #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 Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 53 | |
| 54 | #define SLOG_IF(scope, verbose_level, condition) \ |
Christopher Wiley | 5c77efa | 2014-06-19 17:22:41 -0700 | [diff] [blame] | 55 | LAZY_STREAM(SLOG_STREAM(verbose_level), \ |
| 56 | SLOG_IS_ON(scope, verbose_level) && (condition)) |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 57 | |
Christopher Wiley | 5c77efa | 2014-06-19 17:22:41 -0700 | [diff] [blame] | 58 | #define SPLOG_STREAM(verbose_level) \ |
| 59 | ::logging::ErrnoLogMessage(__FILE__, __LINE__, -verbose_level, \ |
| 60 | ::logging::GetLastSystemErrorCode()).stream() |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 61 | |
Christopher Wiley | 5c77efa | 2014-06-19 17:22:41 -0700 | [diff] [blame] | 62 | #define SPLOG(scope, verbose_level) \ |
| 63 | LAZY_STREAM(SPLOG_STREAM(verbose_level), SLOG_IS_ON(scope, verbose_level)) |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 64 | |
Christopher Wiley | 5c77efa | 2014-06-19 17:22:41 -0700 | [diff] [blame] | 65 | #define SPLOG_IF(scope, verbose_level, condition) \ |
| 66 | LAZY_STREAM(SPLOG_STREAM(verbose_level), \ |
| 67 | SLOG_IS_ON(scope, verbose_level) && (condition)) |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 68 | |
Prathmesh Prabhu | f002cdb | 2014-09-17 22:41:05 -0700 | [diff] [blame] | 69 | namespace base { |
| 70 | |
| 71 | class CommandLine; |
| 72 | |
| 73 | } // namespace base |
| 74 | |
| 75 | namespace shill { |
| 76 | |
| 77 | namespace 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. |
| 85 | extern const char kLogLevel[]; |
| 86 | // Scopes to enable for SLOG()-based logging. |
| 87 | extern 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. |
| 93 | void SetLogLevelFromCommandLine(base::CommandLine *cl); |
| 94 | |
| 95 | } // namespace shill |
| 96 | |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 97 | #endif // SHILL_LOGGING_H_ |