summaryrefslogtreecommitdiff
path: root/lib/utils/mu-logger.cc
blob: 8715bffedbb7d4ec4c3af2edece792d94002cee9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
** Copyright (C) 2020-2024 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** 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 3, 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.
**
*/

#include "config.h"
#include "mu-logger.hh"

#define G_LOG_USE_STRUCTURED
#include <glib.h>
#include <glib/gstdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>

#include <iostream>
#include <fstream>
#include <cstring>

#include <thread>
#include <mutex>

using namespace Mu;

static bool			MuLogInitialized = false;
static Mu::Logger::Options	MuLogOptions;
static std::ofstream		MuStream;
static auto			MaxLogFileSize	 = 1000 * 1024;
static std::mutex		logger_mtx;

static std::string MuLogPath;

static bool
maybe_open_logfile()
{
	if (MuStream.is_open())
		return true;

	const auto logdir{to_string_gchar(g_path_get_dirname(MuLogPath.c_str()))};
	if (g_mkdir_with_parents(logdir.c_str(), 0700) != 0) {
		mu_printerrln("creating {} failed: {}", logdir, g_strerror(errno));
		return false;
	}

	MuStream.open(MuLogPath, std::ios::out | std::ios::app);
	if (!MuStream.is_open()) {
		mu_printerrln("opening {} failed: {}", MuLogPath, g_strerror(errno));
		return false;
	}

	MuStream.sync_with_stdio(false);
	return true;
}

static bool
maybe_rotate_logfile()
{
	static unsigned n = 0;

	if (n++ % 1000 != 0)
		return true;

	GStatBuf statbuf;
	if (g_stat(MuLogPath.c_str(), &statbuf) == -1 || statbuf.st_size <= MaxLogFileSize)
		return true;

	const auto old = MuLogPath + ".old";
	g_unlink(old.c_str()); // opportunistic

	if (MuStream.is_open())
		MuStream.close();

	if (g_rename(MuLogPath.c_str(), old.c_str()) != 0)
		mu_printerrln("failed to rename {} -> {}: {}", MuLogPath, old, g_strerror(errno));

	return maybe_open_logfile();
}

static GLogWriterOutput
log_file(GLogLevelFlags level, const GLogField* fields, gsize n_fields, gpointer user_data)
{
	std::lock_guard lock{logger_mtx};

	if (!maybe_open_logfile())
		return G_LOG_WRITER_UNHANDLED;

	char   timebuf[22];
	time_t now{::time(NULL)};
	::strftime(timebuf, sizeof(timebuf), "%F %T", ::localtime(&now));

	char* msg = g_log_writer_format_fields(level, fields, n_fields, FALSE);
	if (msg && msg[0] == '\n') // hmm... seems lines start with '\n'r
		msg[0] = ' ';

	MuStream << timebuf << ' ' << msg << std::endl;

	g_free(msg);

	return maybe_rotate_logfile() ? G_LOG_WRITER_HANDLED : G_LOG_WRITER_UNHANDLED;
}

static GLogWriterOutput
log_stdouterr(GLogLevelFlags level, const GLogField* fields, gsize n_fields, gpointer user_data)
{
	return g_log_writer_standard_streams(level, fields, n_fields, user_data);
}



// log to some logging system; the one that is available & works of journal,
// syslog, file.
static GLogWriterOutput
log_system(GLogLevelFlags level, const GLogField* fields, gsize n_fields, gpointer user_data)
{
	GLogWriterOutput res = G_LOG_WRITER_UNHANDLED;

#ifdef MAYBE_USE_JOURNAL
	res = g_log_writer_journald(level, fields, n_fields, user_data);
	if (res == G_LOG_WRITER_HANDLED)
		return res;
#endif /*MAYBE_USE_JOURNAL*/

#ifdef MAYBE_USE_SYSLOG
	/* since glib 2.80 */
	res = g_log_writer_syslog(level, fields, n_fields, user_data);
	if (res == G_LOG_WRITER_HANDLED)
		return res;
#endif /*MAYBE_USE_SYSLOG*/

	return res = log_file(level, fields, n_fields, user_data);
}


Result<Logger>
Mu::Logger::make(const std::string& path, Mu::Logger::Options opts)
{
	if (MuLogInitialized)
		return Err(Error::Code::Internal, "logging already initialized");

	return Ok(Logger(path, opts));
}

Mu::Logger::Logger(const std::string& path, Mu::Logger::Options opts)
{
	if (g_getenv("MU_LOG_STDOUTERR"))
		opts |= Logger::Options::StdOutErr;

	MuLogOptions = opts;
	MuLogPath    = path;

	g_log_set_writer_func(
	    [](GLogLevelFlags level, const GLogField* fields, gsize n_fields, gpointer user_data) {
		    // filter out debug-level messages?
		    if (level == G_LOG_LEVEL_DEBUG &&
			(none_of(MuLogOptions & Options::Debug)))
			    return G_LOG_WRITER_HANDLED;

		    // log criticals to stdout / err or if asked
		    if (level == G_LOG_LEVEL_CRITICAL ||
			any_of(MuLogOptions & Options::StdOutErr)) {
			    log_stdouterr(level, fields, n_fields, user_data);
		    }

		    // log to the journal, or, if not available to a file.
		    if (any_of(MuLogOptions & Options::File))
			return log_file(level, fields, n_fields, user_data);

		    return log_system(level, fields, n_fields, user_data);
	    },
	    NULL,
	    NULL);

	if (none_of(opts & Options::Quiet))
		g_message("logging initialized; debug: %s, stdout/stderr: %s",
			  any_of(opts & Options::Debug) ? "yes" : "no",
			  any_of(opts & Options::StdOutErr) ? "yes" : "no");

	MuLogInitialized = true;
}

Logger::~Logger()
{
	if (!MuLogInitialized)
		return;

	if (MuStream.is_open())
		MuStream.close();

	MuLogInitialized = false;
}


#ifdef BUILD_TESTS
#include <vector>
#include <atomic>

#include "mu-test-utils.hh"
#include "mu-utils-file.hh"

static void
test_logger_threads(void)
{
	TempDir temp_dir;
	const auto testpath{join_paths(temp_dir.path(), "test.log")};
	mu_message("log-file: {}", testpath);

	auto logger = Logger::make(testpath, Logger::Options::File | Logger::Options::Debug);
	assert_valid_result(logger);

	const auto		thread_num = 16;
	std::atomic<bool>	running	   = true;

	std::vector<std::thread> threads;

	/* log to the logger file from many threass */
	for (auto n = 0; n != thread_num; ++n)
		threads.emplace_back(
			std::thread([&running]{
				while (running) {
					//mu_debug("log message from thread <{}>", n);
					std::this_thread::yield();
				}
			}));

	using namespace std::chrono_literals;
	std::this_thread::sleep_for(1s);
	running = false;

	for (auto n = 0; n != 16; ++n)
		if (threads[n].joinable())
			threads[n].join();
}


int
main(int argc, char* argv[])
{
	mu_test_init(&argc, &argv);

	g_test_add_func("/utils/logger", test_logger_threads);

	return g_test_run();
}

#endif /*BUILD_TESTS*/