LogBackend.cxx 4.05 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

20
#include "LogBackend.hxx"
21
#include "Log.hxx"
22
#include "util/Compiler.h"
23
#include "util/Domain.hxx"
24
#include "util/StringStrip.hxx"
25
#include "Version.h"
26
#include "config.h"
27

28 29
#include <cassert>

30 31
#include <stdio.h>
#include <string.h>
32
#include <time.h>
33

34 35 36 37
#ifdef HAVE_SYSLOG
#include <syslog.h>
#endif

38 39
#ifdef ANDROID
#include <android/log.h>
40 41
#include "android/LogListener.hxx"
#include "Main.hxx"
42 43

static int
Max Kellermann's avatar
Max Kellermann committed
44
ToAndroidLogLevel(LogLevel log_level) noexcept
45 46 47 48 49 50
{
	switch (log_level) {
	case LogLevel::DEBUG:
		return ANDROID_LOG_DEBUG;

	case LogLevel::INFO:
51
	case LogLevel::NOTICE:
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
		return ANDROID_LOG_INFO;

	case LogLevel::WARNING:
		return ANDROID_LOG_WARN;

	case LogLevel::ERROR:
		return ANDROID_LOG_ERROR;
	}

	assert(false);
	gcc_unreachable();
}

#else

67
static LogLevel log_threshold = LogLevel::NOTICE;
68

69 70 71 72 73 74 75
static bool enable_timestamp;

#ifdef HAVE_SYSLOG
static bool enable_syslog;
#endif

void
Max Kellermann's avatar
Max Kellermann committed
76
SetLogThreshold(LogLevel _threshold) noexcept
77 78 79 80 81
{
	log_threshold = _threshold;
}

void
Max Kellermann's avatar
Max Kellermann committed
82
EnableLogTimestamp() noexcept
83
{
84 85 86 87 88 89 90 91
#ifdef HAVE_SYSLOG
	assert(!enable_syslog);
#endif
	assert(!enable_timestamp);

	enable_timestamp = true;
}

Max Kellermann's avatar
Max Kellermann committed
92 93
static const char *
log_date() noexcept
94 95 96 97 98 99 100 101 102 103 104 105 106
{
	static constexpr size_t LOG_DATE_BUF_SIZE = 16;
	static char buf[LOG_DATE_BUF_SIZE];
	time_t t = time(nullptr);
	strftime(buf, LOG_DATE_BUF_SIZE, "%b %d %H:%M : ", localtime(&t));
	return buf;
}

/**
 * Determines the length of the string excluding trailing whitespace
 * characters.
 */
static int
107
chomp_length(std::string_view p) noexcept
108
{
109
	return StripRight(p.data(), p.size());
110 111 112 113
}

#ifdef HAVE_SYSLOG

114
[[gnu::const]]
115
static int
Max Kellermann's avatar
Max Kellermann committed
116
ToSysLogLevel(LogLevel log_level) noexcept
117 118
{
	switch (log_level) {
119
	case LogLevel::DEBUG:
120
		return LOG_DEBUG;
121 122

	case LogLevel::INFO:
123
		return LOG_INFO;
124

125
	case LogLevel::NOTICE:
126
		return LOG_NOTICE;
127 128

	case LogLevel::WARNING:
129 130
		return LOG_WARNING;

131
	case LogLevel::ERROR:
132
		return LOG_ERR;
133 134 135 136 137 138
	}

	assert(false);
	gcc_unreachable();
}

139
static void
140
SysLog(const Domain &domain, LogLevel log_level, std::string_view message) noexcept
141 142 143
{
	syslog(ToSysLogLevel(log_level), "%s: %.*s",
	       domain.GetName(),
144
	       chomp_length(message), message.data());
145 146 147
}

void
Max Kellermann's avatar
Max Kellermann committed
148
LogInitSysLog() noexcept
149 150 151 152 153 154
{
	openlog(PACKAGE, 0, LOG_DAEMON);
	enable_syslog = true;
}

void
Max Kellermann's avatar
Max Kellermann committed
155
LogFinishSysLog() noexcept
156 157 158 159 160 161 162 163
{
	if (enable_syslog)
		closelog();
}

#endif

static void
164
FileLog(const Domain &domain, std::string_view message) noexcept
165 166 167 168
{
	fprintf(stderr, "%s%s: %.*s\n",
		enable_timestamp ? log_date() : "",
		domain.GetName(),
169
		chomp_length(message), message.data());
170

171
#ifdef _WIN32
172 173 174 175
	/* force-flush the log file, because setvbuf() does not seem
	   to have an effect on WIN32 */
	fflush(stderr);
#endif
176 177
}

178 179
#endif /* !ANDROID */

180
void
181
Log(LogLevel level, const Domain &domain, std::string_view msg) noexcept
182
{
183 184
#ifdef ANDROID
	__android_log_print(ToAndroidLogLevel(level), "MPD",
185 186
			    "%s: %.*s", domain.GetName(),
			    (int)msg.size(), msg.data());
187 188
	if (logListener != nullptr) {
		char buffer[1024];
189 190
		snprintf(buffer, sizeof(buffer), "%s: %.*s",
			 domain.GetName(), (int)msg.size(), msg.data());
191

192
		logListener->OnLog(Java::GetEnv(), ToAndroidLogLevel(level),
193 194
				   buffer);
	}
195 196
#else

197 198 199 200 201 202 203 204 205 206 207
	if (level < log_threshold)
		return;

#ifdef HAVE_SYSLOG
	if (enable_syslog) {
		SysLog(domain, level, msg);
		return;
	}
#endif

	FileLog(domain, msg);
208
#endif /* !ANDROID */
209
}