File: LogStreamBuf.cpp

package info (click to toggle)
darkradiant 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 41,080 kB
  • sloc: cpp: 264,743; ansic: 10,659; python: 1,852; xml: 1,650; sh: 92; makefile: 21
file content (82 lines) | stat: -rw-r--r-- 1,480 bytes parent folder | download | duplicates (4)
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
#include "LogStreamBuf.h"

#include <stdio.h>
#include <stdexcept>
#include "LogWriter.h"

namespace applog {

LogStreamBuf::LogStreamBuf(LogLevel level, int bufferSize) :
    _reserve(nullptr),
	_level(level)
{
	if (bufferSize > 0)
    {
		_reserve = new char[bufferSize];
		setp(_reserve, _reserve + bufferSize);
	}
	else
    {
        setp(nullptr, nullptr);
	}

	// No input buffer, set this to NULL
    setg(nullptr, nullptr, nullptr);
}

LogStreamBuf::~LogStreamBuf()
{
	// greebo: Removed this - at destruction time, there is no need
	// to sync with the buffer anymore.
	//sync();

    if (_reserve != nullptr)
    {
		delete[] _reserve;
	}
}

// These two get called by the base class streambuf
LogStreamBuf::int_type LogStreamBuf::overflow(int_type c)
{
	// Write the buffer
	writeToBuffer();

    if (c != traits_type::eof()) 
    {
		if (pbase() == epptr())
        {
			// Write just this single character
			int c1 = c;

			LogWriter::Instance().write(reinterpret_cast<const char*>(&c1), 1, _level);
		}
		else
        {
			sputc(c);
		}
	}

	return 0;
}

LogStreamBuf::int_type LogStreamBuf::sync()
{
	writeToBuffer();
	return 0;
}

void LogStreamBuf::writeToBuffer()
{
	int_type charsToWrite = static_cast<int_type>(pptr() - pbase());

	if (pbase() != pptr()) 
    {
		// Write the given characters to the GtkTextBuffer
		LogWriter::Instance().write(_reserve, static_cast<std::size_t>(charsToWrite), _level);

		setp(pbase(), epptr());
	}
}

} // namespace applog