OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium 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 #include "services/log/log_impl.h" |
| 6 |
| 7 #include <stdio.h> |
| 8 |
| 9 #include "mojo/public/cpp/application/application_connection.h" |
| 10 #include "mojo/public/cpp/bindings/interface_request.h" |
| 11 #include "mojo/public/cpp/system/functions.h" |
| 12 #include "mojo/services/log/interfaces/entry.mojom.h" |
| 13 |
| 14 namespace mojo { |
| 15 namespace log { |
| 16 |
| 17 namespace { |
| 18 std::string LogLevelToString(int32_t log_level) { |
| 19 if (log_level <= kLogLevelVerbose - 3) |
| 20 return "VERBOSE4+"; |
| 21 switch (log_level) { |
| 22 case kLogLevelVerbose - 2: |
| 23 return "VERBOSE3"; |
| 24 case kLogLevelVerbose - 1: |
| 25 return "VERBOSE2"; |
| 26 case kLogLevelVerbose: |
| 27 return "VERBOSE1"; |
| 28 case kLogLevelInfo: |
| 29 return "INFO"; |
| 30 case kLogLevelWarning: |
| 31 return "WARNING"; |
| 32 case kLogLevelError: |
| 33 return "ERROR"; |
| 34 } |
| 35 return "FATAL"; |
| 36 } |
| 37 } // namespace |
| 38 |
| 39 LogImpl::LogImpl(const std::string& remote_url, InterfaceRequest<Log> request) |
| 40 : remote_url_(remote_url), binding_(this, request.Pass()) {} |
| 41 |
| 42 LogImpl::~LogImpl() {} |
| 43 |
| 44 // static |
| 45 void LogImpl::Create(ApplicationConnection* connection, |
| 46 InterfaceRequest<Log> request) { |
| 47 MOJO_DCHECK(connection); |
| 48 const std::string& remote_url = connection->GetRemoteApplicationURL(); |
| 49 if (remote_url.empty()) { |
| 50 MOJO_LOG(ERROR) << "No remote URL"; |
| 51 return; |
| 52 } |
| 53 |
| 54 new LogImpl(remote_url, request.Pass()); |
| 55 } |
| 56 |
| 57 void LogImpl::AddEntry(EntryPtr entry) { |
| 58 MOJO_DCHECK(entry); |
| 59 |
| 60 int64_t server_timestamp = GetTimeTicksNow(); |
| 61 |
| 62 MOJO_DLOG_IF(WARNING, entry->metadata) |
| 63 << "Got AddEntry() with metadata (will discard)"; |
| 64 |
| 65 entry->metadata = EntryMetadata::New(); |
| 66 entry->metadata->server_timestamp = server_timestamp; |
| 67 entry->metadata->source = remote_url_; |
| 68 |
| 69 // In order to keep LogImpl thread-safe (for the future), we should only print |
| 70 // one thing here (otherwise, it could interleave with other prints). |
| 71 fprintf(stderr, "%s\n", FormatEntry(entry).c_str()); |
| 72 } |
| 73 |
| 74 std::string LogImpl::FormatEntry(const EntryPtr& entry) { |
| 75 std::string rv = "<"; |
| 76 rv += remote_url_; |
| 77 rv += "> ["; |
| 78 rv += LogLevelToString(entry->log_level); |
| 79 rv += "] "; |
| 80 if (entry->source_file) { |
| 81 rv += entry->source_file.To<std::string>(); |
| 82 if (entry->source_line) { |
| 83 char line_str[20]; |
| 84 int chars_written = snprintf(line_str, sizeof(line_str), ":%u", |
| 85 static_cast<uint32_t>(entry->source_line)); |
| 86 MOJO_DCHECK(chars_written > 0); |
| 87 MOJO_DCHECK(chars_written <= static_cast<int>(sizeof(line_str) - 1)); |
| 88 |
| 89 rv += line_str; |
| 90 } |
| 91 rv += ": "; |
| 92 } |
| 93 rv += entry->message ? entry->message.To<std::string>() : "<no message>"; |
| 94 return rv; |
| 95 } |
| 96 |
| 97 } // namespace log |
| 98 } // namespace mojo |
OLD | NEW |