| Index: services/log/log_impl.cc
|
| diff --git a/services/log/log_impl.cc b/services/log/log_impl.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..23ba6d209afc2f18d0d79e14bc590b8522646640
|
| --- /dev/null
|
| +++ b/services/log/log_impl.cc
|
| @@ -0,0 +1,98 @@
|
| +// Copyright 2015 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 "services/log/log_impl.h"
|
| +
|
| +#include <stdio.h>
|
| +
|
| +#include "mojo/public/cpp/application/application_connection.h"
|
| +#include "mojo/public/cpp/bindings/interface_request.h"
|
| +#include "mojo/public/cpp/system/functions.h"
|
| +#include "mojo/services/log/interfaces/entry.mojom.h"
|
| +
|
| +namespace mojo {
|
| +namespace log {
|
| +
|
| +namespace {
|
| +std::string LogLevelToString(int32_t log_level) {
|
| + if (log_level <= kLogLevelVerbose - 3)
|
| + return "VERBOSE4+";
|
| + switch (log_level) {
|
| + case kLogLevelVerbose - 2:
|
| + return "VERBOSE3";
|
| + case kLogLevelVerbose - 1:
|
| + return "VERBOSE2";
|
| + case kLogLevelVerbose:
|
| + return "VERBOSE1";
|
| + case kLogLevelInfo:
|
| + return "INFO";
|
| + case kLogLevelWarning:
|
| + return "WARNING";
|
| + case kLogLevelError:
|
| + return "ERROR";
|
| + }
|
| + return "FATAL";
|
| +}
|
| +} // namespace
|
| +
|
| +LogImpl::LogImpl(const std::string& remote_url, InterfaceRequest<Log> request)
|
| + : remote_url_(remote_url), binding_(this, request.Pass()) {}
|
| +
|
| +LogImpl::~LogImpl() {}
|
| +
|
| +// static
|
| +void LogImpl::Create(ApplicationConnection* connection,
|
| + InterfaceRequest<Log> request) {
|
| + MOJO_DCHECK(connection);
|
| + const std::string& remote_url = connection->GetRemoteApplicationURL();
|
| + if (remote_url.empty()) {
|
| + MOJO_LOG(ERROR) << "No remote URL";
|
| + return;
|
| + }
|
| +
|
| + new LogImpl(remote_url, request.Pass());
|
| +}
|
| +
|
| +void LogImpl::AddEntry(EntryPtr entry) {
|
| + MOJO_DCHECK(entry);
|
| +
|
| + int64_t server_timestamp = GetTimeTicksNow();
|
| +
|
| + MOJO_DLOG_IF(WARNING, entry->metadata)
|
| + << "Got AddEntry() with metadata (will discard)";
|
| +
|
| + entry->metadata = EntryMetadata::New();
|
| + entry->metadata->server_timestamp = server_timestamp;
|
| + entry->metadata->source = remote_url_;
|
| +
|
| + // In order to keep LogImpl thread-safe (for the future), we should only print
|
| + // one thing here (otherwise, it could interleave with other prints).
|
| + fprintf(stderr, "%s\n", FormatEntry(entry).c_str());
|
| +}
|
| +
|
| +std::string LogImpl::FormatEntry(const EntryPtr& entry) {
|
| + std::string rv = "<";
|
| + rv += remote_url_;
|
| + rv += "> [";
|
| + rv += LogLevelToString(entry->log_level);
|
| + rv += "] ";
|
| + if (entry->source_file) {
|
| + rv += entry->source_file.To<std::string>();
|
| + if (entry->source_line) {
|
| + char line_str[20];
|
| + int chars_written = snprintf(line_str, sizeof(line_str), ":%u",
|
| + static_cast<uint32_t>(entry->source_line));
|
| + MOJO_DCHECK(chars_written > 0);
|
| + MOJO_DCHECK(chars_written <= static_cast<int>(sizeof(line_str) - 1));
|
| +
|
| + rv += line_str;
|
| + }
|
| + rv += ": ";
|
| + }
|
| + rv += entry->message ? entry->message.To<std::string>() : "<no message>";
|
| + return rv;
|
| +}
|
| +
|
| +} // namespace log
|
| +} // namespace mojo
|
|
|