Subversion Repositories HomeAutomation

Rev

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