File: SimulatorLog.cpp

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 (102 lines) | stat: -rw-r--r-- 2,222 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
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
/* Copyright (c) 2020, Dyssol Development Team. All rights reserved. This file is part of Dyssol. See LICENSE file for license information. */

#include "SimulatorLog.h"
#include <iostream>

CSimulatorLog::CSimulatorLog()
{
	m_log.resize(MAX_LOG_SIZE);
	m_iReadPos = 0;
	m_iWritePos = 0;
}

CSimulatorLog::~CSimulatorLog()
{
}

void CSimulatorLog::Clear()
{
	for(auto l : m_log)
	{
		l.color = ELogColor::DEFAULT;
		l.text.clear();
	}
	m_iReadPos = m_iWritePos = 0;
}

void CSimulatorLog::Write(const std::string& _text, ELogColor _color, bool _console)
{
	const size_t iPos = m_iWritePos % MAX_LOG_SIZE; // up to MAX_LOG_SIZE and then again cyclic from 0
	m_log[iPos].text = _text;
	m_log[iPos].color = _color;

	m_iWritePos++;
	if(_console)
		std::cout << _text << std::endl;
}

void CSimulatorLog::WriteInfo(const std::string& _text, bool _console /*= false*/)
{
	Write(_text, ELogColor::DEFAULT, _console);
}

void CSimulatorLog::WriteWarning(const std::string& _text, bool _console /*= true*/)
{
	Write("Warning! " + _text, ELogColor::ORANGE, _console);
}

void CSimulatorLog::WriteError(const std::string& _text, bool _console /*= true*/)
{
	Write("Error! " + _text, ELogColor::RED, _console);
}

std::string CSimulatorLog::Read()
{
	if (EndOfLog()) return "";
	const size_t iPos = m_iReadPos % MAX_LOG_SIZE; // up to MAX_LOG_SIZE and then again cyclic from 0
	m_iReadPos++;
	return m_log[iPos].text;
}

std::string CSimulatorLog::GetFullLog() const
{
	std::string result;

	// Iterate through all valid entries up to write position
	for (size_t i = 0; i < m_iWritePos && i < MAX_LOG_SIZE; ++i)
	{
		const size_t iPos = i % MAX_LOG_SIZE;
		if (!m_log[iPos].text.empty())
		{
			if (!result.empty())
				result += "\n";

			// Add color-specific prefix
			switch (m_log[iPos].color)
			{
			case ELogColor::RED:
				result += "Error! ";
				break;
			case ELogColor::ORANGE:
				result += "Warning! ";
				break;
			case ELogColor::DEFAULT:
			default:
				break;
			}
			result += m_log[iPos].text;
		}
	}

	return result;
}

CSimulatorLog::ELogColor CSimulatorLog::GetReadColor() const
{
	return m_log[m_iReadPos % MAX_LOG_SIZE].color;
}

bool CSimulatorLog::EndOfLog() const
{
	return m_iReadPos == m_iWritePos;
}