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