2 Copyright (c) 2004-2006, John Hurst
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
8 1. Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13 3. The name of the author may not be used to endorse or promote products
14 derived from this software without specific prior written permission.
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 \brief message logging API
36 #include <KM_platform.h>
41 #define LOG_MSG_IMPL(t) va_list args; va_start(args, fmt); vLogf((t), fmt, &args); va_end(args)
46 // no log message will exceed this length
47 const ui32_t MaxLogLength = 512;
49 //---------------------------------------------------------------------------------
52 // Error and debug messages will be delivered to an object having this interface.
53 // The default implementation sends only LOG_ERROR and LOG_WARN messages to stderr.
54 // To receive LOG_INFO or LOG_DEBUG messages, or to send messages somewhere other
55 // than stderr, implement this interface and register an instance of your new class
56 // by calling SetDefaultLogSink().
60 enum LogType_t { LOG_DEBUG, LOG_INFO, LOG_WARN, LOG_ERROR,
61 LOG_NOTICE, LOG_ALERT, LOG_CRIT };
63 virtual ~ILogSink() {}
65 void Critical(const char* fmt, ...) { LOG_MSG_IMPL(LOG_CRIT); }
66 void Alert(const char* fmt, ...) { LOG_MSG_IMPL(LOG_ALERT); }
67 void Notice(const char* fmt, ...) { LOG_MSG_IMPL(LOG_NOTICE); }
68 void Error(const char* fmt, ...) { LOG_MSG_IMPL(LOG_ERROR); }
69 void Warn(const char* fmt, ...) { LOG_MSG_IMPL(LOG_WARN); }
70 void Info(const char* fmt, ...) { LOG_MSG_IMPL(LOG_INFO); }
71 void Debug(const char* fmt, ...) { LOG_MSG_IMPL(LOG_DEBUG); }
72 void Logf(ILogSink::LogType_t type, const char* fmt, ...) { LOG_MSG_IMPL(type); }
73 virtual void vLogf(LogType_t, const char*, va_list*) = 0; // log a formatted string with a va_list struct
76 // Sets the internal default sink to the given receiver. If the given value
77 // is zero, sets the default sink to the internally allocated stderr sink.
78 void SetDefaultLogSink(ILogSink* = 0);
80 // Returns the internal default sink.
81 ILogSink& DefaultLogSink();
84 class StdioLogSink : public ILogSink
88 KM_NO_COPY_CONSTRUCT(StdioLogSink);
91 StdioLogSink() : m_stream(stderr) {};
92 StdioLogSink(FILE* stream) : m_stream(stream) {}
93 virtual ~StdioLogSink() {}
94 virtual void vLogf(LogType_t, const char*, va_list*);
99 class WinDbgLogSink : public ILogSink
102 KM_NO_COPY_CONSTRUCT(WinDbgLogSink);
106 virtual ~WinDbgLogSink() {}
107 virtual void vLogf(LogType_t, const char*, va_list*);
113 class StreamLogSink : public ILogSink
117 KM_NO_COPY_CONSTRUCT(StreamLogSink);
121 StreamLogSink(int fd) : m_fd(fd) {}
122 virtual ~StreamLogSink() {}
123 virtual void vLogf(LogType_t, const char*, va_list*);