9#ifndef CLUE_CLUE_H_INCLUDED
10#define CLUE_CLUE_H_INCLUDED
16#define clue_VERSION clue_STRINGIFY(clue_MAJOR) "." clue_STRINGIFY(clue_MINOR) "." clue_STRINGIFY(clue_PATCH)
18#define clue_STRINGIFY(x) clue_STRINGIFY_(x)
19#define clue_STRINGIFY_(x) #x
26#define clue_COMPILER_IS_MSVC
27#if (_MSC_VER >= 1200) && (_MSC_VER < 1300)
28#define clue_COMPILER_IS_MSVC6
32#ifdef clue_COMPILER_IS_MSVC
34#pragma warning(disable : 4996)
37#ifndef clue_NO_SHORT_NAMES
40#define clue_LOG_MODULE_NAME LOG_MODULE_NAME
44#define clue_LOG_LEVEL LOG_LEVEL
48#define clue_LOG_LEVEL_BUILD LOG_LEVEL_BUILD
52#define clue_LOG_TO_CONSOLE LOG_TO_CONSOLE
56#define clue_LOG_TO_FILE LOG_TO_FILE
60#define clue_LOG_TO_DEBUGGER LOG_TO_DEBUGGER
63#ifdef LOG_TO_DEBUGGER_WINDOWS
64#define clue_LOG_TO_DEBUGGER_WINDOWS LOG_TO_DEBUGGER_WINDOWS
68#define clue_LOG_TO_EVENTLOG LOG_TO_EVENTLOG
72#define clue_LOG_TO_STRING LOG_TO_STRING
76#define clue_LOG_TO_SYSLOG LOG_TO_SYSLOG
80#define clue_LOG_EXPRESSION LOG_EXPRESSIONLOG_TO_SYSLOG
87#ifndef clue_OMIT_UNUSED_LOG_EXPRESSION
88#define clue_OMIT_UNUSED_LOG_EXPRESSION 0
91#if !defined(clue_LOG_TO_CONSOLE) && \
92 !defined(clue_LOG_TO_FILE) && \
93 !defined(clue_LOG_TO_STRING) && \
94 !defined(clue_LOG_TO_DEBUGGER) && \
95 !defined(clue_LOG_TO_EVENTLOG) && \
96 !defined(clue_LOG_TO_SYSLOG)
98#define clue_LOG_TO_DEBUGGER
99#elif defined(NTS_TO_BE_DETERMINED_UNIX)
100#define clue_LOG_TO_SYSLOG
102#define clue_LOG_TO_CONSOLE
106#if defined(clue_LOG_TO_CONSOLE) + \
107 defined(clue_LOG_TO_FILE) + \
108 defined(clue_LOG_TO_STRING) + \
109 defined(clue_LOG_TO_DEBUGGER) + \
110 defined(clue_LOG_TO_EVENTLOG) + \
111 defined(clue_LOG_TO_SYSLOG) > \
113#error Please specify one, or none of [clue_]LOG_TO_CONSOLE, [clue_]LOG_TO_FILE, [clue_]LOG_TO_STRING, [clue_]LOG_TO_DEBUGGER [clue_]LOG_TO_EVENTLOG and [clue_]LOG_TO_SYSLOG
117#ifdef clue_LOG_TO_DEBUGGER
118#define clue_LOG_TO_DEBUGGER_WINDOWS
121#ifndef clue_NO_SHORT_NAMES
123#define LOG_SEV_NONE clue_LOG_SEV_NONE
124#define LOG_SEV_EMERGENCY clue_LOG_SEV_EMERGENCY
125#define LOG_SEV_ALERT clue_LOG_SEV_ALERT
126#define LOG_SEV_CRITICAL clue_LOG_SEV_CRITICAL
127#define LOG_SEV_ERROR clue_LOG_SEV_ERROR
128#define LOG_SEV_WARNING clue_LOG_SEV_WARNING
129#define LOG_SEV_NOTICE clue_LOG_SEV_NOTICE
130#define LOG_SEV_INFO clue_LOG_SEV_INFO
131#define LOG_SEV_DEBUG clue_LOG_SEV_DEBUG
132#define LOG_SEV_MAX clue_LOG_SEV_MAX
134#ifndef clue_LOG_TO_SYSLOG
135#define LOG_EMERGENCY clue_LOG_EMERGENCY
136#define LOG_ALERT clue_LOG_ALERT
137#define LOG_CRITICAL clue_LOG_CRITICAL
138#define LOG_ERROR clue_LOG_ERROR
139#define LOG_WARNING clue_LOG_WARNING
140#define LOG_NOTICE clue_LOG_NOTICE
141#define LOG_INFO clue_LOG_INFO
142#define LOG_DEBUG clue_LOG_DEBUG
145#define LOG_LOGGED_SEVERITIES clue_LOG_LOGGED_SEVERITIES
146#define LOG_EXPRESSION clue_LOG_EXPRESSION
150#ifdef clue_LOG_TO_CONSOLE
154#ifdef clue_LOG_TO_FILE
158#ifdef clue_LOG_TO_DEBUGGER_WINDOWS
162#ifdef clue_LOG_TO_DEBUGGER_UNIX
163#error log to debugger under Unix not implemented
166#ifdef clue_LOG_TO_EVENTLOG
170#ifdef clue_LOG_TO_SYSLOG
174#define clue_LOG_SEV_NONE -1
175#define clue_LOG_SEV_EMERGENCY 0
176#define clue_LOG_SEV_ALERT 1
177#define clue_LOG_SEV_CRITICAL 2
178#define clue_LOG_SEV_ERROR 3
179#define clue_LOG_SEV_WARNING 4
180#define clue_LOG_SEV_NOTICE 5
181#define clue_LOG_SEV_INFO 6
182#define clue_LOG_SEV_DEBUG 7
183#define clue_LOG_SEV_MAX 7
185#ifndef clue_LOG_LEVEL
186#define clue_LOG_LEVEL clue_LOG_SEV_DEBUG
189#ifndef clue_LOG_LEVEL_BUILD
190#define clue_LOG_LEVEL_BUILD clue_LOG_SEV_DEBUG
193#define clue_LOG_SEV_NONE_TEXT "[clue]"
194#define clue_LOG_SEV_EMERGENCY_TEXT "Emergency"
195#define clue_LOG_SEV_ALERT_TEXT "Alert"
196#define clue_LOG_SEV_CRITICAL_TEXT "Critical"
197#define clue_LOG_SEV_ERROR_TEXT "Error"
198#define clue_LOG_SEV_WARNING_TEXT "Warning"
199#define clue_LOG_SEV_NOTICE_TEXT "Notice"
200#define clue_LOG_SEV_INFO_TEXT "Info"
201#define clue_LOG_SEV_DEBUG_TEXT "Debug"
203#ifndef clue_LOG_MODULE_NAME
204#define clue_LOG_MODULE_NAME ""
207#ifndef clue_LOG_PREFIX_WIDTH
208#define clue_LOG_PREFIX_WIDTH sizeof(clue_LOG_SEV_EMERGENCY_TEXT)
211#define clue_is_active(severity) \
212 clue::is_true(severity <= clue_LOG_LEVEL)
214#define clue_is_active_build(severity) \
215 clue::is_true(clue_IS_ACTIVE_BUILD(severity))
217#define clue_IS_ACTIVE_BUILD(severity) \
218 (severity <= clue_LOG_LEVEL_BUILD)
220#define clue_LOG_LOGGED_SEVERITIES() \
221 clue_LOG_EXPRESSION(clue_LOG_SEV_NONE, clue::to_severities_text(clue_LOG_LEVEL_BUILD))
223#define clue_LOG_NO_EXPRESSION() \
226 } while (clue::is_true(false))
228#define clue_IS_ACTIVE(severity) \
229 (clue_IS_ACTIVE_BUILD(severity) || !clue_OMIT_UNUSED_LOG_EXPRESSION)
231#if clue_IS_ACTIVE(clue_LOG_SEV_EMERGENCY)
232#define clue_LOG_EMERGENCY(expr) clue_LOG_EXPRESSION(clue_LOG_SEV_EMERGENCY, expr)
234#define clue_LOG_EMERGENCY(expr) clue_LOG_NO_EXPRESSION()
237#if clue_IS_ACTIVE(clue_LOG_SEV_ALERT)
238#define clue_LOG_ALERT(expr) clue_LOG_EXPRESSION(clue_LOG_SEV_ALERT, expr)
240#define clue_LOG_ALERT(expr) clue_LOG_NO_EXPRESSION()
243#if clue_IS_ACTIVE(clue_LOG_SEV_CRITICAL)
244#define clue_LOG_CRITICAL(expr) clue_LOG_EXPRESSION(clue_LOG_SEV_CRITICAL, expr)
246#define clue_LOG_CRITICAL(expr) clue_LOG_NO_EXPRESSION()
249#if clue_IS_ACTIVE(clue_LOG_SEV_ERROR)
250#define clue_LOG_ERROR(expr) clue_LOG_EXPRESSION(clue_LOG_SEV_ERROR, expr)
252#define clue_LOG_ERROR(expr) clue_LOG_NO_EXPRESSION()
255#if clue_IS_ACTIVE(clue_LOG_SEV_WARNING)
256#define clue_LOG_WARNING(expr) clue_LOG_EXPRESSION(clue_LOG_SEV_WARNING, expr)
258#define clue_LOG_WARNING(expr) clue_LOG_NO_EXPRESSION()
261#if clue_IS_ACTIVE(clue_LOG_SEV_NOTICE)
262#define clue_LOG_NOTICE(expr) clue_LOG_EXPRESSION(clue_LOG_SEV_NOTICE, expr)
264#define clue_LOG_NOTICE(expr) clue_LOG_NO_EXPRESSION()
267#if clue_IS_ACTIVE(clue_LOG_SEV_INFO)
268#define clue_LOG_INFO(expr) clue_LOG_EXPRESSION(clue_LOG_SEV_INFO, expr)
270#define clue_LOG_INFO(expr) clue_LOG_NO_EXPRESSION()
273#if clue_IS_ACTIVE(clue_LOG_SEV_DEBUG) && !defined(NDEBUG)
274#define clue_LOG_DEBUG(expr) clue_LOG_EXPRESSION(clue_LOG_SEV_DEBUG, expr)
276#define clue_LOG_DEBUG(expr) clue_LOG_NO_EXPRESSION()
279#if defined(clue_LOG_TO_CONSOLE) && !defined(clue_LOG_EXPRESSION)
280#define clue_LOG_EXPRESSION(severity, expr) \
283 if (clue_is_active_build(severity)) \
285 if (clue_is_active(severity)) \
287 std::clog << clue::now_text() << std::setw(clue_LOG_PREFIX_WIDTH) << clue::to_severity_text(severity) << clue::to_module_text(clue_LOG_MODULE_NAME) << ": " << expr << "\n"; \
290 } while (clue::is_true(false))
293#if defined(clue_LOG_TO_FILE) && !defined(clue_LOG_EXPRESSION)
294#define clue_LOG_EXPRESSION(severity, expr) \
297 if (clue_is_active_build(severity)) \
299 if (clue_is_active(severity)) \
301 clue::filelog() << clue::now_text() << std::setw(clue_LOG_PREFIX_WIDTH) << clue::to_severity_text(severity) << clue::to_module_text(clue_LOG_MODULE_NAME) << ": " << expr << "\n"; \
304 } while (clue::is_true(false))
307#if defined(clue_LOG_TO_STRING) && !defined(clue_LOG_EXPRESSION)
308#define clue_LOG_EXPRESSION(sev, expr) \
309 clue_LOG_STRING_EXPRESSION(the_log(), sev, expr)
312#if defined(clue_LOG_TO_STRING) && !defined(clue_LOG_STRING_EXPRESSION)
313#define clue_LOG_STRING_EXPRESSION(log, sev, expr) \
316 if (clue_is_active_build(sev)) \
318 if (clue_is_active(sev)) \
321 log << clue_LOG_MODULE_NAME << expr; \
324 } while (clue::is_true(false))
327#if defined(clue_LOG_TO_DEBUGGER_WINDOWS) && !defined(clue_LOG_EXPRESSION)
328#define clue_LOG_EXPRESSION(severity, expr) \
331 if (clue_is_active_build(severity)) \
333 if (clue_is_active(severity)) \
335 clue::windbg() << std::setw(clue_LOG_PREFIX_WIDTH) << clue::to_severity_text(severity) << clue::to_module_text(clue_LOG_MODULE_NAME) << ": " << expr; \
338 } while (clue::is_true(false))
341#if defined(clue_LOG_TO_EVENTLOG) && !defined(clue_LOG_EXPRESSION)
342#define clue_LOG_EXPRESSION(severity, expr) \
345 if (clue_is_active_build(severity)) \
347 if (clue_is_active(severity)) \
349 clue::evtlog(severity, clue_LOG_MODULE_NAME) << clue::text_with_or("", clue_LOG_MODULE_NAME, ": ", "") << expr; \
352 } while (clue::is_true(false))
355#if defined(clue_LOG_TO_SYSLOG) && !defined(clue_LOG_EXPRESSION)
356#define clue_LOG_EXPRESSION(severity, expr) \
359 if (clue_is_active_build(severity)) \
361 if (clue_is_active(severity)) \
363 clue::syslog(severity) << clue_LOG_MODULE_NAME << ": " << expr; \
366 } while (clue::is_true(false))
372 inline bool is_true(
bool const on) {
return on; }
374 inline std::string
text_or(std::string
const &text, std::string
const &or_text)
376 return text.length() ? text : or_text;
379 inline std::string
text_with_or(std::string
const &prefix, std::string
const &text, std::string
const &postfix, std::string
const &or_text)
381 return text.length() ? prefix + text + postfix : or_text;
398 std::string
const cont[] =
409 return cont[severity];
412 inline std::string
to_severities_text(
int const level, std::string
const &postfix =
".", std::string
const &result =
"")
415 return result + postfix;
422#ifdef clue_NO_TIMESTAMP
426 inline std::string
now_text() {
return ""; }
433#ifdef clue_COMPILER_IS_MSVC6
449 const std::time_t now = std::time(NULL);
452 if (std::strftime(mbstr, 100,
"%Y-%m-%dT%H:%M:%S", std::localtime(&now)))
461#ifdef clue_LOG_TO_FILE
475 std::ofstream os(clue_LOG_TO_FILE, std::ios_base::app);
479 template <
typename T>
480 filelog &operator<<(T
const &that)
487 std::ostringstream stream;
494#ifdef clue_LOG_TO_STRING
508 stream.str(std::string());
511 void severity(
int const sev)
521 std::string text()
const
526 template <
typename T>
527 strlog &operator<<(T
const &that)
535 std::ostringstream stream;
538 inline strlog &the_log()
548#ifdef clue_LOG_TO_DEBUGGER_WINDOWS
561 OutputDebugString(stream.str().c_str());
564 template <
typename T>
565 windbg &operator<<(T
const &that)
572 std::ostringstream stream;
579#ifdef clue_LOG_TO_EVENTLOG
584 inline int to_eventlog_severity(
int severity)
591 return EVENTLOG_INFORMATION_TYPE;
593 return EVENTLOG_ERROR_TYPE;
595 return EVENTLOG_ERROR_TYPE;
597 return EVENTLOG_ERROR_TYPE;
599 return EVENTLOG_ERROR_TYPE;
601 return EVENTLOG_WARNING_TYPE;
603 return EVENTLOG_INFORMATION_TYPE;
605 return EVENTLOG_INFORMATION_TYPE;
608 return EVENTLOG_INFORMATION_TYPE;
615 evtlog(
int const severity, std::string
const &module)
616 : severity(severity), module(module), stream() {}
621 const std::string text = stream.str();
622 const char *strings[] = {
626 const ::HANDLE hlog = ::RegisterEventSource(
627 0,
text_or(module,
"[clue]").c_str());
632 to_eventlog_severity(severity)
649 ::DeregisterEventSource(hlog);
652 template <
typename T>
653 evtlog &operator<<(T
const &that)
661 const std::string module;
662 std::ostringstream stream;
669#ifdef clue_LOG_TO_SYSLOG
674 inline int to_syslog_severity(
int severity)
705 syslog(
int const severity)
706 : severity(severity), stream() {}
711 ::openlog(NULL, LOG_PID, LOG_USER);
712 ::syslog(to_syslog_severity(severity),
"%s", stream.str().c_str());
716 template <
typename T>
717 syslog &operator<<(T
const &that)
725 std::ostringstream stream;
732#ifdef clue_COMPILER_IS_MSVC
#define clue_LOG_SEV_DEBUG_TEXT
Definition clue.hpp:201
#define clue_LOG_SEV_NONE
Definition clue.hpp:174
#define clue_LOG_SEV_NOTICE
Definition clue.hpp:180
#define LOG_ALERT
Definition clue.hpp:136
#define clue_LOG_SEV_NOTICE_TEXT
Definition clue.hpp:199
#define clue_LOG_SEV_CRITICAL
Definition clue.hpp:177
#define clue_LOG_SEV_DEBUG
Definition clue.hpp:182
#define clue_LOG_SEV_INFO_TEXT
Definition clue.hpp:200
#define clue_LOG_SEV_ALERT_TEXT
Definition clue.hpp:195
#define LOG_DEBUG
Definition clue.hpp:142
#define clue_LOG_SEV_WARNING_TEXT
Definition clue.hpp:198
#define clue_LOG_SEV_MAX
Definition clue.hpp:183
#define clue_LOG_SEV_EMERGENCY
Definition clue.hpp:175
#define clue_LOG_SEV_ERROR_TEXT
Definition clue.hpp:197
#define clue_LOG_SEV_NONE_TEXT
Definition clue.hpp:193
#define clue_LOG_SEV_ERROR
Definition clue.hpp:178
#define clue_LOG_SEV_WARNING
Definition clue.hpp:179
#define clue_LOG_SEV_INFO
Definition clue.hpp:181
#define clue_LOG_SEV_CRITICAL_TEXT
Definition clue.hpp:196
#define clue_LOG_SEV_ALERT
Definition clue.hpp:176
#define LOG_NOTICE
Definition clue.hpp:140
#define LOG_WARNING
Definition clue.hpp:139
#define LOG_INFO
Definition clue.hpp:141
#define clue_LOG_SEV_EMERGENCY_TEXT
Definition clue.hpp:194
std::string text_with_or(std::string const &prefix, std::string const &text, std::string const &postfix, std::string const &or_text)
Definition clue.hpp:379
bool is_true(bool const on)
Definition clue.hpp:372
std::string now_text()
Definition clue.hpp:446
std::string to_severities_text(int const level, std::string const &postfix=".", std::string const &result="")
Definition clue.hpp:412
std::string to_severity_text(int const severity)
Definition clue.hpp:389
std::string to_module_text(std::string const &module)
Definition clue.hpp:384
std::string text_or(std::string const &text, std::string const &or_text)
Definition clue.hpp:374