Subversion Repositories HomeAutomation

Rev

Rev 1959 | Rev 1964 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1959 Rev 1961
1
/*
1
/*
2
 *
2
 *
3
 *  Copyright (C) 2012  Mattias Runge
3
 *  Copyright (C) 2012  Mattias Runge
4
 *
4
 *
5
 *  This program is free software; you can redistribute it and/or modify
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
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
7
 *  the Free Software Foundation; either version 2 of the License, or
8
 *  (at your option) any later version.
8
 *  (at your option) any later version.
9
 *
9
 *
10
 *  This program is distributed in the hope that it will be useful,
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
13
 *  GNU General Public License for more details.
14
 *
14
 *
15
 *  You should have received a copy of the GNU General Public License along
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.,
16
 *  with this program; if not, write to the Free Software Foundation, Inc.,
17
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
 *
18
 *
19
 */
19
 */
20
 
20
 
21
#include "log.h"
21
#include "log.h"
22
 
22
 
23
#include <iostream>
23
#include <iostream>
24
#include <stdarg.h>
24
#include <stdarg.h>
25
#include <string.h>
25
#include <string.h>
26
 
26
 
27
#include <boost/date_time.hpp>
27
#include <boost/date_time.hpp>
28
#include <boost/thread/mutex.hpp>
28
#include <boost/thread/mutex.hpp>
29
 
29
 
