summaryrefslogtreecommitdiffstats
path: root/mmc_updater/src/Log.cpp
blob: d4e5a214005f09c06d3cfdbf17a92fcb561c6413 (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
63
64
65
#include "Log.h"

#include "Platform.h"
#include "StringUtils.h"
#include "ProcessUtils.h"

#include <string.h>
#include <iostream>

Log m_globalLog;

Log* Log::instance()
{
	return &m_globalLog;
}

Log::Log()
{
}

Log::~Log()
{
}

void Log::open(const std::string& path)
{
	m_mutex.lock();
	m_output.open(path.c_str(),std::ios_base::out | std::ios_base::app);
	m_mutex.unlock();
}

void Log::writeToStream(std::ostream& stream, Type type, const char* text)
{
	// Multiple processes may be writing to the same log file during
	// an update.  No attempt is made to synchronize access to the file.
	//
	// Under Unix, appends to a single file on a local FS by multiple writers should be atomic
	// provided that the length of 'text' is less than PIPE_BUF
	//
	switch (type)
	{
		case Info:
			stream << "INFO  ";
			break;
		case Warn:
			stream << "WARN  ";
			break;
		case Error:
			stream << "ERROR ";
			break;
	}
	stream << '(' << intToStr(ProcessUtils::currentProcessId()) << ") " << text << std::endl;
}

void Log::write(Type type, const char* text)
{
	m_mutex.lock();
	writeToStream(std::cerr,type,text);
	if (m_output.is_open())
	{
		writeToStream(m_output,type,text);
	}
	m_mutex.unlock();
}