Subversion Repositories HomeAutomation

Rev

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

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