File: SimulatorLog.h

package info (click to toggle)
dyssol 1.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 18,188 kB
  • sloc: cpp: 53,643; sh: 85; python: 55; makefile: 12
file content (59 lines) | stat: -rw-r--r-- 2,384 bytes parent folder | download | duplicates (2)
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
/* Copyright (c) 2020, Dyssol Development Team. All rights reserved. This file is part of Dyssol. See LICENSE file for license information. */

#pragma once

#include <string>
#include <vector>

/** Describes simulation log. Implements circular storage of messages by maintaining separate read and write positions for message buffering.
 *	Assumes that the distance between read and write positions never will be larger as MAX_LOG_SIZE. */
class CSimulatorLog
{
public:
	enum class ELogColor
	{
		DEFAULT = 0,
		RED = 1,
		ORANGE = 2
	};

private:
	struct SColorLog
	{
		ELogColor color;
		std::string text;
	};

	const size_t MAX_LOG_SIZE = 500;

	std::vector<SColorLog> m_log;
	size_t m_iReadPos;
	size_t m_iWritePos;

public:
	CSimulatorLog();
	~CSimulatorLog();

	// Removes all messages and resets read and write positions.
	void Clear();

	// Writes a message with the specified color to the current write position and advances this position. If _console is set, the message will be additionally written into std::out.
	void Write(const std::string& _text, ELogColor _color, bool _console);
	// Writes an info message with the pre-defined color to the current write position and advances this position. If _console is set, the message will be additionally written into std::out.
	void WriteInfo(const std::string& _text, bool _console = false);
	// Writes a warning message with the pre-defined color to the current write position and advances this position. If _console is set, the message will be additionally written into std::out.
	void WriteWarning(const std::string& _text, bool _console = true);
	// Writes an error message with the pre-defined color to the current write position and advances this position. If _console is set, the message will be additionally written into std::out.
	void WriteError(const std::string& _text, bool _console = true);

	// Returns message from the current read position and advances this position. Returns empty string if the end of log is reached.
	std::string Read();
	// Returns the complete log as a single string, containing all valid messages from start to current write position
	std::string GetFullLog() const;
	// Returns color from the current read position.
	ELogColor GetReadColor() const;

	// Returns true if the end of the log file is reached (the read position is equal to the write position).
	bool EndOfLog() const;
};