Subversion Repositories HomeAutomation

Rev

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

Rev 1594 Rev 1600
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>
-
 
26
 
25
#include "boost/date_time.hpp"
27
#include <boost/date_time.hpp>
26
 
28
 
27
namespace atom {
29
namespace atom {
28
namespace logging {
30
namespace logging {
29
   
31
   
30
Logger::Level Logger::level_ = Logger::LEVEL_ALL;
32
Logger::Level Logger::level_ = Logger::LEVEL_ALL;
31
std::ofstream Logger::file_;
33
std::ofstream Logger::file_;
32
boost::mutex Logger::mutex_;
34
boost::mutex Logger::mutex_;
33
 
35
 
34
Logger::Logger(std::string name)
36
Logger::Logger(std::string name)
35
{
37
{
36
    this->name_ = name;
38
    this->name_ = name;
37
    this->name_.insert(this->name_.end(), 20 - this->name_.size(), ' ');
39
    this->name_.insert(this->name_.end(), 20 - this->name_.size(), ' ');
-
 
40
   
-
 
41
    openlog("Atom", LOG_ODELAY, LOG_DAEMON);
38
}
42
}
39
 
43
 
40
Logger::~Logger()
44
Logger::~Logger()
41
{
45
{
42
 
46
    closelog();
43
}
47
}
44
 
48
 
45
void Logger::SetLevel(Level level)
49
void Logger::SetLevel(Level level)
46
{
50
{
47
    Logger::level_ = level;
51
    Logger::level_ = level;
48
}
52
}
49
 
53
 
50
bool Logger::OpenFile(std::string filename)
54
bool Logger::OpenFile(std::string filename)
51
{
55
{
52
    Logger::file_.open(filename.data(), std::ios::app);
56
    Logger::file_.open(filename.data(), std::ios::app);
53
   
57
   
54
    return Logger::file_.is_open();
58
    return Logger::file_.is_open();
55
}
59
}
56
 
60
 
57
void Logger::CloseFile()
61
void Logger::CloseFile()
58
{
62
{
59
    Logger::file_.close();
63
    Logger::file_.close();
60
}
64
}
61
 
65
 
62
void Logger::Error(std::string message)
66
void Logger::Error(std::string message)
63
{
67
{
64
    if (Logger::level_ >= Logger::LEVEL_ERROR)
68
    if (Logger::level_ >= Logger::LEVEL_ERROR)
65
    {
69
    {
66
        this->Print(" ERROR " + this->name_ + message);
70
        this->Print(" ERROR " + this->name_ + message);
67
    }
71
    }
68
}
72
}
69
 
73
 
70
void Logger::Warning(std::string message)
74
void Logger::Warning(std::string message)
71
{
75
{
72
    if (Logger::level_ >= Logger::LEVEL_WARNING)
76
    if (Logger::level_ >= Logger::LEVEL_WARNING)
73
    {
77
    {
74
        this->Print(" WARN  " + this->name_ + message);
78
        this->Print(" WARN  " + this->name_ + message);
75
    }
79
    }
76
}
80
}
77
 
81
 
78
void Logger::Info(std::string message)
82
void Logger::Info(std::string message)
79
{
83
{
80
    if (Logger::level_ >= Logger::LEVEL_INFO)
84
    if (Logger::level_ >= Logger::LEVEL_INFO)
81
    {
85
    {
82
        this->Print(" INFO  " + this->name_ + message);
86
        this->Print(" INFO  " + this->name_ + message);
83
    }
87
    }
84
}
88
}
85
 
89
 
86
void Logger::Debug(std::string message)
90
void Logger::Debug(std::string message)
87
{
91
{
88
    if (Logger::level_ >= Logger::LEVEL_DEBUG)
92
    if (Logger::level_ >= Logger::LEVEL_DEBUG)
89
    {
93
    {
90
        this->Print(" DEBUG " + this->name_ + message);
94
        this->Print(" DEBUG " + this->name_ + message);
91
    }
95
    }
92
}
96
}
93
 
97
 
94
void Logger::Print(std::string line)
98
void Logger::Print(std::string line)
95
{
99
{
96
    this->mutex_.lock();
100
    this->mutex_.lock();
97
   
101
   
98
    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());
99
   
103
   
100
    std::cout << date_and_time << line << std::endl;
104
    std::cout << date_and_time << line << std::endl;
101
   
105
   
102
    if (Logger::file_.is_open())
106
    if (Logger::file_.is_open())
103
    {
107
    {
104
        Logger::file_ << date_and_time << line << std::endl;
108
        Logger::file_ << date_and_time << line << std::endl;
105
    }
109
    }
-
 
110
   
-
 
111
    syslog(LOG_INFO, "%s", line.data());
106
   
112
   
107
    this->mutex_.unlock();
113
    this->mutex_.unlock();
108
}
114
}
109
 
115
 
110
}; // namespace logging
116
}; // namespace logging
111
}; // namespace atom
117
}; // namespace atom
112
 
118