blob: 3225526ce001ecd5fbc5c8084d35e040970f9d86 [file] [log] [blame]
dcaiafa25cef112015-04-10 01:07:101// 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 "remoting/host/logging.h"
6
7#include <asl.h>
8#include <sys/types.h>
9#include <unistd.h>
10
11#include "base/logging.h"
12#include "base/scoped_generic.h"
13#include "base/strings/string_number_conversions.h"
14
15namespace remoting {
16
17namespace {
18
19const char kChromotingLoggingFacility[] = "org.chromium.chromoting";
20
21// Define a scoper for objects allocated by asl_new.
22struct ScopedAslMsgTraits {
23 static aslmsg InvalidValue() {
24 return nullptr;
25 }
26 static void Free(aslmsg msg) {
27 asl_free(msg);
28 }
29};
30typedef base::ScopedGeneric<aslmsg, ScopedAslMsgTraits> ScopedAslMsg;
31
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
35bool LogMessageToAsl(
36 logging::LogSeverity severity,
37 const char* file,
38 int line,
39 size_t message_start,
40 const std::string& message) {
41 int level;
42 switch(severity) {
43 case logging::LOG_INFO:
44 level = ASL_LEVEL_NOTICE;
45 break;
46 case logging::LOG_WARNING:
47 level = ASL_LEVEL_WARNING;
48 break;
49 case logging::LOG_ERROR:
dcaiafa25cef112015-04-10 01:07:1050 case logging::LOG_FATAL:
dcaiafadcc7ab62015-06-25 00:19:5151 level = ASL_LEVEL_ERR;
dcaiafa25cef112015-04-10 01:07:1052 break;
53 default:
54 // 'notice' is the lowest priority that the asl libraries will log by
55 // default.
56 level = ASL_LEVEL_NOTICE;
57 break;
58 }
59
60 ScopedAslMsg asl_message(asl_new(ASL_TYPE_MSG));
61 if (!asl_message.is_valid())
62 return false;
63
64 if (asl_set(asl_message.get(), ASL_KEY_FACILITY,
65 kChromotingLoggingFacility) != 0)
66 return false;
67
68 if (asl_set(asl_message.get(), ASL_KEY_LEVEL,
69 base::IntToString(level).c_str()) != 0)
70 return false;
71
72 // Restrict read access to the message to root and the current user.
73 if (asl_set(asl_message.get(), ASL_KEY_READ_UID,
74 base::IntToString(geteuid()).c_str()) != 0)
75 return false;
76
77 if (asl_set(asl_message.get(), ASL_KEY_MSG,
78 message.c_str() + message_start) != 0)
79 return false;
80
81 asl_send(nullptr, asl_message.get());
82
83 // Don't prevent message from being logged by traditional means.
84 return false;
85}
86
87} // namespace
88
89void InitHostLogging() {
90 // Write logs to the system debug log.
91 logging::LoggingSettings settings;
92 settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
93 logging::InitLogging(settings);
94
95 // Write logs to syslog as well.
96 logging::SetLogMessageHandler(LogMessageToAsl);
97}
98
99} // namespace remoting