Subversion Repositories HomeAutomation

Rev

Rev 981 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 981 Rev 983
1
/***************************************************************************
1
/***************************************************************************
2
 *   Copyright (C) November 27, 2008, 11:57 AM by Mattias Runge            *
2
 *   Copyright (C) November 27, 2008, 11:57 AM by Mattias Runge            *
3
 *   mattias@runge.se                                                      *
3
 *   mattias@runge.se                                                      *
4
 *   syslogstream.cpp                                                      *
4
 *   syslogstream.cpp                                                      *
5
 *                                                                         *
5
 *                                                                         *
6
 *   This program is free software; you can redistribute it and/or modify  *
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  *
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     *
8
 *   the Free Software Foundation; either version 2 of the License, or     *
9
 *   (at your option) any later version.                                   *
9
 *   (at your option) any later version.                                   *
10
 *                                                                         *
10
 *                                                                         *
11
 *   This program is distributed in the hope that it will be useful,       *
11
 *   This program is distributed in the hope that it will be useful,       *
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14
 *   GNU General Public License for more details.                          *
14
 *   GNU General Public License for more details.                          *
15
 *                                                                         *
15
 *                                                                         *
16
 *   You should have received a copy of the GNU General Public License     *
16
 *   You should have received a copy of the GNU General Public License     *
17
 *   along with this program; if not, write to the                         *
17
 *   along with this program; if not, write to the                         *
18
 *   Free Software Foundation, Inc.,                                       *
18
 *   Free Software Foundation, Inc.,                                       *
19
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
20
 ***************************************************************************/
20
 ***************************************************************************/
21
 
21
 
22
#include "syslogstream.h"
22
#include "syslogstream.h"
23
 
23
 
24
SyslogStream* SyslogStream::myInstance = NULL;
24
SyslogStream* SyslogStream::myInstance = NULL;
25
 
25
 
26
SyslogStream& SyslogStream::getInstance()
26
SyslogStream& SyslogStream::getInstance()
27
{
27
{
28
    if (myInstance == NULL)
28
    if (myInstance == NULL)
29
    {
29
    {
30
        myInstance = new SyslogStream();
30
        myInstance = new SyslogStream();
31
    }
31
    }
32
 
32
 
33
    return *myInstance;
33
    return *myInstance;
34
}
34
}
35
 
35
 
36
void SyslogStream::deleteInstance()
36
void SyslogStream::deleteInstance()
37
{
37
{
38
    if (myInstance != NULL)
38
    if (myInstance != NULL)
39
    {
39
    {
40
        delete myInstance;
40
        delete myInstance;
41
        myInstance = NULL;
41
        myInstance = NULL;
42
    }
42
    }
43
}
43
}
44
 
44
 
45
SyslogStream::SyslogStream()
45
SyslogStream::SyslogStream()
46
{
46
{
47
    openlog("Atom", LOG_ODELAY, LOG_DAEMON);
47
    openlog("Atom", LOG_ODELAY, LOG_DAEMON);
48
}
48
}
49
 
49
 
50
SyslogStream::~SyslogStream()
50
SyslogStream::~SyslogStream()
51
{
51
{
52
    closelog();
52
    closelog();
53
}
53
}
54
 
54
 
55
void SyslogStream::add(string str)
55
void SyslogStream::add(string str)
56
{
56
{
57
    syslog(LOG_ODELAY, str.c_str());
57
    //syslog(LOG_ODELAY, str.c_str());
58
 
58
 
59
    if (str == "\n")
59
    if (str == "\n")
60
    {
60
    {
61
        cout << str;
61
        cout << str;
62
    }
62
    }
63
    else
63
    else
64
    {
64
    {
65
        struct tm tmStruct;
65
        struct tm tmStruct;
66
        time_t t = time(NULL);
66
        time_t t = time(NULL);
67
        gmtime_r(&t, &tmStruct);
67
        gmtime_r(&t, &tmStruct);
68
 
68
 
69
        cout << "[" << niceTime() << "] " << str;
69
        cout << "[" << niceTime() << "] " << str;
70
    }
70
    }
71
}
71
}
72
 
72
 
73
SyslogStream& SyslogStream::operator <<(const string& str)
73
SyslogStream& SyslogStream::operator <<(const string& str)
74
{
74
{
75
    add(str);
75
    add(str);
76
}
76
}
77
 
77
 
78
SyslogStream& SyslogStream::operator <<(const char* str)
78
SyslogStream& SyslogStream::operator <<(const char* str)
79
{
79
{
80
    add(str);
80
    add(str);
81
}
81
}
82
 
82
 
83
SyslogStream& SyslogStream::operator <<(const int num)
83
SyslogStream& SyslogStream::operator <<(const int num)
84
{
84
{
85
    add(itos(num));
85
    add(itos(num));
86
}
86
}
87
 
87
 
88
 
88
 
89
 
89