Subversion Repositories HomeAutomation

Rev

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

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