Subversion Repositories HomeAutomation

Rev

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

Rev 1961 Rev 1964
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 std::ofstream  file_;
34
static Level          level_ = (Level)(LOG_LEVEL_ERROR | LOG_LEVEL_EXCEPTION | LOG_LEVEL_WARNING | LOG_LEVEL_INFO);
35
static Level          level_ = (Level)(LOG_LEVEL_ERROR | LOG_LEVEL_EXCEPTION | LOG_LEVEL_WARNING | LOG_LEVEL_INFO);
35
static const uint32_t kMaxBufferSize = 4096;
36
static const uint32_t kMaxBufferSize = 4096;
36
 
37
 
37
void SetLogLevel(Level level)
38
void SetLevel(Level level)
38
{
39
{
39
  level_ = level;
40
  level_ = level;
40
}
41
}
41
 
42
 
42
void SetLogLevelByString(std::string level_string)
43
void SetLevelByString(std::string level_string)
43
{
44
{
44
  level_ = LOG_LEVEL_NONE;
45
  level_ = LOG_LEVEL_NONE;
45
 
46
 
46
  if (level_string.find("LOG_LEVEL_ERROR") != std::string::npos)
47
  if (level_string.find("LOG_LEVEL_ERROR") != std::string::npos)
47
  {
48
  {
48
    level_ = (Level)(level_ | LOG_LEVEL_ERROR);
49
    level_ = (Level)(level_ | LOG_LEVEL_ERROR);
49
  }
50
  }
50
 
51
 
51
  if (level_string.find("LOG_LEVEL_EXCEPTION") != std::string::npos)
52
  if (level_string.find("LOG_LEVEL_EXCEPTION") != std::string::npos)
52
  {
53
  {
53
    level_ = (Level)(level_ | LOG_LEVEL_EXCEPTION);
54
    level_ = (Level)(level_ | LOG_LEVEL_EXCEPTION);
54
  }
55
  }
55
 
56
 
56
  if (level_string.find("LOG_LEVEL_WARNING") != std::string::npos)
57
  if (level_string.find("LOG_LEVEL_WARNING") != std::string::npos)
57
  {
58
  {
58
    level_ = (Level)(level_ | LOG_LEVEL_WARNING);
59
    level_ = (Level)(level_ | LOG_LEVEL_WARNING);
59
  }
60
  }
60
 
61
 
61
  if (level_string.find("LOG_LEVEL_INFO") != std::string::npos)
62
  if (level_string.find("LOG_LEVEL_INFO") != std::string::npos)
62
  {
63
  {
63
    level_ = (Level)(level_ | LOG_LEVEL_INFO);
64
    level_ = (Level)(level_ | LOG_LEVEL_INFO);
64
  }
65
  }
65
 
66
 
66
  if (level_string.find("LOG_LEVEL_DEBUG") != std::string::npos)
67
  if (level_string.find("LOG_LEVEL_DEBUG") != std::string::npos)
67
  {
68
  {
68
    level_ = (Level)(level_ | LOG_LEVEL_DEBUG);
69
    level_ = (Level)(level_ | LOG_LEVEL_DEBUG);
69
  }
70
  }
70
 
71
 
71
  if (level_string.find("LOG_LEVEL_ALL") != std::string::npos)
72
  if (level_string.find("LOG_LEVEL_ALL") != std::string::npos)
72
  {
73
  {
73
    level_ = (Level)(level_ | LOG_LEVEL_ALL);
74
    level_ = (Level)(level_ | LOG_LEVEL_ALL);
-
 
75
  }
-
 
76
}
-
 
77
 
-
 
78
bool OpenFile(std::string filepath)
-
 
79
{
-
 
80
  CloseFile();
-
 
81
 
-
 
82
  file_.open(filepath.data(), std::ios::app);
-
 
83
 
-
 
84
  return file_.is_open();
-
 
85
}
-
 
86
 
-
 
87
void CloseFile(void)
-
 
88
{
-
 
89
  if (file_.is_open())
-
 
90
  {
-
 
91
    file_.close();
74
  }
92
  }
75
}
93
}
76
 
94
 
