ginormo merge-back with Kumu, SMPTE MIC key and MPEG parser fix
[asdcplib.git] / src / KM_log.cpp
1 /*
2 Copyright (c) 2004-2006, John Hurst
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
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.
15
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.
26 */
27   /*! \file    KM_log.cpp
28     \version $Id$
29     \brief   message logging API
30   */
31
32 #include <KM_util.h>
33 #include <KM_log.h>
34 #include <sys/types.h>
35 #include <string.h>
36 #include <stdarg.h>
37
38 #ifdef KM_WIN32
39 #define getpid GetCurrentProcessId
40 #else
41 #include <unistd.h>
42 #endif
43
44
45 void
46 Kumu::StdioLogSink::vLogf(ILogSink::LogType_t type, const char* fmt, va_list* list)
47 {
48   AutoMutex L(m_Lock);
49
50   switch ( type )
51     {
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());
60     }
61
62   vfprintf(m_stream, fmt, *list);
63 }
64
65 static Kumu::ILogSink* s_DefaultLogSink;
66 static Kumu::StdioLogSink s_StderrLogSink;
67
68 //
69 void
70 Kumu::SetDefaultLogSink(ILogSink* Sink)
71 {
72     s_DefaultLogSink = Sink;
73 }
74
75 // Returns the internal default sink.
76 Kumu::ILogSink&
77 Kumu::DefaultLogSink()
78 {
79   if ( s_DefaultLogSink == 0 )
80     s_DefaultLogSink = &s_StderrLogSink;
81
82   return *s_DefaultLogSink;
83 }
84
85 //---------------------------------------------------------------------------------
86 #ifdef KM_WIN32
87
88 //
89 void
90 Kumu::WinDbgLogSink::vLogf(ILogSink::LogType_t type, const char* fmt, va_list* list)
91 {
92   AutoMutex L(m_Lock);
93   char msg_buf[MaxLogLength];
94
95   DWORD pid = GetCurrentProcessId();
96
97   switch ( type )
98     {
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);
107     }
108   
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);
113 }
114
115 #else
116
117 void
118 Kumu::StreamLogSink::vLogf(ILogSink::LogType_t type, const char* fmt, va_list* list)
119 {
120   AutoMutex L(m_Lock);
121   char msg_buf[MaxLogLength];
122   char ts_buf[MaxLogLength];
123   Timestamp Now;
124
125   switch ( type )
126     {
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());
143     }
144   
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));
149 }
150 #endif
151
152
153 //
154 // end
155 //