Subversion Repositories HomeAutomation

Rev

Rev 1939 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1939 Rev 1964
Line 19... Line 19...
19
 */
19
 */
20
 
20
 
21
#include "Logger.h"
21
#include "Logger.h"
22
 
22
 
23
#include <iostream>
23
#include <iostream>
24
 
-
 
25
#include <syslog.h>
24
#include <syslog.h>
26
 
-
 
27
#include <boost/date_time.hpp>
25
#include <boost/date_time.hpp>
28
 
26
 
-
 
27
#include "common/log.h"
-
 
28
 
29
namespace atom {
29
namespace atom {
30
namespace logging {
30
namespace logging {
31
   
-
 
32
Logger::Level Logger::level_ = Logger::LEVEL_ALL;
-
 
33
std::ofstream Logger::file_;
-
 
34
boost::mutex Logger::mutex_;
-
 
35
 
31
 
36
Logger::Logger(std::string name)
32
Logger::Logger(std::string name)
37
{
33
{
38
    this->name_ = name;
34
  this->name_ = name;
39
    this->name_.insert(this->name_.end(), 25 - this->name_.size(), ' ');
35
  this->name_.insert(this->name_.end(), 25 - this->name_.size(), ' ');
40
   
-
 
41
    openlog("Atom", LOG_ODELAY, LOG_DAEMON);
-
 
42
}
36
}
43
 
37
 
44
Logger::~Logger()
38
Logger::~Logger()
45
{
39
{
46
    closelog();
-
 
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
}
40
}
65
 
41
 
66
void Logger::Error(std::string message)
42
void Logger::Error(std::string message)
-
 
43
{
-
 
44
  log::Error(this->name_, message.c_str());
-
 
45
}
-
 
46
 
-
 
47
void Logger::Warning(std::string message)
67
{
48
{
68
    if (Logger::level_ >= Logger::LEVEL_ERROR)
-
 
69
    {
-
 
70
        this->Print(" ERROR " + this->name_ + "\033[31m" + message + "\033[0m");
49
  log::Warning(this->name_, message.c_str());
71
    }
50
}
72
}
-
 
73
 
51
 
74
void Logger::Warning(std::string message)
-
 
75
{
-
 
76
    if (Logger::level_ >= Logger::LEVEL_WARNING)
-
 
77
    {
-
 
78
        this->Print(" WARN  " + this->name_ + "\033[33m" + message + "\033[0m");
-
 
79
    }
-
 
80
}
-
 
81
 
-
 
82
void Logger::Info(std::string message)
52
void Logger::Info(std::string message)
83
{
53
{
84
    if (Logger::level_ >= Logger::LEVEL_INFO)
-
 
85
    {
-
 
86
        this->Print(" INFO  " + this->name_ + message);
54
  log::Info(this->name_, message.c_str());
87
    }
-
 
88
}
-
 
89
 
-
 
90
void Logger::Debug(std::string message)
-
 
91
{
-
 
92
    if (Logger::level_ >= Logger::LEVEL_DEBUG)
-
 
93
    {
-
 
94
        this->Print(" DEBUG " + this->name_ + "\033[35m" + message + "\033[0m");
-
 
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
    }
55
}
110
   
56
 
111
    syslog(LOG_INFO, "%s", line.data());
-
 
112
   
-
 
113
    std::cout.imbue(std::locale::classic());
57
void Logger::Debug(std::string message)
114
   
58
{
115
    this->mutex_.unlock();
59
  log::Debug(this->name_, message.c_str());
116
}
60
}
117
 
61
 
118
}; // namespace logging
62
}; // namespace logging
119
}; // namespace atom
63
}; // namespace atom