OpenNI 1.5.7
XnLogWriterBase.h
Go to the documentation of this file.
00001 /*****************************************************************************
00002 *                                                                            *
00003 *  OpenNI 1.x Alpha                                                          *
00004 *  Copyright (C) 2012 PrimeSense Ltd.                                        *
00005 *                                                                            *
00006 *  This file is part of OpenNI.                                              *
00007 *                                                                            *
00008 *  Licensed under the Apache License, Version 2.0 (the "License");           *
00009 *  you may not use this file except in compliance with the License.          *
00010 *  You may obtain a copy of the License at                                   *
00011 *                                                                            *
00012 *      http://www.apache.org/licenses/LICENSE-2.0                            *
00013 *                                                                            *
00014 *  Unless required by applicable law or agreed to in writing, software       *
00015 *  distributed under the License is distributed on an "AS IS" BASIS,         *
00016 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  *
00017 *  See the License for the specific language governing permissions and       *
00018 *  limitations under the License.                                            *
00019 *                                                                            *
00020 *****************************************************************************/
00021 #ifndef __XN_LOG_WRITER_BASE_H__
00022 #define __XN_LOG_WRITER_BASE_H__
00023 
00024 //---------------------------------------------------------------------------
00025 // Includes
00026 //---------------------------------------------------------------------------
00027 #include <XnLogTypes.h>
00028 #include <XnLog.h>
00029 
00030 //---------------------------------------------------------------------------
00031 // Types
00032 //---------------------------------------------------------------------------
00033 class XnLogWriterBase
00034 {
00035 public:
00036     XnLogWriterBase() : m_bRegistered(FALSE)
00037     {
00038         m_cObject.pCookie = this;
00039         m_cObject.WriteEntry = WriteEntryCallback;
00040         m_cObject.WriteUnformatted = WriteUnformattedCallback;
00041         m_cObject.OnConfigurationChanged = OnConfigurationChangedCallback;
00042         m_cObject.OnClosing = OnClosingCallback;
00043     }
00044 
00045     virtual ~XnLogWriterBase()
00046     {
00047         Unregister();
00048     }
00049 
00050     XnStatus Register()
00051     {
00052         XnStatus nRetVal = XN_STATUS_OK;
00053         
00054         if (!m_bRegistered)
00055         {
00056             OnRegister();
00057 
00058             nRetVal = xnLogRegisterLogWriter(&m_cObject);
00059             if (nRetVal != XN_STATUS_OK)
00060             {
00061                 OnUnregister();
00062                 return (nRetVal);
00063             }
00064 
00065             m_bRegistered = TRUE;
00066         }
00067         
00068         return (XN_STATUS_OK);
00069     }
00070 
00071     void Unregister()
00072     {
00073         if (m_bRegistered)
00074         {
00075             xnLogUnregisterLogWriter(&m_cObject);
00076             m_bRegistered = FALSE;
00077 
00078             OnUnregister();
00079         }
00080     }
00081 
00082     inline XnBool IsRegistered() { return m_bRegistered; }
00083 
00084     virtual void WriteEntry(const XnLogEntry* pEntry) = 0;
00085     virtual void WriteUnformatted(const XnChar* strMessage) = 0;
00086     virtual void OnConfigurationChanged() {};
00087     virtual void OnClosing() 
00088     {
00089         Unregister();
00090     };
00091 
00092     operator const XnLogWriter*() const
00093     {
00094         return &m_cObject;
00095     }
00096 
00097 protected:
00098     virtual void OnRegister() {}
00099     virtual void OnUnregister() {}
00100 
00101 private:
00102     static void XN_CALLBACK_TYPE WriteEntryCallback(const XnLogEntry* pEntry, void* pCookie)
00103     {
00104         XnLogWriterBase* pThis = (XnLogWriterBase*)pCookie;
00105         pThis->WriteEntry(pEntry);
00106     }
00107     static void XN_CALLBACK_TYPE WriteUnformattedCallback(const XnChar* strMessage, void* pCookie)
00108     {
00109         XnLogWriterBase* pThis = (XnLogWriterBase*)pCookie;
00110         pThis->WriteUnformatted(strMessage);
00111     }
00112     static void XN_CALLBACK_TYPE OnConfigurationChangedCallback(void* pCookie)
00113     {
00114         XnLogWriterBase* pThis = (XnLogWriterBase*)pCookie;
00115         pThis->OnConfigurationChanged();
00116     }
00117     static void XN_CALLBACK_TYPE OnClosingCallback(void* pCookie)
00118     {
00119         XnLogWriterBase* pThis = (XnLogWriterBase*)pCookie;
00120         pThis->OnClosing();
00121     }
00122 
00123     XnLogWriter m_cObject;
00124     XnBool m_bRegistered;
00125 };
00126 
00127 #endif // __XN_LOG_WRITER_BASE_H__