Subversion Repositories HomeAutomation

Rev

Rev 1603 | Rev 1964 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

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