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
34 #include <sys/types.h>
39 #define getpid GetCurrentProcessId
46 Kumu::StdioLogSink::vLogf(ILogSink::LogType_t type, const char* fmt, va_list* list)
52 case LOG_CRIT: fprintf(m_stream, "[%d CRT]: ", getpid()); break;
53 case LOG_ALERT: fprintf(m_stream, "[%d ALR]: ", getpid()); break;
54 case LOG_NOTICE: fprintf(m_stream, "[%d NTC]: ", getpid()); break;
55 case LOG_ERROR: fprintf(m_stream, "[%d ERR]: ", getpid()); break;
56 case LOG_WARN: fprintf(m_stream, "[%d WRN]: ", getpid()); break;
57 case LOG_INFO: fprintf(m_stream, "[%d INF]: ", getpid()); break;
58 case LOG_DEBUG: fprintf(m_stream, "[%d DBG]: ", getpid()); break;
59 default: fprintf(m_stream, "[%d DFL]: ", getpid());
62 vfprintf(m_stream, fmt, *list);
65 static Kumu::ILogSink* s_DefaultLogSink;
66 static Kumu::StdioLogSink s_StderrLogSink;
70 Kumu::SetDefaultLogSink(ILogSink* Sink)
72 s_DefaultLogSink = Sink;
75 // Returns the internal default sink.
77 Kumu::DefaultLogSink()
79 if ( s_DefaultLogSink == 0 )
80 s_DefaultLogSink = &s_StderrLogSink;
82 return *s_DefaultLogSink;
85 //---------------------------------------------------------------------------------
90 Kumu::WinDbgLogSink::vLogf(ILogSink::LogType_t type, const char* fmt, va_list* list)
93 char msg_buf[MaxLogLength];
95 DWORD pid = GetCurrentProcessId();
99 case LOG_CRIT: snprintf(msg_buf, MaxLogLength, "[%d CRT]: ", pid); break;
100 case LOG_ALERT: snprintf(msg_buf, MaxLogLength, "[%d ALR]: ", pid); break;
101 case LOG_NOTICE: snprintf(msg_buf, MaxLogLength, "[%d NTC]: ", pid); break;
102 case LOG_ERROR: snprintf(msg_buf, MaxLogLength, "[%d ERR]: ", pid); break;
103 case LOG_WARN: snprintf(msg_buf, MaxLogLength, "[%d WRN]: ", pid); break;
104 case LOG_INFO: snprintf(msg_buf, MaxLogLength, "[%d INF]: ", pid); break;
105 case LOG_DEBUG: snprintf(msg_buf, MaxLogLength, "[%d DBG]: ", pid); break;
106 default: snprintf(msg_buf, MaxLogLength, "[%d DFL]: ", pid);
109 ui32_t len = strlen(msg_buf);
110 vsnprintf(msg_buf + len, MaxLogLength - len, fmt, *list);
111 msg_buf[MaxLogLength-1] = 0;
112 ::OutputDebugString(msg_buf);
118 Kumu::StreamLogSink::vLogf(ILogSink::LogType_t type, const char* fmt, va_list* list)
121 char msg_buf[MaxLogLength];
122 char ts_buf[MaxLogLength];
127 case LOG_CRIT: snprintf(msg_buf, MaxLogLength, "[%s %d CRT]: ",
128 Now.EncodeString(ts_buf, MaxLogLength), getpid()); break;
129 case LOG_ALERT: snprintf(msg_buf, MaxLogLength, "[%s %d ALR]: ",
130 Now.EncodeString(ts_buf, MaxLogLength), getpid()); break;
131 case LOG_NOTICE: snprintf(msg_buf, MaxLogLength, "[%s %d NTC]: ",
132 Now.EncodeString(ts_buf, MaxLogLength), getpid()); break;
133 case LOG_ERROR: snprintf(msg_buf, MaxLogLength, "[%s %d ERR]: ",
134 Now.EncodeString(ts_buf, MaxLogLength), getpid()); break;
135 case LOG_WARN: snprintf(msg_buf, MaxLogLength, "[%s %d WRN]: ",
136 Now.EncodeString(ts_buf, MaxLogLength), getpid()); break;
137 case LOG_INFO: snprintf(msg_buf, MaxLogLength, "[%s %d INF]: ",
138 Now.EncodeString(ts_buf, MaxLogLength), getpid()); break;
139 case LOG_DEBUG: snprintf(msg_buf, MaxLogLength, "[%s %d DBG]: ",
140 Now.EncodeString(ts_buf, MaxLogLength), getpid()); break;
141 default: snprintf(msg_buf, MaxLogLength, "[%s %d DFL]: ",
142 Now.EncodeString(ts_buf, MaxLogLength), getpid());
145 ui32_t len = strlen(msg_buf);
146 vsnprintf(msg_buf + len, MaxLogLength - len, fmt, *list);
147 msg_buf[MaxLogLength-1] = 0;
148 write(m_fd, msg_buf, strlen(msg_buf));