30
namespace atom {
30
namespace atom {
31
namespace log {
31
namespace log {
32
 
32
 
33
static boost::mutex   mutex_;
33
static boost::mutex   mutex_;
34
static Level          level_ = LOG_LEVEL_ALL;
34
static Level          level_ = (Level)(LOG_LEVEL_ERROR | LOG_LEVEL_EXCEPTION | LOG_LEVEL_WARNING | LOG_LEVEL_INFO);
35
static const uint32_t kMaxBufferSize = 4096;
35
static const uint32_t kMaxBufferSize = 4096;
36
 
36
 
37
void SetLogLevel(Level level)
37
void SetLogLevel(Level level)
38
{
38
{
39
  level_ = level;
39
  level_ = level;
-
 
40
}
-
 
41
 
-
 
42
void SetLogLevelByString(std::string level_string)
-
 
43
{
-
 
44
  level_ = LOG_LEVEL_NONE;
-
 
45
 
-
 
46
  if (level_string.find("LOG_LEVEL_ERROR") != std::string::npos)
-
 
47
  {
-
 
48
    level_ = (Level)(level_ | LOG_LEVEL_ERROR);
-
 
49
  }
-
 
50
 
-
 
51
  if (level_string.find("LOG_LEVEL_EXCEPTION") != std::string::npos)
-
 
52
  {
-
 
53
    level_ = (Level)(level_ | LOG_LEVEL_EXCEPTION);
-
 
54
  }
-
 
55
 
-
 
56
  if (level_string.find("LOG_LEVEL_WARNING") != std::string::npos)
-
 
57
  {
-
 
58
    level_ = (Level)(level_ | LOG_LEVEL_WARNING);
-
 
59
  }
-
 
60
 
-
 
61
  if (level_string.find("LOG_LEVEL_INFO") != std::string::npos)
-
 
62
  {
-
 
63
    level_ = (Level)(level_ | LOG_LEVEL_INFO);
-
 
64
  }
-
 
65
 
-
 
66
  if (level_string.find("LOG_LEVEL_DEBUG") != std::string::npos)
-
 
67
  {
-
 
68
    level_ = (Level)(level_ | LOG_LEVEL_DEBUG);
-
 
69
  }
-
 
70
 
-
 
71
  if (level_string.find("LOG_LEVEL_ALL") != std::string::npos)
-
 
72
  {
-
 
73
    level_ = (Level)(level_ | LOG_LEVEL_ALL);
-
 
74
  }
40
}
75
}
41
 
76
 
42
void Print(Level level, std::string module, std::string message)
77
void Print(Level level, std::string module, std::string message)
43
{
78
{
44
  std::string               level_string;
79
  std::string               level_string;
45
  boost::posix_time::ptime  date_and_time(boost::posix_time::second_clock::local_time());
80
  boost::posix_time::ptime  date_and_time(boost::posix_time::second_clock::local_time());
46
 
81
 
47
 
82
 
48
  /* Check log level */
83
  /* Check log level */
49
  if (0 == (level & level_))
84
  if (0 == (level & level_))
50
  {
85
  {
51
    return;
86
    return;
52
  }
87
  }
53
 
88
 
54
 
89
 
55
  /* Convert log level to string */
90
  /* Convert log level to string */
56
  if (0 != (level & LOG_LEVEL_ERROR))
91
  if (0 != (level & LOG_LEVEL_ERROR))
57
  {
92
  {
58
    level_string = "ERROR";
93
    level_string = "ERROR";
59
  }
94
  }
60
  else if (0 != (level & LOG_LEVEL_INFO))
95
  else if (0 != (level & LOG_LEVEL_INFO))
61
  {
96
  {
62
    level_string = "INFO";
97
    level_string = "INFO";
63
  }
98
  }
64
  else if (0 != (level & LOG_LEVEL_DEBUG))
99
  else if (0 != (level & LOG_LEVEL_DEBUG))
65
  {
100
  {
66
    level_string = "DEBUG";
101
    level_string = "DEBUG";
67
  }
102
  }
68
  else if (0 != (level & LOG_LEVEL_EXCEPTION))
103
  else if (0 != (level & LOG_LEVEL_EXCEPTION))
69
  {
104
  {
70
    level_string = "EXCEPTION";
105
    level_string = "EXCEPTION";
71
  }
106
  }
72
  else if (0 != (level & LOG_LEVEL_WARNING))
107
  else if (0 != (level & LOG_LEVEL_WARNING))
73
  {
108
  {
74
    level_string = "WARNING";
109
    level_string = "WARNING";
75
  }
110
  }
76
  else
111
  else
77
  {
112
  {
78
    level_string = "UNKNOWN";
113
    level_string = "UNKNOWN";
79
  }
114
  }
80
 
115
 
81
 
116
 
82
  /* Adjust lengths of sub strings */
117
  /* Adjust lengths of sub strings */
83
  level_string.insert(level_string.end(), 9 - level_string.size(), ' ');
118
  level_string.insert(level_string.end(), 9 - level_string.size(), ' ');
84
  module.insert(module.end(), 25 - module.size(), ' ');
119
  module.insert(module.end(), 25 - module.size(), ' ');
85
 
120
 
86
 
121
 
87
  /* Print message */
122
  /* Print message */
88
  mutex_.lock();
123
  mutex_.lock();
89
 
124
 
90
  std::cout << boost::posix_time::to_simple_string(date_and_time) << " " << level_string << " " << module << " " << message << std::endl;
125
  std::cout << boost::posix_time::to_simple_string(date_and_time) << " " << level_string << " " << module << " " << message << std::endl;
91
  std::cout.imbue(std::locale::classic());
126
  std::cout.imbue(std::locale::classic());
92
 
127
 
93
  mutex_.unlock();
128
  mutex_.unlock();
94
}
129
}
95
 
130
 
96
void Info(std::string module, char* format, ...)
131
void Info(std::string module, char* format, ...)
97
{
132
{
98
  char    buffer[kMaxBufferSize];
133
  char    buffer[kMaxBufferSize];
99
  va_list parameters;
134
  va_list parameters;
100
 
135
 
101
 
136
 
102
  /* Initialize variables */
137
  /* Initialize variables */
103
  memset(&buffer[0], 0, sizeof(buffer));
138
  memset(&buffer[0], 0, sizeof(buffer));
104
 
139
 
105
 
140
 
106
  /* Construct message */
141
  /* Construct message */
107
  va_start(parameters, format);
142
  va_start(parameters, format);
108
  vsnprintf(&buffer[0], sizeof(buffer), format, parameters);
143
  vsnprintf(&buffer[0], sizeof(buffer), format, parameters);
109
  va_end(parameters);
144
  va_end(parameters);
110
 
145
 
111
  Print(LOG_LEVEL_INFO, module, &buffer[0]);
146
  Print(LOG_LEVEL_INFO, module, &buffer[0]);
112
}
147
}
113
 
148
 
114
void Info(std::string module, std::string message)
149
void Info(std::string module, std::string message)
115
{
150
{
116
  Print(LOG_LEVEL_INFO, module, message);
151
  Print(LOG_LEVEL_INFO, module, message);
117
}
152
}
118
 
153
 
119
void Debug(std::string module, char* format, ...)
154
void Debug(std::string module, char* format, ...)
120
{
155
{
121
  char    buffer[kMaxBufferSize];
156
  char    buffer[kMaxBufferSize];
122
  va_list parameters;
157
  va_list parameters;
123
 
158
 
124
 
159
 
125
  /* Initialize variables */
160
  /* Initialize variables */
126
  memset(&buffer[0], 0, sizeof(buffer));
161
  memset(&buffer[0], 0, sizeof(buffer));
127
 
162
 
128
 
163
 
129
  /* Construct message */
164
  /* Construct message */
130
  va_start(parameters, format);
165
  va_start(parameters, format);
131
  vsnprintf(&buffer[0], sizeof(buffer), format, parameters);
166
  vsnprintf(&buffer[0], sizeof(buffer), format, parameters);
132
  va_end(parameters);
167
  va_end(parameters);
133
 
168
 
134
  Print(LOG_LEVEL_DEBUG, module, &buffer[0]);
169
  Print(LOG_LEVEL_DEBUG, module, &buffer[0]);
135
}
170
}
136
 
171
 
137
void Debug(std::string module, std::string message)
172
void Debug(std::string module, std::string message)
138
{
173
{
139
  Print(LOG_LEVEL_DEBUG, module, message);
174
  Print(LOG_LEVEL_DEBUG, module, message);
140
}
175
}
141
 
176
 
142
void Error(std::string module, char* format, ...)
177
void Error(std::string module, char* format, ...)
143
{
178
{
144
  char    buffer[kMaxBufferSize];
179
  char    buffer[kMaxBufferSize];
145
  va_list parameters;
180
  va_list parameters;
146
 
181
 
147
 
182
 
148
  /* Initialize variables */
183
  /* Initialize variables */
149
  memset(&buffer[0], 0, sizeof(buffer));
184
  memset(&buffer[0], 0, sizeof(buffer));
150
 
185
 
151
 
186
 
152
  /* Construct message */
187
  /* Construct message */
153
  va_start(parameters, format);
188
  va_start(parameters, format);
154
  vsnprintf(&buffer[0], sizeof(buffer), format, parameters);
189
  vsnprintf(&buffer[0], sizeof(buffer), format, parameters);
155
  va_end(parameters);
190
  va_end(parameters);
156
 
191
 
157
  Print(LOG_LEVEL_ERROR, module, &buffer[0]);
192
  Print(LOG_LEVEL_ERROR, module, &buffer[0]);
158
}
193
}
159
 
194
 
160
void Error(std::string module, std::string message)
195
void Error(std::string module, std::string message)
161
{
196
{
162
  Print(LOG_LEVEL_ERROR, module, message);
197
  Print(LOG_LEVEL_ERROR, module, message);
163
}
198
}
164
 
199
 
165
void Warning(std::string module, char* format, ...)
200
void Warning(std::string module, char* format, ...)
166
{
201
{
167
  char    buffer[kMaxBufferSize];
202
  char    buffer[kMaxBufferSize];
168
  va_list parameters;
203
  va_list parameters;
169
 
204
 
170
 
205
 
171
  /* Initialize variables */
206
  /* Initialize variables */
172
  memset(&buffer[0], 0, sizeof(buffer));
207
  memset(&buffer[0], 0, sizeof(buffer));
173
 
208
 
174
 
209
 
175
  /* Construct message */
210
  /* Construct message */
176
  va_start(parameters, format);
211
  va_start(parameters, format);
177
  vsnprintf(&buffer[0], sizeof(buffer), format, parameters);
212
  vsnprintf(&buffer[0], sizeof(buffer), format, parameters);
178
  va_end(parameters);
213
  va_end(parameters);
179
 
214
 
180
  Print(LOG_LEVEL_WARNING, module, &buffer[0]);
215
  Print(LOG_LEVEL_WARNING, module, &buffer[0]);
181
}
216
}
182
 
217
 
183
void Warning(std::string module, std::string message)
218
void Warning(std::string module, std::string message)
184
{
219
{
185
  Print(LOG_LEVEL_WARNING, module, message);
220
  Print(LOG_LEVEL_WARNING, module, message);
186
}
221
}
187
 
222
 
188
void Exception(std::string module, std::exception& exception)
223
void Exception(std::string module, std::exception& exception)
189
{
224
{
190
  Print(LOG_LEVEL_EXCEPTION, module, exception.what());
225
  Print(LOG_LEVEL_EXCEPTION, module, exception.what());
191
}
226
}
192
 
227
 
193
 
228
 
194
}; // namespace log
229
}; // namespace log
195
}; // namespace atom
230
}; // namespace atom
196
 
231