Home | History | Annotate | Download | only in shill
      1 //
      2 // Copyright (C) 2012 The Android Open Source Project
      3 //
      4 // Licensed under the Apache License, Version 2.0 (the "License");
      5 // you may not use this file except in compliance with the License.
      6 // You may obtain a copy of the License at
      7 //
      8 //      http://www.apache.org/licenses/LICENSE-2.0
      9 //
     10 // Unless required by applicable law or agreed to in writing, software
     11 // distributed under the License is distributed on an "AS IS" BASIS,
     12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13 // See the License for the specific language governing permissions and
     14 // limitations under the License.
     15 //
     16 
     17 #ifndef SHILL_LOGGING_H_
     18 #define SHILL_LOGGING_H_
     19 
     20 #include <base/logging.h>
     21 
     22 #include "shill/scope_logger.h"
     23 
     24 // How to use:
     25 //
     26 // The SLOG macro and its variants are similar to the VLOG macros
     27 // defined in base/logging.h, except that the SLOG macros take an additional
     28 // |scope| argument to enable logging only if |scope| is enabled.
     29 //
     30 // Like VLOG, SLOG macros internally map verbosity to LOG severity using
     31 // negative values, i.e. SLOG(scope, 1) corresponds to LOG(-1).
     32 //
     33 // Example usages:
     34 //  SLOG(Service, 1) << "Printed when the 'service' scope is enabled and "
     35 //                      "the verbose level is greater than or equal to 1";
     36 //
     37 //  SLOG_IF(Service, 1, (size > 1024))
     38 //      << "Printed when the 'service' scope is enabled, the verbose level "
     39 //         "is greater than or equal to 1, and size is more than 1024";
     40 //
     41 
     42 #define GET_MACRO_OVERLOAD2(arg1, arg2, arg3, macro_name, ...) macro_name
     43 
     44 #define SLOG_IS_ON(scope, verbose_level) \
     45   ::shill::ScopeLogger::GetInstance()->IsLogEnabled( \
     46       ::shill::ScopeLogger::k##scope, verbose_level)
     47 
     48 #define SLOG_STREAM(verbose_level) \
     49   ::logging::LogMessage(__FILE__, __LINE__, -verbose_level).stream()
     50 
     51 #define SLOG_2ARG(object, verbose_level) \
     52   LAZY_STREAM(SLOG_STREAM(verbose_level), \
     53     ::shill::ScopeLogger::GetInstance()->IsLogEnabled( \
     54         Logging::kModuleLogScope, verbose_level)) \
     55   << (object ? Logging::ObjectID(object) : "(anon)") << " "
     56 
     57 #define SLOG_3ARG(scope, object, verbose_level) \
     58   LAZY_STREAM(SLOG_STREAM(verbose_level), \
     59     ::shill::ScopeLogger::GetInstance()->IsLogEnabled( \
     60         ::shill::ScopeLogger::k##scope, verbose_level)) \
     61   << (object ? Logging::ObjectID(object) : "(anon)") << " "
     62 
     63 #define SLOG(...) \
     64   GET_MACRO_OVERLOAD2(__VA_ARGS__, SLOG_3ARG, SLOG_2ARG)(__VA_ARGS__)
     65 
     66 #define SLOG_IF(scope, verbose_level, condition) \
     67   LAZY_STREAM(SLOG_STREAM(verbose_level), \
     68               SLOG_IS_ON(scope, verbose_level) && (condition))
     69 
     70 #define SPLOG_STREAM(verbose_level) \
     71   ::logging::ErrnoLogMessage(__FILE__, __LINE__, -verbose_level, \
     72                              ::logging::GetLastSystemErrorCode()).stream()
     73 
     74 #define SPLOG(scope, verbose_level) \
     75   LAZY_STREAM(SPLOG_STREAM(verbose_level), SLOG_IS_ON(scope, verbose_level))
     76 
     77 #define SPLOG_IF(scope, verbose_level, condition) \
     78   LAZY_STREAM(SPLOG_STREAM(verbose_level), \
     79               SLOG_IS_ON(scope, verbose_level) && (condition))
     80 
     81 namespace base {
     82 
     83 class CommandLine;
     84 
     85 }  // namespace base
     86 
     87 namespace shill {
     88 
     89 namespace switches {
     90 
     91 // Command line switches used to setup logging.
     92 // Clients may use this to display useful help messages.
     93 
     94 // Logging level:
     95 //   0 = LOG(INFO), 1 = LOG(WARNING), 2 = LOG(ERROR),
     96 //   -1 = SLOG(..., 1), -2 = SLOG(..., 2), etc.
     97 extern const char kLogLevel[];
     98 // Scopes to enable for SLOG()-based logging.
     99 extern const char kLogScopes[];
    100 
    101 }  // namespace switches
    102 
    103 // Looks for the command line switches |kLogLevelSwitch| and |kLogScopesSwitch|
    104 // in |cl| and accordingly sets log scopes and levels.
    105 void SetLogLevelFromCommandLine(base::CommandLine* cl);
    106 
    107 }  // namespace shill
    108 
    109 #endif  // SHILL_LOGGING_H_
    110