Mojo: Add logging macros (and supporting code) for use in mojo/public/cpp.

The logging macros should be usable by anything that depends on
mojo/public/cpp/environment (i.e., everything except
mojo/public/cpp/{system,utility}).

The macros are a simplified version of the usual ones in base/logging.h,
omitting (D)CHECK_{EQ,NE,GT,...}. Also currently not present are
(D)VLOG, etc.

I'm considering also adding MOJO_(D)CHECK_OK(mojo_result) and
MOJO_(D)LOG_IF_NOT_OK(level, mojo_result), which might be used similarly
to (D)PCHECK and (D)PLOG (for errno/GetLastError()). But I'd do that
separately.

R=darin@chromium.org

Review URL: https://codereview.chromium.org/342653004

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@278504 0039d316-1c4b-4281-b951-d872f2087c98
parent cb440072
...@@ -8,6 +8,9 @@ static_library("chromium") { ...@@ -8,6 +8,9 @@ static_library("chromium") {
sources = [ sources = [
"environment.cc", "environment.cc",
# TODO(vtl): This is kind of ugly. (See TODO in logging.h.)
"../public/cpp/environment/logging.h",
"../public/cpp/environment/lib/logging.cc",
] ]
deps = [ deps = [
......
...@@ -395,6 +395,9 @@ ...@@ -395,6 +395,9 @@
], ],
'sources': [ 'sources': [
'environment/environment.cc', 'environment/environment.cc',
# TODO(vtl): This is kind of ugly. (See TODO in logging.h.)
"public/cpp/environment/logging.h",
"public/cpp/environment/lib/logging.h",
], ],
'include_dirs': [ 'include_dirs': [
'..', '..',
......
...@@ -162,6 +162,7 @@ ...@@ -162,6 +162,7 @@
'sources': [ 'sources': [
'public/cpp/environment/tests/async_waiter_unittest.cc', 'public/cpp/environment/tests/async_waiter_unittest.cc',
'public/cpp/environment/tests/logger_unittest.cc', 'public/cpp/environment/tests/logger_unittest.cc',
'public/cpp/environment/tests/logging_unittest.cc',
], ],
}, },
{ {
...@@ -320,12 +321,14 @@ ...@@ -320,12 +321,14 @@
'sources': [ 'sources': [
'public/c/environment/async_waiter.h', 'public/c/environment/async_waiter.h',
'public/c/environment/logger.h', 'public/c/environment/logger.h',
'public/c/environment/logging.h',
'public/cpp/environment/environment.h', 'public/cpp/environment/environment.h',
'public/cpp/environment/lib/default_async_waiter.cc', 'public/cpp/environment/lib/default_async_waiter.cc',
'public/cpp/environment/lib/default_async_waiter.h', 'public/cpp/environment/lib/default_async_waiter.h',
'public/cpp/environment/lib/default_logger.cc', 'public/cpp/environment/lib/default_logger.cc',
'public/cpp/environment/lib/default_logger.h', 'public/cpp/environment/lib/default_logger.h',
'public/cpp/environment/lib/environment.cc', 'public/cpp/environment/lib/environment.cc',
'public/cpp/environment/lib/logging.cc',
], ],
'include_dirs': [ 'include_dirs': [
'..', '..',
......
...@@ -18,11 +18,15 @@ namespace { ...@@ -18,11 +18,15 @@ namespace {
MojoLogLevel g_minimum_log_level = MOJO_LOG_LEVEL_INFO; MojoLogLevel g_minimum_log_level = MOJO_LOG_LEVEL_INFO;
const char* GetLogLevelString(MojoLogLevel log_level) { const char* GetLogLevelString(MojoLogLevel log_level) {
if (log_level < MOJO_LOG_LEVEL_VERBOSE) if (log_level <= MOJO_LOG_LEVEL_VERBOSE-3)
return "VERBOSE"; return "VERBOSE4+";
switch (log_level) { switch (log_level) {
case MOJO_LOG_LEVEL_VERBOSE-2:
return "VERBOSE3";
case MOJO_LOG_LEVEL_VERBOSE-1:
return "VERBOSE2";
case MOJO_LOG_LEVEL_VERBOSE: case MOJO_LOG_LEVEL_VERBOSE:
return "VERBOSE"; return "VERBOSE1";
case MOJO_LOG_LEVEL_INFO: case MOJO_LOG_LEVEL_INFO:
return "INFO"; return "INFO";
case MOJO_LOG_LEVEL_WARNING: case MOJO_LOG_LEVEL_WARNING:
...@@ -39,7 +43,7 @@ void LogMessage(MojoLogLevel log_level, const char* message) { ...@@ -39,7 +43,7 @@ void LogMessage(MojoLogLevel log_level, const char* message) {
return; return;
// TODO(vtl): Add timestamp also? // TODO(vtl): Add timestamp also?
fprintf(stderr, "%s:%s\n", GetLogLevelString(log_level), message); fprintf(stderr, "%s: %s\n", GetLogLevelString(log_level), message);
if (log_level >= MOJO_LOG_LEVEL_FATAL) if (log_level >= MOJO_LOG_LEVEL_FATAL)
abort(); abort();
} }
......
// Copyright 2014 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.
#include "mojo/public/cpp/environment/logging.h"
#include "mojo/public/cpp/environment/environment.h"
namespace mojo {
namespace internal {
namespace {
// Gets a pointer to the filename portion of |s|. Assumes that the filename
// follows the last slash or backslash in |s|, or is |s| if no slash or
// backslash is present.
//
// E.g., a pointer to "foo.cc" is returned for the following inputs: "foo.cc",
// "./foo.cc", ".\foo.cc", "/absolute/path/to/foo.cc",
// "relative/path/to/foo.cc", "C:\absolute\path\to\foo.cc", etc.
const char* GetFilename(const char* s) {
const char* rv = s;
while (*s) {
if (*s == '/' || *s == '\\')
rv = s + 1;
s++;
}
return rv;
}
} // namespace
LogMessage::LogMessage(const char* file, int line, MojoLogLevel log_level)
: log_level_(log_level) {
// Note: Don't include the log level in the message, since that's passed on.
stream_ << GetFilename(file) << '(' << line << "): ";
}
LogMessage::~LogMessage() {
Environment::GetDefaultLogger()->LogMessage(log_level_,
stream_.str().c_str());
}
} // namespace internal
} // namespace mojo
// Copyright 2014 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.
// Logging macros, similar to Chromium's base/logging.h, except with |MOJO_|
// prefixes and missing some features (notably |CHECK_EQ()|, etc.).
// TODO(vtl): It's weird that this is in the environment directory, since its
// implementation (in environment/lib) is meant to be used by any implementation
// of the environment.
#ifndef MOJO_PUBLIC_CPP_ENVIRONMENT_LOGGING_H_
#define MOJO_PUBLIC_CPP_ENVIRONMENT_LOGGING_H_
#include <sstream>
#include "mojo/public/c/environment/logger.h"
#include "mojo/public/cpp/environment/environment.h"
#include "mojo/public/cpp/system/macros.h"
#define MOJO_LOG_STREAM(level) \
::mojo::internal::LogMessage(__FILE__, __LINE__, \
MOJO_LOG_LEVEL_ ## level).stream()
#define MOJO_LAZY_LOG_STREAM(level, condition) \
!(condition) ? \
(void) 0 : \
::mojo::internal::VoidifyOstream() & MOJO_LOG_STREAM(level)
#define MOJO_SHOULD_LOG(level) \
(MOJO_LOG_LEVEL_ ## level >= \
::mojo::Environment::GetDefaultLogger()->GetMinimumLogLevel())
#define MOJO_LOG(level) \
MOJO_LAZY_LOG_STREAM(level, MOJO_SHOULD_LOG(level))
#define MOJO_LOG_IF(level, condition) \
MOJO_LAZY_LOG_STREAM(level, MOJO_SHOULD_LOG(level) && (condition))
#define MOJO_CHECK(condition) \
MOJO_LAZY_LOG_STREAM(FATAL, !(condition)) \
<< "Check failed: " #condition ". "
// Note: For non-debug builds, |MOJO_DLOG_IF()| *eliminates* (i.e., doesn't
// compile) the condition, whereas |MOJO_DCHECK()| "neuters" the condition
// (i.e., compiles, but doesn't evaluate).
#ifdef NDEBUG
#define MOJO_DLOG(level) MOJO_LAZY_LOG_STREAM(level, false)
#define MOJO_DLOG_IF(level, condition) MOJO_LAZY_LOG_STREAM(level, false)
#define MOJO_DCHECK(condition) MOJO_LAZY_LOG_STREAM(FATAL, false && (condition))
#else
#define MOJO_DLOG(level) MOJO_LOG(level)
#define MOJO_DLOG_IF(level, condition) MOJO_LOG_IF(level, condition)
#define MOJO_DCHECK(condition) MOJO_CHECK(condition)
#endif // NDEBUG
namespace mojo {
namespace internal {
class LogMessage {
public:
LogMessage(const char* file, int line, MojoLogLevel log_level);
~LogMessage();
std::ostream& stream() { return stream_; }
private:
const MojoLogLevel log_level_;
std::ostringstream stream_;
MOJO_DISALLOW_COPY_AND_ASSIGN(LogMessage);
};
// Used to ignore a stream.
struct VoidifyOstream {
// Use & since it has precedence lower than << but higher than ?:.
void operator&(std::ostream&) {}
};
} // namespace internal
} // namespace mojo
#endif // MOJO_PUBLIC_CPP_ENVIRONMENT_LOGGING_H_
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment