2 Copyright (c) 2004-2007, 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
34 #include <sys/types.h>
39 #define getpid GetCurrentProcessId
44 //------------------------------------------------------------------------------------------
48 Kumu::ILogSink::vLogf(LogType_t type, const char* fmt, va_list* list)
50 char buf[MaxLogLength];
51 vsnprintf(buf, MaxLogLength, fmt, *list);
53 WriteEntry(LogEntry(getpid(), type, buf));
56 //------------------------------------------------------------------------------------------
59 static Kumu::ILogSink* s_DefaultLogSink;
60 static Kumu::StdioLogSink s_StderrLogSink;
64 Kumu::SetDefaultLogSink(ILogSink* Sink)
66 s_DefaultLogSink = Sink;
69 // Returns the internal default sink.
71 Kumu::DefaultLogSink()
73 if ( s_DefaultLogSink == 0 )
74 s_DefaultLogSink = &s_StderrLogSink;
76 return *s_DefaultLogSink;
79 //------------------------------------------------------------------------------------------
83 Kumu::StdioLogSink::WriteEntry(const LogEntry& Entry)
87 if ( Entry.CreateStringWithFilter(m_filter, buf) )
88 fputs(buf.c_str(), m_stream);
91 //---------------------------------------------------------------------------------
96 Kumu::WinDbgLogSink::WriteEntry(const LogEntry& Entry)
100 if ( Entry.CreateStringWithFilter(m_filter, buf) )
101 ::OutputDebugString(buf.c_str());
105 //------------------------------------------------------------------------------------------
111 Kumu::StreamLogSink::WriteEntry(const LogEntry& Entry)
115 if ( Entry.CreateStringWithFilter(m_filter, buf) )
116 write(m_fd, buf.c_str(), buf.size());
120 //------------------------------------------------------------------------------------------
123 Kumu::LogEntry::CreateStringWithFilter(i32_t filter, std::string& out_buf) const
130 if ( (filter & LOG_ALLOW_CRIT) != 0 )
135 if ( (filter & LOG_ALLOW_ALERT) != 0 )
140 if ( (filter & LOG_ALLOW_NOTICE) != 0 )
145 if ( (filter & LOG_ALLOW_ERROR) != 0 )
150 if ( (filter & LOG_ALLOW_WARN) != 0 )
155 if ( (filter & LOG_ALLOW_INFO) != 0 )
160 if ( (filter & LOG_ALLOW_DEBUG) != 0 )
165 if ( (filter & LOG_ALLOW_DEFAULT) != 0 )
176 if ( (filter & LOG_ALLOW_TIMESTAMP) != 0 )
179 out_buf += Now.EncodeString(buf, 64);
181 if ( (filter & LOG_ALLOW_PID) != 0 )
185 if ( (filter & LOG_ALLOW_PID) != 0 )
187 snprintf(buf, 64, "%d", PID);
191 out_buf += "] " + Msg;
198 Kumu::LogEntry::ArchiveLength() const
200 return sizeof(ui32_t)
201 + EventTime.ArchiveLength()
203 + sizeof(ui32_t) + Msg.size();
208 Kumu::LogEntry::Archive(Kumu::MemIOWriter* Writer) const
210 if ( ! Writer->WriteUi32BE(PID) ) return false;
211 if ( ! EventTime.Archive(Writer) ) return false;
212 if ( ! Writer->WriteUi32BE(Type) ) return false;
213 if ( ! ArchiveString(*Writer, Msg) ) return false;
219 Kumu::LogEntry::Unarchive(Kumu::MemIOReader* Reader)
221 if ( ! Reader->ReadUi32BE(&PID) ) return false;
222 if ( ! EventTime.Unarchive(Reader) ) return false;
223 if ( ! Reader->ReadUi32BE((ui32_t*)&Type) ) return false;
224 if ( ! UnarchiveString(*Reader, Msg) ) return false;