summaryrefslogtreecommitdiffhomepage
path: root/windows/nsis-plugins/src/log/logger.h
blob: 71054d0bdc4a377669297642ac8732a0af4ff8bb (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
#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 AnsiFileLogSink : public ILogSink
{
public:

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

	AnsiFileLogSink(const AnsiFileLogSink &) = delete;
	AnsiFileLogSink &operator=(const AnsiFileLogSink &) = 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;

	size_t m_ordinal = 1;

	static std::wstring Timestamp();

	std::wstring ordinal();

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