ginormo merge-back with Kumu, SMPTE MIC key and MPEG parser fix
[asdcplib.git] / src / KM_log.h
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.h
28     \version $Id$
29     \brief   message logging API
30   */
31
32
33 #ifndef _KM_LOG_H_
34 #define _KM_LOG_H_
35
36 #include <KM_platform.h>
37 #include <KM_mutex.h>
38 #include <stdarg.h>
39 #include <errno.h>
40
41 #define LOG_MSG_IMPL(t) va_list args; va_start(args, fmt); vLogf((t), fmt, &args); va_end(args)
42
43
44 namespace Kumu
45 {
46   // no log message will exceed this length
47   const ui32_t MaxLogLength = 512;
48
49   //---------------------------------------------------------------------------------
50   // message logging
51
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().
57   class ILogSink
58     {
59     public:
60       enum LogType_t { LOG_DEBUG, LOG_INFO, LOG_WARN, LOG_ERROR,
61                        LOG_NOTICE, LOG_ALERT, LOG_CRIT };
62
63       virtual ~ILogSink() {}
64
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
74     };
75
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);
79
80   // Returns the internal default sink.
81   ILogSink& DefaultLogSink();
82
83   //
84   class StdioLogSink : public ILogSink
85     {
86       Mutex m_Lock;
87       FILE* m_stream;
88       KM_NO_COPY_CONSTRUCT(StdioLogSink);
89
90     public:
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*);
95     };
96
97 #ifdef KM_WIN32
98   //
99   class WinDbgLogSink : public ILogSink
100     {
101       Mutex m_Lock;
102       KM_NO_COPY_CONSTRUCT(WinDbgLogSink);
103
104     public:
105       WinDbgLogSink() {}
106       virtual ~WinDbgLogSink() {}
107       virtual void vLogf(LogType_t, const char*, va_list*);
108     };
109
110 #else
111
112   //
113   class StreamLogSink : public ILogSink
114     {
115       Mutex m_Lock;
116       int   m_fd;
117       KM_NO_COPY_CONSTRUCT(StreamLogSink);
118       StreamLogSink();
119
120     public:
121       StreamLogSink(int fd) : m_fd(fd) {}
122       virtual ~StreamLogSink() {}
123       virtual void vLogf(LogType_t, const char*, va_list*);
124     };
125 #endif
126
127 } // namespace Kumu
128
129 #endif // _KM_LOG_H_
130
131 //
132 // end KM_log.h
133 //