#include "DebugLog.h"

detail::DebugLog DebugLog;

namespace detail
{
	DebugLog::DebugLog()
	{
		buffer[sizeof(buffer)-1] = 0;
	}

	DebugLog::~DebugLog()
	{
	}

	void DebugLog::append(uint8_t b)
	{
		buffer[end] = b;
		end = (end + 1) % (sizeof(buffer)-1);
		if(end == start) start = (start + 1) % (sizeof(buffer)-1);
	}

	size_t DebugLog::write(uint8_t b)
	{
		append(b);
		return Serial.write(b);
	}

	size_t DebugLog::write(const uint8_t *buffer, size_t size)
	{
		for(size_t i = 0; i < size; ++i)
		{
			append(buffer[i]);
		}
		return Serial.write(buffer, size);
	}

	const char* DebugLog::get(int partIdx)
	{
		buffer[end] = 0;
		if(partIdx == 0)
		{
			return (const char*)&buffer[start];
		}
		else if(partIdx == 1)
		{
			if(end < start)
				return (const char*)&buffer[0];
		}

		return (const char*)&buffer[end];
	}
}
