LogBackend.cxx 3.87 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 22
#include "Log.hxx"
#include "util/Domain.hxx"
23
#include "util/StringStrip.hxx"
24
#include "Version.h"
25
#include "config.h"
26

27 28
#include <cassert>

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

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

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

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

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

	case LogLevel::WARNING:
		return ANDROID_LOG_WARN;

	case LogLevel::ERROR:
		return ANDROID_LOG_ERROR;
	}

	assert(false);
	gcc_unreachable();
}

#else

66
static LogLevel log_threshold = LogLevel::NOTICE;
67

68 69 70 71 72 73 74
static bool enable_timestamp;

#ifdef HAVE_SYSLOG
static bool enable_syslog;
#endif

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

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

	enable_timestamp = true;
}

Max Kellermann's avatar
Max Kellermann committed
91 92
static const char *
log_date() noexcept
93 94 95 96 97 98 99 100 101 102 103 104 105
{
	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
Max Kellermann's avatar
Max Kellermann committed
106
chomp_length(const char *p) noexcept
107 108
{
	size_t length = strlen(p);
109
	return StripRight(p, length);
110 111 112 113
}

#ifdef HAVE_SYSLOG

Max Kellermann's avatar
Max Kellermann committed
114
gcc_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
Max Kellermann's avatar
Max Kellermann committed
140
SysLog(const Domain &domain, LogLevel log_level, const char *message) noexcept
141 142 143 144 145 146 147
{
	syslog(ToSysLogLevel(log_level), "%s: %.*s",
	       domain.GetName(),
	       chomp_length(message), message);
}

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
Max Kellermann's avatar
Max Kellermann committed
164
FileLog(const Domain &domain, const char *message) noexcept
165 166 167 168 169 170
{
	fprintf(stderr, "%s%s: %.*s\n",
		enable_timestamp ? log_date() : "",
		domain.GetName(),
		chomp_length(message), message);

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, const char *msg) noexcept
182
{
183 184 185
#ifdef ANDROID
	__android_log_print(ToAndroidLogLevel(level), "MPD",
			    "%s: %s", domain.GetName(), msg);
186 187 188
	if (logListener != nullptr)
		logListener->OnLog(Java::GetEnv(), ToAndroidLogLevel(level),
				   "%s: %s", domain.GetName(), msg);
189 190
#else

191 192 193 194 195 196 197 198 199 200 201
	if (level < log_threshold)
		return;

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

	FileLog(domain, msg);
202
#endif /* !ANDROID */
203
}