77
void Print(Level level, std::string module, std::string message)
95
void Print(Level level, std::string module, std::string message)
78
{
96
{
-
 
97
  std::string               log_string;
79
  std::string               level_string;
98
  std::string               level_string;
80
  boost::posix_time::ptime  date_and_time(boost::posix_time::second_clock::local_time());
99
  boost::posix_time::ptime  date_and_time(boost::posix_time::second_clock::local_time());
81
 
100
 
82
 
101
 
83
  /* Check log level */
102
  /* Check log level */
84
  if (0 == (level & level_))
103
  if (0 == (level & level_))
85
  {
104
  {
86
    return;
105
    return;
87
  }
106
  }
88
 
107
 
89
 
108
 
90
  /* Convert log level to string */
109
  /* Convert log level to string */
91
  if (0 != (level & LOG_LEVEL_ERROR))
110
  if (0 != (level & LOG_LEVEL_ERROR))
92
  {
111
  {
93
    level_string = "ERROR";
112
    level_string = "ERROR";
94
  }
113
  }
95
  else if (0 != (level & LOG_LEVEL_INFO))
114
  else if (0 != (level & LOG_LEVEL_INFO))
96
  {
115
  {
97
    level_string = "INFO";
116
    level_string = "INFO";
98
  }
117
  }
99
  else if (0 != (level & LOG_LEVEL_DEBUG))
118
  else if (0 != (level & LOG_LEVEL_DEBUG))
100
  {
119
  {
101
    level_string = "DEBUG";
120
    level_string = "DEBUG";
102
  }
121
  }
103
  else if (0 != (level & LOG_LEVEL_EXCEPTION))
122
  else if (0 != (level & LOG_LEVEL_EXCEPTION))
104
  {
123
  {
105
    level_string = "EXCEPTION";
124
    level_string = "EXCEPTION";
106
  }
125
  }
107
  else if (0 != (level & LOG_LEVEL_WARNING))
126
  else if (0 != (level & LOG_LEVEL_WARNING))
108
  {
127
  {
109
    level_string = "WARNING";
128
    level_string = "WARNING";
110
  }
129
  }
111
  else
130
  else
112
  {
131
  {
113
    level_string = "UNKNOWN";
132
    level_string = "UNKNOWN";
114
  }
133
  }
115
 
134
 
116
 
135
 
117
  /* Adjust lengths of sub strings */
136
  /* Adjust lengths of sub strings */
118
  level_string.insert(level_string.end(), 9 - level_string.size(), ' ');
137
  level_string.insert(level_string.end(), 9 - level_string.size(), ' ');
119
  module.insert(module.end(), 25 - module.size(), ' ');
138
  module.insert(module.end(), 25 - module.size(), ' ');
-
 
139
 
-
 
140
 
-
 
141
  /* Create log string */
-
 
142
  log_string = boost::posix_time::to_simple_string(date_and_time) + " " + level_string + " " + module + " " + message;
120
 
143
 
121
 
144
 
122
  /* Print message */
145
  /* Print message */
123
  mutex_.lock();
146
  mutex_.lock();
124
 
147
 
125
  std::cout << boost::posix_time::to_simple_string(date_and_time) << " " << level_string << " " << module << " " << message << std::endl;
148
  std::cout << log_string << std::endl;
-
 
149
 
-
 
150
 
-
 
151
  if (file_.is_open())
-
 
152
  {
-
 
153
    file_ << log_string << std::endl;
-
 
154
  }
-
 
155
 
126
  std::cout.imbue(std::locale::classic());
156
  std::cout.imbue(std::locale::classic());
127
 
157
 
128
  mutex_.unlock();
158
  mutex_.unlock();
129
}
159
}
130
 
160
 
131
void Info(std::string module, char* format, ...)
161
void Info(std::string module, char* format, ...)
132
{
162
{
133
  char    buffer[kMaxBufferSize];
163
  char    buffer[kMaxBufferSize];
134
  va_list parameters;
164
  va_list parameters;
135
 
165
 
136
 
166
 
137
  /* Initialize variables */
167
  /* Initialize variables */
138
  memset(&buffer[0], 0, sizeof(buffer));
168
  memset(&buffer[0], 0, sizeof(buffer));
139
 
169
 
140
 
170
 
141
  /* Construct message */
171
  /* Construct message */
142
  va_start(parameters, format);
172
  va_start(parameters, format);
143
  vsnprintf(&buffer[0], sizeof(buffer), format, parameters);
173
  vsnprintf(&buffer[0], sizeof(buffer), format, parameters);
144
  va_end(parameters);
174
  va_end(parameters);
145
 
175
 
146
  Print(LOG_LEVEL_INFO, module, &buffer[0]);
176
  Print(LOG_LEVEL_INFO, module, &buffer[0]);
147
}
177
}
148
 
178
 
149
void Info(std::string module, std::string message)
179
void Info(std::string module, std::string message)
150
{
180
{
151
  Print(LOG_LEVEL_INFO, module, message);
181
  Print(LOG_LEVEL_INFO, module, message);
152
}
182
}
153
 
183
 
