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.
5 #include "remoting/host/logging.h"
11 #include "base/logging.h"
12 #include "base/scoped_generic.h"
13 #include "base/strings/string_number_conversions.h"
19 const char kChromotingLoggingFacility
[] = "org.chromium.chromoting";
21 // Define a scoper for objects allocated by asl_new.
22 struct ScopedAslMsgTraits
{
23 static aslmsg
InvalidValue() {
26 static void Free(aslmsg msg
) {
30 typedef base::ScopedGeneric
<aslmsg
, ScopedAslMsgTraits
> ScopedAslMsg
;
32 // Logging message handler that writes to syslog.
33 // The log can be obtained by running the following in a terminal:
34 // syslog -k Facility org.chromium.chromoting
36 logging::LogSeverity severity
,
40 const std::string
& message
) {
43 case logging::LOG_INFO
:
44 level
= ASL_LEVEL_NOTICE
;
46 case logging::LOG_WARNING
:
47 level
= ASL_LEVEL_WARNING
;
49 case logging::LOG_ERROR
:
50 level
= ASL_LEVEL_ERR
;
52 case logging::LOG_FATAL
:
53 level
= ASL_LEVEL_EMERG
;
56 // 'notice' is the lowest priority that the asl libraries will log by
58 level
= ASL_LEVEL_NOTICE
;
62 ScopedAslMsg
asl_message(asl_new(ASL_TYPE_MSG
));
63 if (!asl_message
.is_valid())
66 if (asl_set(asl_message
.get(), ASL_KEY_FACILITY
,
67 kChromotingLoggingFacility
) != 0)
70 if (asl_set(asl_message
.get(), ASL_KEY_LEVEL
,
71 base::IntToString(level
).c_str()) != 0)
74 // Restrict read access to the message to root and the current user.
75 if (asl_set(asl_message
.get(), ASL_KEY_READ_UID
,
76 base::IntToString(geteuid()).c_str()) != 0)
79 if (asl_set(asl_message
.get(), ASL_KEY_MSG
,
80 message
.c_str() + message_start
) != 0)
83 asl_send(nullptr, asl_message
.get());
85 // Don't prevent message from being logged by traditional means.
91 void InitHostLogging() {
92 // Write logs to the system debug log.
93 logging::LoggingSettings settings
;
94 settings
.logging_dest
= logging::LOG_TO_SYSTEM_DEBUG_LOG
;
95 logging::InitLogging(settings
);
97 // Write logs to syslog as well.
98 logging::SetLogMessageHandler(LogMessageToAsl
);
101 } // namespace remoting