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 <utility> |
| 10 |
| 11 #include "base/logging.h" |
| 12 #include "base/strings/stringprintf.h" |
| 13 #include "mojo/public/cpp/application/application_connection.h" |
| 14 #include "mojo/public/cpp/system/functions.h" |
| 15 #include "mojo/services/log/interfaces/entry.mojom.h" |
| 16 |
| 17 namespace mojo { |
| 18 namespace log { |
| 19 namespace { |
| 20 |
| 21 std::string LogLevelToString(int32_t log_level) { |
| 22 if (log_level <= kLogLevelVerbose - 3) |
| 23 return "VERBOSE4+"; |
| 24 switch (log_level) { |
| 25 case kLogLevelVerbose - 2: |
| 26 return "VERBOSE3"; |
| 27 case kLogLevelVerbose - 1: |
| 28 return "VERBOSE2"; |
| 29 case kLogLevelVerbose: |
| 30 return "VERBOSE1"; |
| 31 case kLogLevelInfo: |
| 32 return "INFO"; |
| 33 case kLogLevelWarning: |
| 34 return "WARNING"; |
| 35 case kLogLevelError: |
| 36 return "ERROR"; |
| 37 } |
| 38 return "FATAL"; |
| 39 } |
| 40 |
| 41 } // namespace |
| 42 |
| 43 LogImpl::LogImpl(const std::string& remote_url, |
| 44 InterfaceRequest<Log> request, |
| 45 FILE* out_file) |
| 46 : remote_url_(remote_url), |
| 47 binding_(this, std::move(request)), |
| 48 out_file_(out_file) {} |
| 49 |
| 50 LogImpl::~LogImpl() {} |
| 51 |
| 52 // static |
| 53 void LogImpl::Create(ApplicationConnection* connection, |
| 54 InterfaceRequest<Log> request, |
| 55 FILE* out_file) { |
| 56 DCHECK(connection); |
| 57 DCHECK(out_file); |
| 58 |
| 59 const std::string& remote_url = connection->GetRemoteApplicationURL(); |
| 60 if (remote_url.empty()) { |
| 61 LOG(ERROR) << "No remote URL."; |
| 62 return; |
| 63 } |
| 64 |
| 65 new LogImpl(remote_url, std::move(request), out_file); |
| 66 } |
| 67 |
| 68 void LogImpl::AddEntry(EntryPtr entry) { |
| 69 DCHECK(entry); |
| 70 |
| 71 // In order to keep LogImpl thread-safe (for the future), we should only print |
| 72 // one thing here (otherwise, it could interleave with other prints). |
| 73 fprintf(out_file_, "%s\n", FormatEntry(entry).c_str()); |
| 74 } |
| 75 |
| 76 // This should return: |
| 77 // <REMOTE_URL> [LOG_LEVEL] SOURCE_FILE:SOURCE_LINE MESSAGE |
| 78 std::string LogImpl::FormatEntry(const EntryPtr& entry) { |
| 79 std::string source; |
| 80 if (entry->source_file) { |
| 81 source += entry->source_file.To<std::string>(); |
| 82 if (entry->source_line) { |
| 83 base::StringAppendF(&source, ":%u", entry->source_line); |
| 84 } |
| 85 source += ": "; |
| 86 } |
| 87 |
| 88 return base::StringPrintf( |
| 89 "<%s> [%s] %s%s", remote_url_.c_str(), |
| 90 LogLevelToString(entry->log_level).c_str(), source.c_str(), |
| 91 entry->message ? entry->message.To<std::string>().c_str() |
| 92 : "<no message>"); |
| 93 } |
| 94 |
| 95 } // namespace log |
| 96 } // namespace mojo |
OLD | NEW |