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" | |
viettrungluu
2015/11/20 23:21:51
Probably don't need this include (already included
vardhan
2015/12/02 00:06:14
Done.
| |
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 { | |
viettrungluu
2015/11/20 23:21:51
blank line after
vardhan
2015/12/02 00:06:14
Done.
| |
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 | |
viettrungluu
2015/11/20 23:21:51
blank line before
vardhan
2015/12/02 00:06:14
Done.
| |
38 | |
39 LogImpl::LogImpl(const std::string& remote_url, InterfaceRequest<Log> request) | |
40 : remote_url_(remote_url), binding_(this, request.Pass()) {} | |
viettrungluu
2015/11/20 23:21:51
nit: should probably use std::move instead of .Pas
vardhan
2015/12/02 00:06:14
Done.
| |
41 | |
42 LogImpl::~LogImpl() {} | |
43 | |
44 // static | |
45 void LogImpl::Create(ApplicationConnection* connection, | |
46 InterfaceRequest<Log> request) { | |
47 MOJO_DCHECK(connection); | |
viettrungluu
2015/11/20 23:21:51
Probably need to include some header for MOJO_DCHE
vardhan
2015/12/02 00:06:14
Done. do you recommend using DCHECK or MOJO_DCHEC
viettrungluu
2015/12/02 00:08:01
Probably, if you're using //base anyway, you shoul
| |
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 // In order to keep LogImpl thread-safe (for the future), we should only print | |
61 // one thing here (otherwise, it could interleave with other prints). | |
62 fprintf(stderr, "%s\n", FormatEntry(entry).c_str()); | |
63 } | |
64 | |
65 std::string LogImpl::FormatEntry(const EntryPtr& entry) { | |
66 std::string rv = "<"; | |
viettrungluu
2015/11/20 23:21:51
This would all be more readable if you just used S
vardhan
2015/12/02 00:06:14
Done.
| |
67 rv += remote_url_; | |
68 rv += "> ["; | |
69 rv += LogLevelToString(entry->log_level); | |
70 rv += "] "; | |
71 if (entry->source_file) { | |
72 rv += entry->source_file.To<std::string>(); | |
73 if (entry->source_line) { | |
74 char line_str[20]; | |
75 int chars_written = snprintf(line_str, sizeof(line_str), ":%u", | |
76 static_cast<uint32_t>(entry->source_line)); | |
77 MOJO_DCHECK(chars_written > 0); | |
78 MOJO_DCHECK(chars_written <= static_cast<int>(sizeof(line_str) - 1)); | |
79 | |
80 rv += line_str; | |
81 } | |
82 rv += ": "; | |
83 } | |
84 rv += entry->message ? entry->message.To<std::string>() : "<no message>"; | |
85 return rv; | |
86 } | |
87 | |
88 } // namespace log | |
89 } // namespace mojo | |
OLD | NEW |