| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "services/log/log_impl.h" | 5 #include "services/log/log_impl.h" |
| 6 | 6 |
| 7 #include <stdio.h> | |
| 8 | |
| 9 #include <utility> | 7 #include <utility> |
| 10 | 8 |
| 11 #include "base/logging.h" | 9 #include "base/logging.h" |
| 12 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| 13 #include "mojo/public/cpp/application/application_connection.h" | 11 #include "mojo/public/cpp/application/application_connection.h" |
| 14 #include "mojo/services/log/interfaces/entry.mojom.h" | 12 #include "mojo/services/log/interfaces/entry.mojom.h" |
| 15 | 13 |
| 16 namespace mojo { | 14 namespace mojo { |
| 17 namespace log { | 15 namespace log { |
| 18 namespace { | 16 namespace { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 34 case kLogLevelError: | 32 case kLogLevelError: |
| 35 return "ERROR"; | 33 return "ERROR"; |
| 36 } | 34 } |
| 37 return "FATAL"; | 35 return "FATAL"; |
| 38 } | 36 } |
| 39 | 37 |
| 40 } // namespace | 38 } // namespace |
| 41 | 39 |
| 42 LogImpl::LogImpl(const std::string& remote_url, | 40 LogImpl::LogImpl(const std::string& remote_url, |
| 43 InterfaceRequest<Log> request, | 41 InterfaceRequest<Log> request, |
| 44 FILE* out_file) | 42 PrintLogMessageFunction print_log_message_function) |
| 45 : remote_url_(remote_url), | 43 : remote_url_(remote_url), |
| 46 binding_(this, std::move(request)), | 44 binding_(this, std::move(request)), |
| 47 out_file_(out_file) {} | 45 print_log_message_function_(print_log_message_function) {} |
| 48 | 46 |
| 49 LogImpl::~LogImpl() {} | 47 LogImpl::~LogImpl() {} |
| 50 | 48 |
| 51 // static | 49 // static |
| 52 void LogImpl::Create(ApplicationConnection* connection, | 50 void LogImpl::Create(ApplicationConnection* connection, |
| 53 InterfaceRequest<Log> request, | 51 InterfaceRequest<Log> request, |
| 54 FILE* out_file) { | 52 PrintLogMessageFunction print_log_message_function) { |
| 55 DCHECK(connection); | 53 DCHECK(connection); |
| 56 DCHECK(out_file); | 54 DCHECK(print_log_message_function); |
| 57 | 55 |
| 58 const std::string& remote_url = connection->GetRemoteApplicationURL(); | 56 const std::string& remote_url = connection->GetRemoteApplicationURL(); |
| 59 if (remote_url.empty()) { | 57 if (remote_url.empty()) { |
| 60 LOG(ERROR) << "No remote URL."; | 58 LOG(ERROR) << "No remote URL."; |
| 61 return; | 59 return; |
| 62 } | 60 } |
| 63 | 61 |
| 64 new LogImpl(remote_url, std::move(request), out_file); | 62 new LogImpl(remote_url, std::move(request), |
| 63 std::move(print_log_message_function)); |
| 65 } | 64 } |
| 66 | 65 |
| 67 void LogImpl::AddEntry(EntryPtr entry) { | 66 void LogImpl::AddEntry(EntryPtr entry) { |
| 68 DCHECK(entry); | 67 DCHECK(entry); |
| 69 | 68 print_log_message_function_(FormatEntry(entry)); |
| 70 // In order to keep LogImpl thread-safe (for the future), we should only print | |
| 71 // one thing here (otherwise, it could interleave with other prints). | |
| 72 fprintf(out_file_, "%s\n", FormatEntry(entry).c_str()); | |
| 73 } | 69 } |
| 74 | 70 |
| 75 // This should return: | 71 // This should return: |
| 76 // <REMOTE_URL> [LOG_LEVEL] SOURCE_FILE:SOURCE_LINE MESSAGE | 72 // <REMOTE_URL> [LOG_LEVEL] SOURCE_FILE:SOURCE_LINE MESSAGE |
| 77 std::string LogImpl::FormatEntry(const EntryPtr& entry) { | 73 std::string LogImpl::FormatEntry(const EntryPtr& entry) { |
| 78 std::string source; | 74 std::string source; |
| 79 if (entry->source_file) { | 75 if (entry->source_file) { |
| 80 source += entry->source_file.To<std::string>(); | 76 source += entry->source_file.To<std::string>(); |
| 81 if (entry->source_line) { | 77 if (entry->source_line) { |
| 82 base::StringAppendF(&source, ":%u", entry->source_line); | 78 base::StringAppendF(&source, ":%u", entry->source_line); |
| 83 } | 79 } |
| 84 source += ": "; | 80 source += ": "; |
| 85 } | 81 } |
| 86 | 82 |
| 87 return base::StringPrintf( | 83 return base::StringPrintf( |
| 88 "<%s> [%s] %s%s", remote_url_.c_str(), | 84 "<%s> [%s] %s%s", remote_url_.c_str(), |
| 89 LogLevelToString(entry->log_level).c_str(), source.c_str(), | 85 LogLevelToString(entry->log_level).c_str(), source.c_str(), |
| 90 entry->message ? entry->message.To<std::string>().c_str() | 86 entry->message ? entry->message.To<std::string>().c_str() |
| 91 : "<no message>"); | 87 : "<no message>"); |
| 92 } | 88 } |
| 93 | 89 |
| 94 } // namespace log | 90 } // namespace log |
| 95 } // namespace mojo | 91 } // namespace mojo |
| OLD | NEW |