Subversion Repositories HomeAutomation

Rev

Rev 1026 | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
1024 runge 1
/***************************************************************************
2
 *   Copyright (C) January 10, 2009 by Mattias Runge                             *
3
 *   mattias@runge.se                                                      *
4
 *   logger.cpp                                            *
5
 *                                                                         *
6
 *   This program is free software; you can redistribute it and/or modify  *
7
 *   it under the terms of the GNU General Public License as published by  *
8
 *   the Free Software Foundation; either version 2 of the License, or     *
9
 *   (at your option) any later version.                                   *
10
 *                                                                         *
11
 *   This program is distributed in the hope that it will be useful,       *
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14
 *   GNU General Public License for more details.                          *
15
 *                                                                         *
16
 *   You should have received a copy of the GNU General Public License     *
17
 *   along with this program; if not, write to the                         *
18
 *   Free Software Foundation, Inc.,                                       *
19
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
20
 ***************************************************************************/
21
 
22
#include "logger.h"
23
 
24
Logger* Logger::myInstance = NULL;
25
 
26
Logger& Logger::getInstance()
27
{
28
	if (myInstance == NULL)
29
	{
30
		myInstance = new Logger();
31
	}
32
 
33
	return *myInstance;
34
}
35
 
36
void Logger::deleteInstance()
37
{
38
	if (myInstance != NULL)
39
	{
40
		delete myInstance;
41
		myInstance = NULL;
42
	}
43
}
44
 
45
Logger::Logger()
46
{
47
	openlog("Atom", LOG_ODELAY, LOG_DAEMON);
48
}
49
 
50
Logger::~Logger()
51
{
52
	if (myLogfile.is_open())
53
	{
54
		myLogfile << "\n\n";
55
		myLogfile.flush();
56
		myLogfile.close();
57
	}
58
 
59
	closelog();
60
}
61
 
62
bool Logger::open(string filename)
63
{
64
	myLogfile.open(filename.c_str(), ios::app);
65
 
66
	if (myLogfile.is_open())
67
	{
68
		myLogfile.close();
69
		return false;
70
	}
71
 
72
	return true;
73
}
74
 
75
void Logger::addToSyslog(string message)
76
{
77
	mySyslogMutex.lock();
78
	syslog(LOG_ODELAY, message.c_str());
79
	mySyslogMutex.unlock();
80
	add(message);
81
}
82
 
83
void Logger::add(string message)
84
{
85
	addRaw(formatMessage(message));
86
}
87
 
88
void Logger::addRaw(string message)
89
{
1033 runge 90
	myMutex.lock();
91
 
1024 runge 92
	cout << message;
1033 runge 93
 
1024 runge 94
	if (myLogfile.is_open())
95
	{
96
		myLogfile << message;
97
		myLogfile.flush();
98
	}
99
 
100
	myMutex.unlock();
101
}
102
 
103
string Logger::formatMessage(string message)
104
{
105
	if (message != "\n")
106
	{
107
		return "[" + niceTime() + "] " + message;
108
	}
109
 
110
	return message;
111
}