Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(797)

Side by Side Diff: remoting/host/logging_mac.cc

Issue 2034393004: Allow multiple logging::LogMessage{Handler,Listener}s Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address grt's comments Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "remoting/host/logging.h" 5 #include "remoting/host/logging.h"
6 6
7 #include <asl.h> 7 #include <asl.h>
8 #include <stddef.h> 8 #include <stddef.h>
9 #include <sys/types.h> 9 #include <sys/types.h>
10 #include <unistd.h> 10 #include <unistd.h>
(...skipping 15 matching lines...) Expand all
26 } 26 }
27 static void Free(aslmsg msg) { 27 static void Free(aslmsg msg) {
28 asl_free(msg); 28 asl_free(msg);
29 } 29 }
30 }; 30 };
31 typedef base::ScopedGeneric<aslmsg, ScopedAslMsgTraits> ScopedAslMsg; 31 typedef base::ScopedGeneric<aslmsg, ScopedAslMsgTraits> ScopedAslMsg;
32 32
33 // Logging message handler that writes to syslog. 33 // Logging message handler that writes to syslog.
34 // The log can be obtained by running the following in a terminal: 34 // The log can be obtained by running the following in a terminal:
35 // syslog -k Facility org.chromium.chromoting 35 // syslog -k Facility org.chromium.chromoting
36 bool LogMessageToAsl( 36 class LogMessageListener : logging::LogMessageListener {
37 void OnMessage(
38 logging::LogSeverity severity,
39 const char* file,
40 int line,
41 size_t message_start,
42 const std::string& message) override;
43 };
44
45 void LogMessageListener::OnMessage(
37 logging::LogSeverity severity, 46 logging::LogSeverity severity,
38 const char* file, 47 const char* file,
39 int line, 48 int line,
40 size_t message_start, 49 size_t message_start,
41 const std::string& message) { 50 const std::string& message) {
42 int level; 51 int level;
43 switch(severity) { 52 switch(severity) {
44 case logging::LOG_INFO: 53 case logging::LOG_INFO:
45 level = ASL_LEVEL_NOTICE; 54 level = ASL_LEVEL_NOTICE;
46 break; 55 break;
47 case logging::LOG_WARNING: 56 case logging::LOG_WARNING:
48 level = ASL_LEVEL_WARNING; 57 level = ASL_LEVEL_WARNING;
49 break; 58 break;
50 case logging::LOG_ERROR: 59 case logging::LOG_ERROR:
51 case logging::LOG_FATAL: 60 case logging::LOG_FATAL:
52 level = ASL_LEVEL_ERR; 61 level = ASL_LEVEL_ERR;
53 break; 62 break;
54 default: 63 default:
55 // 'notice' is the lowest priority that the asl libraries will log by 64 // 'notice' is the lowest priority that the asl libraries will log by
56 // default. 65 // default.
57 level = ASL_LEVEL_NOTICE; 66 level = ASL_LEVEL_NOTICE;
58 break; 67 break;
59 } 68 }
60 69
61 ScopedAslMsg asl_message(asl_new(ASL_TYPE_MSG)); 70 ScopedAslMsg asl_message(asl_new(ASL_TYPE_MSG));
62 if (!asl_message.is_valid()) 71 if (!asl_message.is_valid())
63 return false; 72 return;
64 73
65 if (asl_set(asl_message.get(), ASL_KEY_FACILITY, 74 if (asl_set(asl_message.get(), ASL_KEY_FACILITY,
66 kChromotingLoggingFacility) != 0) 75 kChromotingLoggingFacility) != 0)
67 return false; 76 return;
68 77
69 if (asl_set(asl_message.get(), ASL_KEY_LEVEL, 78 if (asl_set(asl_message.get(), ASL_KEY_LEVEL,
70 base::IntToString(level).c_str()) != 0) 79 base::IntToString(level).c_str()) != 0)
71 return false; 80 return;
72 81
73 // Restrict read access to the message to root and the current user. 82 // Restrict read access to the message to root and the current user.
74 if (asl_set(asl_message.get(), ASL_KEY_READ_UID, 83 if (asl_set(asl_message.get(), ASL_KEY_READ_UID,
75 base::IntToString(geteuid()).c_str()) != 0) 84 base::IntToString(geteuid()).c_str()) != 0)
76 return false; 85 return;
77 86
78 if (asl_set(asl_message.get(), ASL_KEY_MSG, 87 if (asl_set(asl_message.get(), ASL_KEY_MSG,
79 message.c_str() + message_start) != 0) 88 message.c_str() + message_start) != 0)
80 return false; 89 return;
81 90
82 asl_send(nullptr, asl_message.get()); 91 asl_send(nullptr, asl_message.get());
83 92
84 // Don't prevent message from being logged by traditional means. 93 // Don't prevent message from being logged by traditional means.
85 return false;
86 } 94 }
87 95
88 } // namespace 96 } // namespace
89 97
90 void InitHostLogging() { 98 void InitHostLogging() {
91 // Write logs to the system debug log. 99 // Write logs to the system debug log.
92 logging::LoggingSettings settings; 100 logging::LoggingSettings settings;
93 settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG; 101 settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
94 logging::InitLogging(settings); 102 logging::InitLogging(settings);
95 103
96 // Write logs to syslog as well. 104 // Write logs to syslog as well.
97 logging::SetLogMessageHandler(LogMessageToAsl); 105 // Intentionally leak the listener
106 auto* listener = new LogMessageListener();
107 CHECK(listener);
98 } 108 }
99 109
100 } // namespace remoting 110 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698