Subversion Repositories HomeAutomation

Rev

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

  1. /*
  2.  *
  3.  *  Copyright (C) 2012  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 "log.h"
  22.  
  23. #include <iostream>
  24. #include <stdarg.h>
  25. #include <string.h>
  26.  
  27. #include <boost/date_time.hpp>
  28. #include <boost/thread/mutex.hpp>
  29.  
  30. namespace atom {
  31. namespace log {
  32.  
  33. static boost::mutex mutex_;
  34. static Level level_ = LOG_LEVEL_ALL;
  35.  
  36. void SetLogLevel(Level level)
  37. {
  38.   level_ = level;
  39. }
  40.  
  41. void Print(Level level, std::string module, std::string message)
  42. {
  43.   std::string               level_string;
  44.   boost::posix_time::ptime  date_and_time(boost::posix_time::second_clock::local_time());
  45.  
  46.  
  47.   /* Check log level */
  48.   if (0 == (level & level_))
  49.   {
  50.     return;
  51.   }
  52.  
  53.  
  54.   /* Convert log level to string */
  55.   if (0 != (level & LOG_LEVEL_ERROR))
  56.   {
  57.     level_string = "ERROR";
  58.   }
  59.   else if (0 != (level & LOG_LEVEL_INFO))
  60.   {
  61.     level_string = "INFO";
  62.   }
  63.   else if (0 != (level & LOG_LEVEL_DEBUG))
  64.   {
  65.     level_string = "DEBUG";
  66.   }
  67.   else if (0 != (level & LOG_LEVEL_EXCEPTION))
  68.   {
  69.     level_string = "EXCEPTION";
  70.   }
  71.   else if (0 != (level & LOG_LEVEL_WARNING))
  72.   {
  73.     level_string = "WARNING";
  74.   }
  75.   else
  76.   {
  77.     level_string = "UNKNOWN";
  78.   }
  79.  
  80.  
  81.   /* Adjust lengths of sub strings */
  82.   level_string.insert(level_string.end(), 9 - level_string.size(), ' ');
  83.   module.insert(module.end(), 25 - module.size(), ' ');
  84.  
  85.  
  86.   /* Print message */
  87.   mutex_.lock();
  88.  
  89.   std::cout << date_and_time << " " << level_string << " " << module << " " << message << std::endl;
  90.   std::cout.imbue(std::locale::classic());
  91.  
  92.   mutex_.unlock();
  93. }
  94.  
  95. void Info(std::string module, char* format, ...)
  96. {
  97.   char    buffer[1024];
  98.   va_list parameters;
  99.  
  100.  
  101.   /* Initialize variables */
  102.   memset(&buffer[0], 0, sizeof(buffer));
  103.  
  104.  
  105.   /* Construct message */
  106.   va_start(parameters, format);
  107.   vsnprintf(&buffer[0], sizeof(buffer), format, parameters);
  108.   va_end(parameters);
  109.  
  110.   Print(LOG_LEVEL_INFO, module, &buffer[0]);
  111. }
  112.  
  113. void Info(std::string module, std::string message)
  114. {
  115.   Print(LOG_LEVEL_INFO, module, message);
  116. }
  117.  
  118. void Debug(std::string module, char* format, ...)
  119. {
  120.   char    buffer[1024];
  121.   va_list parameters;
  122.  
  123.  
  124.   /* Initialize variables */
  125.   memset(&buffer[0], 0, sizeof(buffer));
  126.  
  127.  
  128.   /* Construct message */
  129.   va_start(parameters, format);
  130.   vsnprintf(&buffer[0], sizeof(buffer), format, parameters);
  131.   va_end(parameters);
  132.  
  133.   Print(LOG_LEVEL_DEBUG, module, &buffer[0]);
  134. }
  135.  
  136. void Debug(std::string module, std::string message)
  137. {
  138.   Print(LOG_LEVEL_DEBUG, module, message);
  139. }
  140.  
  141. void Error(std::string module, char* format, ...)
  142. {
  143.   char    buffer[1024];
  144.   va_list parameters;
  145.  
  146.  
  147.   /* Initialize variables */
  148.   memset(&buffer[0], 0, sizeof(buffer));
  149.  
  150.  
  151.   /* Construct message */
  152.   va_start(parameters, format);
  153.   vsnprintf(&buffer[0], sizeof(buffer), format, parameters);
  154.   va_end(parameters);
  155.  
  156.   Print(LOG_LEVEL_ERROR, module, &buffer[0]);
  157. }
  158.  
  159. void Error(std::string module, std::string message)
  160. {
  161.   Print(LOG_LEVEL_ERROR, module, message);
  162. }
  163.  
  164. void Warning(std::string module, char* format, ...)
  165. {
  166.   char    buffer[1024];
  167.   va_list parameters;
  168.  
  169.  
  170.   /* Initialize variables */
  171.   memset(&buffer[0], 0, sizeof(buffer));
  172.  
  173.  
  174.   /* Construct message */
  175.   va_start(parameters, format);
  176.   vsnprintf(&buffer[0], sizeof(buffer), format, parameters);
  177.   va_end(parameters);
  178.  
  179.   Print(LOG_LEVEL_WARNING, module, &buffer[0]);
  180. }
  181.  
  182. void Warning(std::string module, std::string message)
  183. {
  184.   Print(LOG_LEVEL_WARNING, module, message);
  185. }
  186.  
  187. void Exception(std::string module, std::exception& exception)
  188. {
  189.   Print(LOG_LEVEL_EXCEPTION, module, exception.what());
  190. }
  191.  
  192.  
  193. }; // namespace log
  194. }; // namespace atom
  195.