Subversion Repositories HomeAutomation

Rev

Rev 1603 | Rev 1939 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1603 Rev 1657
1
/*
1
/*
2
 *
2
 *
3
 *  Copyright (C) 2010  Mattias Runge
3
 *  Copyright (C) 2010  Mattias Runge
4
 *
4
 *
5
 *  This program is free software; you can redistribute it and/or modify
5
 *  This program is free software; you can redistribute it and/or modify
6
 *  it under the terms of the GNU General Public License as published by
6
 *  it under the terms of the GNU General Public License as published by
7
 *  the Free Software Foundation; either version 2 of the License, or
7
 *  the Free Software Foundation; either version 2 of the License, or
8
 *  (at your option) any later version.
8
 *  (at your option) any later version.
9
 *
9
 *
10
 *  This program is distributed in the hope that it will be useful,
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
13
 *  GNU General Public License for more details.
14
 *
14
 *
15
 *  You should have received a copy of the GNU General Public License along
15
 *  You should have received a copy of the GNU General Public License along
16
 *  with this program; if not, write to the Free Software Foundation, Inc.,
16
 *  with this program; if not, write to the Free Software Foundation, Inc.,
17
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
 *
18
 *
19
 */
19
 */
20
 
20
 
21
#include "Logger.h"
21
#include "Logger.h"
22
 
22
 
23
#include <iostream>
23
#include <iostream>
24
 
24
 
25
#include <syslog.h>
25
#include <syslog.h>
26
 
26
 
27
#include <boost/date_time.hpp>
27
#include <boost/date_time.hpp>
28
 
28
 
29
namespace atom {
29
namespace atom {
30
namespace logging {
30
namespace logging {
31
   
31
   
32
Logger::Level Logger::level_ = Logger::LEVEL_ALL;
32
Logger::Level Logger::level_ = Logger::LEVEL_ALL;
33
std::ofstream Logger::file_;
33
std::ofstream Logger::file_;
34
boost::mutex Logger::mutex_;
34
boost::mutex Logger::mutex_;
35
 
35
 
36
Logger::Logger(std::string name)
36
Logger::Logger(std::string name)
37
{
37
{
38
    this->name_ = name;
38
    this->name_ = name;
39
    this->name_.insert(this->name_.end(), 25 - this->name_.size(), ' ');
39
    this->name_.insert(this->name_.end(), 25 - this->name_.size(), ' ');
40
   
40
   
41
    openlog("Atom", LOG_ODELAY, LOG_DAEMON);
41
    openlog("Atom", LOG_ODELAY, LOG_DAEMON);
42
}
42
}
43
 
43
 
44
Logger::~Logger()
44
Logger::~Logger()
45
{
45
{
46
    closelog();
46
    closelog();
47
}
47
}
48
 
48
 
49
void Logger::SetLevel(Level level)
49
void Logger::SetLevel(Level level)
50
{
50
{
51
    Logger::level_ = level;
51
    Logger::level_ = level;
52
}
52
}
53
 
53
 
54
bool Logger::OpenFile(std::string filename)
54
bool Logger::OpenFile(std::string filename)
55
{
55
{
56
    Logger::file_.open(filename.data(), std::ios::app);
56
    Logger::file_.open(filename.data(), std::ios::app);
57
   
57
   
58
    return Logger::file_.is_open();
58
    return Logger::file_.is_open();
59
}
59
}
60
 
60
 
61
void Logger::CloseFile()
61
void Logger::CloseFile()
62
{
62
{
63
    Logger::file_.close();
63
    Logger::file_.close();
64
}
64
}
65
 
65
 
66
void Logger::Error(std::string message)
66
void Logger::Error(std::string message)
67
{
67
{
68
    if (Logger::level_ >= Logger::LEVEL_ERROR)
68
    if (Logger::level_ >= Logger::LEVEL_ERROR)
69
    {
69
    {
70
        this->Print(" ERROR " + this->name_ + message);
70
        this->Print(" ERROR " + this->name_ + "\033[31m" + message + "\033[0m");
71
    }
71
    }
72
}
72
}
73
 
73
 
74
void Logger::Warning(std::string message)
74
void Logger::Warning(std::string message)
75
{
75
{
76
    if (Logger::level_ >= Logger::LEVEL_WARNING)
76
    if (Logger::level_ >= Logger::LEVEL_WARNING)
77
    {
77
    {
78
        this->Print(" WARN  " + this->name_ + message);
78
        this->Print(" WARN  " + this->name_ + "\033[33m" + message + "\033[0m");
79
    }
79
    }
80
}
80
}
81
 
81
 
82
void Logger::Info(std::string message)
82
void Logger::Info(std::string message)
83
{
83
{
84
    if (Logger::level_ >= Logger::LEVEL_INFO)
84
    if (Logger::level_ >= Logger::LEVEL_INFO)
85
    {
85
    {
86
        this->Print(" INFO  " + this->name_ + message);
86
        this->Print(" INFO  " + this->name_ + message);
87
    }
87
    }
88
}
88
}
89
 
89
 
90
void Logger::Debug(std::string message)
90
void Logger::Debug(std::string message)
91
{
91
{
92
    if (Logger::level_ >= Logger::LEVEL_DEBUG)
92
    if (Logger::level_ >= Logger::LEVEL_DEBUG)
93
    {
93
    {
94
        this->Print(" DEBUG " + this->name_ + message);
94
        this->Print(" DEBUG " + this->name_ + "\033[35m" + message + "\033[0m");
95
    }
95
    }
96
}
96
}
97
 
97
 
98
void Logger::Print(std::string line)
98
void Logger::Print(std::string line)
99
{
99
{
100
    this->mutex_.lock();
100
    this->mutex_.lock();
101
   
101
   
102
    boost::posix_time::ptime date_and_time(boost::posix_time::second_clock::local_time());
102
    boost::posix_time::ptime date_and_time(boost::posix_time::second_clock::local_time());
103
   
103
   
104
    std::cout << date_and_time << line << std::endl;
104
    std::cout << date_and_time << line << std::endl;
105
   
105
   
106
    if (Logger::file_.is_open())
106
    if (Logger::file_.is_open())
107
    {
107
    {
108
        Logger::file_ << date_and_time << line << std::endl;
108
        Logger::file_ << date_and_time << line << std::endl;
109
    }
109
    }
110
   
110
   
111
    syslog(LOG_INFO, "%s", line.data());
111
    syslog(LOG_INFO, "%s", line.data());
112
   
112
   
113
    this->mutex_.unlock();
113
    this->mutex_.unlock();
114
}
114
}
115
 
115
 
116
}; // namespace logging
116
}; // namespace logging
117
}; // namespace atom
117
}; // namespace atom
118
 
118