summaryrefslogtreecommitdiffstats
path: root/base/include
diff options
context:
space:
mode:
authorDan Albert <danalbert@google.com>2015-03-27 11:20:14 -0700
committerDan Albert <danalbert@google.com>2015-04-03 16:22:39 -0700
commitb547c85b5b9d7fc565e47a5d0c734c3a66af242a (patch)
tree1e9490de3134c6383da548f15186ae9f13ea5b1b /base/include
parentea975880112c27293800ede36e0323ff2a7b9322 (diff)
downloadcore-b547c85b5b9d7fc565e47a5d0c734c3a66af242a.tar.gz
core-b547c85b5b9d7fc565e47a5d0c734c3a66af242a.tar.bz2
core-b547c85b5b9d7fc565e47a5d0c734c3a66af242a.zip
Support arbitrary loggers.
While the defaults (logd or stderr) make sense for most use cases, there are places that can only log to the kernel, or need to log to a file, etc. Allow the user to pass in an arbitrary logging object, and provide LogdLogger and StderrLogger as defaults. Change-Id: I62368acc795ff313242bb205d65017404bf64e88
Diffstat (limited to 'base/include')
-rw-r--r--base/include/base/logging.h60
1 files changed, 45 insertions, 15 deletions
diff --git a/base/include/base/logging.h b/base/include/base/logging.h
index 346099781..19c1b647b 100644
--- a/base/include/base/logging.h
+++ b/base/include/base/logging.h
@@ -17,6 +17,7 @@
#ifndef BASE_LOGGING_H
#define BASE_LOGGING_H
+#include <functional>
#include <memory>
#include <ostream>
@@ -35,10 +36,32 @@ enum LogSeverity {
};
enum LogId {
+ DEFAULT,
MAIN,
SYSTEM,
};
+typedef std::function<void(LogId, LogSeverity, const char*, const char*,
+ unsigned int, const char*)> LogFunction;
+
+extern void StderrLogger(LogId, LogSeverity, const char*, const char*,
+ unsigned int, const char*);
+
+#ifdef __ANDROID__
+// We expose this even though it is the default because a user that wants to
+// override the default log buffer will have to construct this themselves.
+class LogdLogger {
+ public:
+ explicit LogdLogger(LogId default_log_id = android::base::MAIN);
+
+ void operator()(LogId, LogSeverity, const char* tag, const char* file,
+ unsigned int line, const char* message);
+
+ private:
+ LogId default_log_id_;
+};
+#endif
+
// Configure logging based on ANDROID_LOG_TAGS environment variable.
// We need to parse a string that looks like
//
@@ -47,8 +70,15 @@ enum LogId {
// The tag (or '*' for the global level) comes first, followed by a colon and a
// letter indicating the minimum priority level we're expected to log. This can
// be used to reveal or conceal logs with specific tags.
+extern void InitLogging(char* argv[], LogFunction&& logger);
+
+// Configures logging using the default logger (logd for the device, stderr for
+// the host).
extern void InitLogging(char* argv[]);
+// Replace the current logger.
+extern void SetLogger(LogFunction&& logger);
+
// Returns the command line used to invoke the current tool or nullptr if
// InitLogging hasn't been performed.
extern const char* GetCmdLine();
@@ -65,8 +95,8 @@ extern const char* ProgramInvocationShortName();
// FATAL it also causes an abort. For example:
//
// LOG(FATAL) << "We didn't expect to reach here";
-#define LOG(severity) \
- ::android::base::LogMessage(__FILE__, __LINE__, ::android::base::MAIN, \
+#define LOG(severity) \
+ ::android::base::LogMessage(__FILE__, __LINE__, ::android::base::DEFAULT, \
::android::base::severity, -1).stream()
// Logs a message to logcat with the specified log ID on Android otherwise to
@@ -77,8 +107,8 @@ extern const char* ProgramInvocationShortName();
// A variant of LOG that also logs the current errno value. To be used when
// library calls fail.
-#define PLOG(severity) \
- ::android::base::LogMessage(__FILE__, __LINE__, ::android::base::MAIN, \
+#define PLOG(severity) \
+ ::android::base::LogMessage(__FILE__, __LINE__, ::android::base::DEFAULT, \
::android::base::severity, errno).stream()
// Behaves like PLOG, but logs to the specified log ID.
@@ -96,20 +126,20 @@ extern const char* ProgramInvocationShortName();
//
// CHECK(false == true) results in a log message of
// "Check failed: false == true".
-#define CHECK(x) \
- if (UNLIKELY(!(x))) \
- ::android::base::LogMessage(__FILE__, __LINE__, ::android::base::MAIN, \
- ::android::base::FATAL, -1).stream() \
+#define CHECK(x) \
+ if (UNLIKELY(!(x))) \
+ ::android::base::LogMessage(__FILE__, __LINE__, ::android::base::DEFAULT, \
+ ::android::base::FATAL, -1).stream() \
<< "Check failed: " #x << " "
// Helper for CHECK_xx(x,y) macros.
-#define CHECK_OP(LHS, RHS, OP) \
- for (auto _values = ::android::base::MakeEagerEvaluator(LHS, RHS); \
- UNLIKELY(!(_values.lhs OP _values.rhs)); \
- /* empty */) \
- ::android::base::LogMessage(__FILE__, __LINE__, ::android::base::MAIN, \
- ::android::base::FATAL, -1).stream() \
- << "Check failed: " << #LHS << " " << #OP << " " << #RHS \
+#define CHECK_OP(LHS, RHS, OP) \
+ for (auto _values = ::android::base::MakeEagerEvaluator(LHS, RHS); \
+ UNLIKELY(!(_values.lhs OP _values.rhs)); \
+ /* empty */) \
+ ::android::base::LogMessage(__FILE__, __LINE__, ::android::base::DEFAULT, \
+ ::android::base::FATAL, -1).stream() \
+ << "Check failed: " << #LHS << " " << #OP << " " << #RHS \
<< " (" #LHS "=" << _values.lhs << ", " #RHS "=" << _values.rhs << ") "
// Check whether a condition holds between x and y, LOG(FATAL) if not. The value