~registry/scalestack/trunk

« back to all changes in this revision

Viewing changes to scalestack/kernel/logger.h

  • Committer: Eric Day
  • Date: 2010-12-04 17:17:34 UTC
  • mfrom: (56.1.9)
  • Revision ID: git-v1:9c392b034d652023a4b28ae2976aca128bb856c2
Merged Eric's branch with style updates, config file support, and other cleanup.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Scale Stack
 
3
 *
 
4
 * Copyright (C) 2010 Eric Day (eday@oddments.org)
 
5
 * All rights reserved.
 
6
 *
 
7
 * Use and distribution licensed under the BSD license. See the
 
8
 * COPYING file in the root project directory for full text.
 
9
 */
 
10
 
 
11
/**
 
12
 * @file
 
13
 * @brief Kernel Logger Base Class
 
14
 */
 
15
 
 
16
#ifndef SCALESTACK_KERNEL_LOGGER_H
 
17
#define SCALESTACK_KERNEL_LOGGER_H
 
18
 
 
19
#include <cstdarg>
 
20
 
 
21
#include <scalestack/common/exception.h>
 
22
#include <scalestack/kernel/log.h>
 
23
 
 
24
namespace scalestack
 
25
{
 
26
namespace kernel
 
27
{
 
28
 
 
29
/**
 
30
 * Base class available for kernel classes that produce log messages.
 
31
 */
 
32
class SCALESTACK_API logger
 
33
{
 
34
public:
 
35
 
 
36
  logger(size_t threshold);
 
37
 
 
38
  virtual ~logger();
 
39
 
 
40
  /**
 
41
   * Set the threshold for logging messages.
 
42
   *
 
43
   * @param[in] threshold The new log threshold to set.
 
44
   */
 
45
  void set_threshold(size_t threshold);
 
46
 
 
47
  /**
 
48
   * Log a fatal message with the formatted string if log threshold is met.
 
49
   *
 
50
   * @param[in] format Message format string.
 
51
   */
 
52
  void log_fatal(const char* format, ...) const;
 
53
 
 
54
  /**
 
55
   * Log an error message with the formatted string if log threshold is met.
 
56
   *
 
57
   * @param[in] format Message format string.
 
58
   */
 
59
  void log_error(const char* format, ...) const;
 
60
 
 
61
  /**
 
62
   * Log a notice message with the formatted string if log threshold is met.
 
63
   *
 
64
   * @param[in] format Message format string.
 
65
   */
 
66
  void log_notice(const char* format, ...) const;
 
67
 
 
68
  /**
 
69
   * Log an info message with the formatted string if log threshold is met.
 
70
   *
 
71
   * @param[in] format Message format string.
 
72
   */
 
73
  void log_info(const char* format, ...) const;
 
74
 
 
75
  /**
 
76
   * Log a debug message with the formatted string if log threshold is met.
 
77
   *
 
78
   * @param[in] format Message format string.
 
79
   */
 
80
  void log_debug(const char* format, ...) const;
 
81
 
 
82
protected:
 
83
 
 
84
  size_t _threshold;
 
85
 
 
86
private:
 
87
 
 
88
  /**
 
89
   * Don't allow copying of objects.
 
90
   */
 
91
  logger(const logger&);
 
92
 
 
93
  /**
 
94
   * Don't allow assignment of objects.
 
95
   */
 
96
  logger& operator=(const logger&);
 
97
 
 
98
  /**
 
99
   * Set a fatal error message. This is used so fatal messages can be caught
 
100
   * for error strings even if logging threshold is not met.
 
101
   *
 
102
   * @param[in] format Message format string.
 
103
   * @param[in] arguments Argument list for formatted message.
 
104
   */
 
105
  virtual void _set_fatal_message(const char* format,
 
106
                                  std::va_list arguments) const = 0;
 
107
 
 
108
  /**
 
109
   * Log the message.
 
110
   *
 
111
   * @param[in] log_level Log level for this message.
 
112
   * @param[in] format Message format string.
 
113
   * @param[in] arguments Argument list for formatted message.
 
114
   */
 
115
  virtual void _log_message(log::level log_level,
 
116
                            const char* format,
 
117
                            std::va_list arguments) const = 0;
 
118
};
 
119
 
 
120
/*
 
121
 * Public methods.
 
122
 */
 
123
 
 
124
inline logger::logger(size_t threshold):
 
125
  _threshold(threshold)
 
126
{
 
127
}
 
128
 
 
129
inline logger::~logger()
 
130
{
 
131
}
 
132
 
 
133
inline void logger::set_threshold(size_t threshold)
 
134
{
 
135
  _threshold = threshold;
 
136
}
 
137
 
 
138
inline void logger::log_fatal(const char* format, ...) const
 
139
{
 
140
  std::va_list arguments;
 
141
  va_start(arguments, format);
 
142
 
 
143
  /*
 
144
   * We know this is going to trigger a catch because fatal messages throw
 
145
   * exceptions, so this is done so we can call va_end before passing on
 
146
   * the exception.
 
147
   */
 
148
  try
 
149
  {
 
150
    if (_threshold >= 1)
 
151
      _log_message(log::LEVEL_FATAL, format, arguments);
 
152
    else
 
153
      _set_fatal_message(format, arguments);
 
154
  }
 
155
  catch (common::exception&)
 
156
  {
 
157
    va_end(arguments);
 
158
    throw;
 
159
  }
 
160
 
 
161
  va_end(arguments);
 
162
}
 
163
 
 
164
inline void logger::log_error(const char* format, ...) const
 
165
{
 
166
  if (_threshold >= 1)
 
167
  {
 
168
    std::va_list arguments;
 
169
    va_start(arguments, format);
 
170
    _log_message(log::LEVEL_ERROR, format, arguments);
 
171
    va_end(arguments);
 
172
  }
 
173
}
 
174
 
 
175
inline void logger::log_notice(const char* format, ...) const
 
176
{
 
177
  if (_threshold >= 1)
 
178
  {
 
179
    std::va_list arguments;
 
180
    va_start(arguments, format);
 
181
    _log_message(log::LEVEL_NOTICE, format, arguments);
 
182
    va_end(arguments);
 
183
  }
 
184
}
 
185
 
 
186
inline void logger::log_info(const char* format, ...) const
 
187
{
 
188
  if (_threshold >= 2)
 
189
  {
 
190
    std::va_list arguments;
 
191
    va_start(arguments, format);
 
192
    _log_message(log::LEVEL_INFO, format, arguments);
 
193
    va_end(arguments);
 
194
  }
 
195
}
 
196
 
 
197
inline void logger::log_debug(const char* format, ...) const
 
198
{
 
199
  if (_threshold >= 3)
 
200
  {
 
201
    std::va_list arguments;
 
202
    va_start(arguments, format);
 
203
    _log_message(log::LEVEL_DEBUG, format, arguments);
 
204
    va_end(arguments);
 
205
  }
 
206
}
 
207
 
 
208
} /* namespace kernel */
 
209
} /* namespace scalestack */
 
210
 
 
211
#endif /* SCALESTACK_KERNEL_LOGGER_H */