Subversion Repositories HomeAutomation

Rev

Details | 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
{
90
    cout << message;
91
 
92
    myMutex.lock();
93
    if (myLogfile.is_open())
94
    {
95
        myLogfile << message;
96
        myLogfile.flush();
97
    }
98
 
99
    myMutex.unlock();
100
}
101
 
102
string Logger::formatMessage(string message)
103
{
104
    if (message != "\n")
105
    {
106
        return "[" + niceTime() + "] " + message;
107
    }
108
 
109
    return message;
110
}