summaryrefslogtreecommitdiffhomepage
path: root/windows/nsis-plugins/src/log/logger.h
blob: 6847d8a805a073d6706add0b16c3d7b36726b653 (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
#pragma once

#include <string>
#include <vector>
#include <memory>
#include <windows.h>

struct ILogSink
{
	virtual ~ILogSink() = 0
	{
	}

	virtual void log(const std::wstring &message) = 0;
};

class VoidLogSink : public ILogSink
{
public:

	void log(const std::wstring &message) override {}
};

class Utf8FileLogSink : public ILogSink
{
public:

	Utf8FileLogSink(const std::wstring &file, bool append = true, bool flush = false);
	~Utf8FileLogSink();

	Utf8FileLogSink(const Utf8FileLogSink &) = delete;
	Utf8FileLogSink &operator=(const Utf8FileLogSink &) = delete;

	void log(const std::wstring &message) override;

private:

	HANDLE m_logfile = INVALID_HANDLE_VALUE;
	bool m_flush;
};

class Logger
{
public:

	Logger(std::unique_ptr<ILogSink> &&logsink)
		: m_logsink(std::move(logsink))
	{
	}

	Logger(const Logger &) = delete;
	Logger &operator=(const Logger &) = delete;

	void log(const std::wstring &message);
	void log(const std::wstring &message, const std::vector<std::wstring> &details);

private:

	std::unique_ptr<ILogSink> m_logsink;

	static std::wstring Timestamp();

	static std::wstring Compose(const std::wstring &message, const std::wstring &timestamp,
		size_t indentation = 0);
};