Subversion Repositories HomeAutomation

Rev

Rev 1947 | Go to most recent revision | Details | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
1945 runge 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
  switch (level)
56
  {
57
    case LOG_LEVEL_ERROR:
58
    {
59
      level_string = "ERROR";
60
      break;
61
    }
62
    case LOG_LEVEL_INFO:
63
    {
64
      level_string = "INFO";
65
      break;
66
    }
67
    case LOG_LEVEL_DEBUG:
68
    {
69
      level_string = "WARNING";
70
      break;
71
    }
72
    case LOG_LEVEL_EXCEPTION:
73
    {
74
      level_string = "EXCEPTION";
75
      break;
76
    }
77
    default:
78
    {
79
      level_string = "UNKNOWN";
80
      break;
81
    }
82
  }
83
 
84
 
85
  /* Adjust lengths of sub strings */
86
  level_string.insert(level_string.end(), 9 - level_string.size(), ' ');
87
  module.insert(module.end(), 25 - module.size(), ' ');
88
 
89
 
90
  /* Print message */
91
  mutex_.lock();
92
 
93
  std::cout << date_and_time << " " << level_string << " " << module << " " << message << std::endl;
94
  std::cout.imbue(std::locale::classic());
95
 
96
  mutex_.unlock();
97
}
98
 
99
void Info(std::string module, char* format, ...)
100
{
101
  char    buffer[1024];
102
  va_list parameters;
103
 
104
 
105
  /* Initialize variables */
106
  memset(&buffer[0], 0, sizeof(buffer));
107
 
108
 
109
  /* Construct message */
110
  va_start(parameters, format);
111
  vsnprintf(&buffer[0], sizeof(buffer), format, parameters);
112
  va_end(parameters);
113
 
114
  Print(LOG_LEVEL_INFO, module, &buffer[0]);
115
}
116
 
117
void Info(std::string module, std::string message)
118
{
119
  Print(LOG_LEVEL_INFO, module, message);
120
}
121
 
122
void Debug(std::string module, char* format, ...)
123
{
124
  char    buffer[1024];
125
  va_list parameters;
126
 
127
 
128
  /* Initialize variables */
129
  memset(&buffer[0], 0, sizeof(buffer));
130
 
131
 
132
  /* Construct message */
133
  va_start(parameters, format);
134
  vsnprintf(&buffer[0], sizeof(buffer), format, parameters);
135
  va_end(parameters);
136
 
137
  Print(LOG_LEVEL_DEBUG, module, &buffer[0]);
138
}
139
 
140
void Debug(std::string module, std::string message)
141
{
142
  Print(LOG_LEVEL_DEBUG, module, message);
143
}
144
 
145
void Error(std::string module, char* format, ...)
146
{
147
  char    buffer[1024];
148
  va_list parameters;
149
 
150
 
151
  /* Initialize variables */
152
  memset(&buffer[0], 0, sizeof(buffer));
153
 
154
 
155
  /* Construct message */
156
  va_start(parameters, format);
157
  vsnprintf(&buffer[0], sizeof(buffer), format, parameters);
158
  va_end(parameters);
159
 
160
  Print(LOG_LEVEL_ERROR, module, &buffer[0]);
161
}
162
 
163
void Error(std::string module, std::string message)
164
{
165
  Print(LOG_LEVEL_ERROR, module, message);
166
}
167
 
168
void Warning(std::string module, char* format, ...)
169
{
170
  char    buffer[1024];
171
  va_list parameters;
172
 
173
 
174
  /* Initialize variables */
175
  memset(&buffer[0], 0, sizeof(buffer));
176
 
177
 
178
  /* Construct message */
179
  va_start(parameters, format);
180
  vsnprintf(&buffer[0], sizeof(buffer), format, parameters);
181
  va_end(parameters);
182
 
183
  Print(LOG_LEVEL_WARNING, module, &buffer[0]);
184
}
185
 
186
void Warning(std::string module, std::string message)
187
{
188
  Print(LOG_LEVEL_WARNING, module, message);
189
}
190
 
191
void Exception(std::string module, std::exception& exception)
192
{
193
  Print(LOG_LEVEL_EXCEPTION, module, exception.what());
194
}
195
 
196
 
197
}; // namespace log
198
}; // namespace atom