154
void Debug(std::string module, char* format, ...)
184
void Debug(std::string module, char* format, ...)
155
{
185
{
156
  char    buffer[kMaxBufferSize];
186
  char    buffer[kMaxBufferSize];
157
  va_list parameters;
187
  va_list parameters;
158
 
188
 
159
 
189
 
160
  /* Initialize variables */
190
  /* Initialize variables */
161
  memset(&buffer[0], 0, sizeof(buffer));
191
  memset(&buffer[0], 0, sizeof(buffer));
162
 
192
 
163
 
193
 
164
  /* Construct message */
194
  /* Construct message */
165
  va_start(parameters, format);
195
  va_start(parameters, format);
166
  vsnprintf(&buffer[0], sizeof(buffer), format, parameters);
196
  vsnprintf(&buffer[0], sizeof(buffer), format, parameters);
167
  va_end(parameters);
197
  va_end(parameters);
168
 
198
 
169
  Print(LOG_LEVEL_DEBUG, module, &buffer[0]);
199
  Print(LOG_LEVEL_DEBUG, module, &buffer[0]);
170
}
200
}
171
 
201
 
172
void Debug(std::string module, std::string message)
202
void Debug(std::string module, std::string message)
173
{
203
{
174
  Print(LOG_LEVEL_DEBUG, module, message);
204
  Print(LOG_LEVEL_DEBUG, module, message);
175
}
205
}
176
 
206
 
177
void Error(std::string module, char* format, ...)
207
void Error(std::string module, char* format, ...)
178
{
208
{
179
  char    buffer[kMaxBufferSize];
209
  char    buffer[kMaxBufferSize];
180
  va_list parameters;
210
  va_list parameters;
181
 
211
 
182
 
212
 
183
  /* Initialize variables */
213
  /* Initialize variables */
184
  memset(&buffer[0], 0, sizeof(buffer));
214
  memset(&buffer[0], 0, sizeof(buffer));
185
 
215
 
186
 
216
 
187
  /* Construct message */
217
  /* Construct message */
188
  va_start(parameters, format);
218
  va_start(parameters, format);
189
  vsnprintf(&buffer[0], sizeof(buffer), format, parameters);
219
  vsnprintf(&buffer[0], sizeof(buffer), format, parameters);
190
  va_end(parameters);
220
  va_end(parameters);
191
 
221
 
192
  Print(LOG_LEVEL_ERROR, module, &buffer[0]);
222
  Print(LOG_LEVEL_ERROR, module, &buffer[0]);
193
}
223
}
194
 
224
 
195
void Error(std::string module, std::string message)
225
void Error(std::string module, std::string message)
196
{
226
{
197
  Print(LOG_LEVEL_ERROR, module, message);
227
  Print(LOG_LEVEL_ERROR, module, message);
198
}
228
}
199
 
229
 
200
void Warning(std::string module, char* format, ...)
230
void Warning(std::string module, char* format, ...)
201
{
231
{
202
  char    buffer[kMaxBufferSize];
232
  char    buffer[kMaxBufferSize];
203
  va_list parameters;
233
  va_list parameters;
204
 
234
 
205
 
235
 
206
  /* Initialize variables */
236
  /* Initialize variables */
207
  memset(&buffer[0], 0, sizeof(buffer));
237
  memset(&buffer[0], 0, sizeof(buffer));
208
 
238
 
209
 
239
 
210
  /* Construct message */
240
  /* Construct message */
211
  va_start(parameters, format);
241
  va_start(parameters, format);
212
  vsnprintf(&buffer[0], sizeof(buffer), format, parameters);
242
  vsnprintf(&buffer[0], sizeof(buffer), format, parameters);
213
  va_end(parameters);
243
  va_end(parameters);
214
 
244
 
215
  Print(LOG_LEVEL_WARNING, module, &buffer[0]);
245
  Print(LOG_LEVEL_WARNING, module, &buffer[0]);
216
}
246
}
217
 
247
 
218
void Warning(std::string module, std::string message)
248
void Warning(std::string module, std::string message)
219
{
249
{
220
  Print(LOG_LEVEL_WARNING, module, message);
250
  Print(LOG_LEVEL_WARNING, module, message);
221
}
251
}
222
 
252
 
223
void Exception(std::string module, std::exception& exception)
253
void Exception(std::string module, std::exception& exception)
224
{
254
{
225
  Print(LOG_LEVEL_EXCEPTION, module, exception.what());
255
  Print(LOG_LEVEL_EXCEPTION, module, exception.what());
226
}
256
}
227
 
257
 
228
 
258
 
229
}; // namespace log
259
}; // namespace log
230
}; // namespace atom
260
}; // namespace atom
231
 
261