summaryrefslogtreecommitdiff
path: root/plugins/!NotAdopted/XSoundNotify/DebugLogger.hpp
blob: 3b2c5a5fc1ff642c97c49cabd7f73d2d7ebaec48 (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
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
#ifndef _GRS_DEBUG_LOGGER_H
#define _GRS_DEBUG_LOGGER_H

#include <fstream>
#include <sstream>
#include <ctime>
#include <memory>

/**
 * Ïðîñòîé ëîããåð, êîòîðûé ìîæíî èñïîëüçîâàòü â ëþáîì ìåñòå äëÿ âûâîäà îòëàäî÷íîé èíôîðìàöèè
 * Ïðè âûâîäå îáû÷íîãî ñîîáùåíèÿ íàäî èñïîëüçîâàòü ìàêðîñ GRS_DEBUG_LOG
 * Äëÿ âûâîäà ñëîæíîãî ñîîáùåíèÿ ñ èñïîëüçîâàíèåì îïåðàòîðîâ ââîäà/âûâîäà ìàêðîñ GRS_DEBUG_FORMAT_LOG
 * Ëîã ñîõðàíÿåòñÿ â ðàáî÷åì êàòàëîãå ïîä èìåíåì : grs_debug.log
 */

namespace grs
{

class DebugLogger
{
public:
	class Except : public std::exception
	{
	public:
		virtual const char * what() const throw() { return "pizda rulu"; }
	};

	DebugLogger()
	{
		try
		{
			_strm.open("D:\\grs_debug.log", std::ios::app);
		}
		catch (...)
		{
			throw Except();
		}
		if (!_strm.is_open())
			throw Except();

		log("Logger started");
	}

	void log(const std::string & str, const char * fileStr = 0, int line = 0)
	{
		if (!_strm.is_open())
			return ;
		
		time_t t(time(0));
		struct tm * timeinfo;
  		timeinfo = localtime (&t);
		char timeStr[9];
		strftime (timeStr, 9, "%H:%M:%S", timeinfo);
		_strm << "[" << timeStr << "] ";
		if (fileStr)
			_strm << fileStr << ":" << line<<" ";
		_strm <<"# "<< str << std::endl;
	}

	static DebugLogger * instance()
	{
		//static DebugLogger * logger = 0;
		static std::auto_ptr<DebugLogger> loggerPtr;
		if (loggerPtr.get() == 0)
			loggerPtr.reset(new DebugLogger());
		return loggerPtr.get();
	}

private:
	std::ofstream _strm;
};

}

#define GRS_DEBUG_FORMAT_LOG(data) {\
		grs::DebugLogger * l = grs::DebugLogger::instance();\
		if (l != 0)	\
		{\
			std::stringstream strm;\
			strm << data;\
			l->log(strm.str(), __FILE__, __LINE__);\
		}\
	}

#define GRS_DEBUG_LOG(data) {\
		grs::DebugLogger * l = grs::DebugLogger::instance();\
		if (l != 0)\
			l->log(data, __FILE__, __LINE__);\
	}

#endif