Subversion Repositories HomeAutomation

Rev

Rev 1600 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

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