OpenNI 1.5.4
XnLogWriterBase.h
Go to the documentation of this file.
1 /****************************************************************************
2 * *
3 * OpenNI 1.x Alpha *
4 * Copyright (C) 2011 PrimeSense Ltd. *
5 * *
6 * This file is part of OpenNI. *
7 * *
8 * OpenNI is free software: you can redistribute it and/or modify *
9 * it under the terms of the GNU Lesser General Public License as published *
10 * by the Free Software Foundation, either version 3 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * OpenNI is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public License *
19 * along with OpenNI. If not, see <http://www.gnu.org/licenses/>. *
20 * *
21 ****************************************************************************/
22 #ifndef __XN_LOG_WRITER_BASE_H__
23 #define __XN_LOG_WRITER_BASE_H__
24 
25 //---------------------------------------------------------------------------
26 // Includes
27 //---------------------------------------------------------------------------
28 #include <XnLogTypes.h>
29 #include <XnLog.h>
30 
31 //---------------------------------------------------------------------------
32 // Types
33 //---------------------------------------------------------------------------
34 class XnLogWriterBase
35 {
36 public:
37  XnLogWriterBase() : m_bRegistered(FALSE)
38  {
39  m_cObject.pCookie = this;
40  m_cObject.WriteEntry = WriteEntryCallback;
41  m_cObject.WriteUnformatted = WriteUnformattedCallback;
42  m_cObject.OnConfigurationChanged = OnConfigurationChangedCallback;
43  m_cObject.OnClosing = OnClosingCallback;
44  }
45 
46  virtual ~XnLogWriterBase()
47  {
48  Unregister();
49  }
50 
52  {
53  XnStatus nRetVal = XN_STATUS_OK;
54 
55  if (!m_bRegistered)
56  {
57  OnRegister();
58 
59  nRetVal = xnLogRegisterLogWriter(&m_cObject);
60  if (nRetVal != XN_STATUS_OK)
61  {
62  OnUnregister();
63  return (nRetVal);
64  }
65 
66  m_bRegistered = TRUE;
67  }
68 
69  return (XN_STATUS_OK);
70  }
71 
72  void Unregister()
73  {
74  if (m_bRegistered)
75  {
76  xnLogUnregisterLogWriter(&m_cObject);
77  m_bRegistered = FALSE;
78 
79  OnUnregister();
80  }
81  }
82 
83  inline XnBool IsRegistered() { return m_bRegistered; }
84 
85  virtual void WriteEntry(const XnLogEntry* pEntry) = 0;
86  virtual void WriteUnformatted(const XnChar* strMessage) = 0;
87  virtual void OnConfigurationChanged() {};
88  virtual void OnClosing()
89  {
91  };
92 
93  operator const XnLogWriter*() const
94  {
95  return &m_cObject;
96  }
97 
98 protected:
99  virtual void OnRegister() {}
100  virtual void OnUnregister() {}
101 
102 private:
103  static void XN_CALLBACK_TYPE WriteEntryCallback(const XnLogEntry* pEntry, void* pCookie)
104  {
105  XnLogWriterBase* pThis = (XnLogWriterBase*)pCookie;
106  pThis->WriteEntry(pEntry);
107  }
108  static void XN_CALLBACK_TYPE WriteUnformattedCallback(const XnChar* strMessage, void* pCookie)
109  {
110  XnLogWriterBase* pThis = (XnLogWriterBase*)pCookie;
111  pThis->WriteUnformatted(strMessage);
112  }
113  static void XN_CALLBACK_TYPE OnConfigurationChangedCallback(void* pCookie)
114  {
115  XnLogWriterBase* pThis = (XnLogWriterBase*)pCookie;
116  pThis->OnConfigurationChanged();
117  }
118  static void XN_CALLBACK_TYPE OnClosingCallback(void* pCookie)
119  {
120  XnLogWriterBase* pThis = (XnLogWriterBase*)pCookie;
121  pThis->OnClosing();
122  }
123 
124  XnLogWriter m_cObject;
125  XnBool m_bRegistered;
126 };
127 
128 #endif // __XN_LOG_WRITER_BASE_H__
XnLogWriterBase::IsRegistered
XnBool IsRegistered()
Definition: XnLogWriterBase.h:101
XnLogWriterBase::OnUnregister
virtual void OnUnregister()
Definition: XnLogWriterBase.h:118
XnLogWriterBase::XnLogWriterBase
XnLogWriterBase()
Definition: XnLogWriterBase.h:55
XN_STATUS_OK
#define XN_STATUS_OK
Definition: XnStatus.h:36
FALSE
#define FALSE
Definition: XnPlatform.h:93
XnLogTypes.h
TRUE
#define TRUE
Definition: XnPlatform.h:89
XnLogWriter::WriteEntry
void(* WriteEntry)(const XnLogEntry *pEntry, void *pCookie)
Definition: XnLogTypes.h:73
XnStatus
XnUInt32 XnStatus
Definition: XnStatus.h:33
XnLogEntry
Definition: XnLogTypes.h:59
XnLog.h
XnLogWriter::OnClosing
void(* OnClosing)(void *pCookie)
Definition: XnLogTypes.h:76
XnLogWriterBase::Unregister
void Unregister()
Definition: XnLogWriterBase.h:90
XnLogWriterBase::WriteEntry
virtual void WriteEntry(const XnLogEntry *pEntry)=0
XnLogWriterBase::OnRegister
virtual void OnRegister()
Definition: XnLogWriterBase.h:117
XnLogWriterBase::~XnLogWriterBase
virtual ~XnLogWriterBase()
Definition: XnLogWriterBase.h:64
XnLogWriterBase::OnConfigurationChanged
virtual void OnConfigurationChanged()
Definition: XnLogWriterBase.h:105
XnLogWriter::pCookie
void * pCookie
Definition: XnLogTypes.h:72
XnLogWriter
Definition: XnLogTypes.h:70
xnLogRegisterLogWriter
XN_C_API XnStatus XN_C_DECL xnLogRegisterLogWriter(XnLogWriter *pWriter)
XnLogWriter::OnConfigurationChanged
void(* OnConfigurationChanged)(void *pCookie)
Definition: XnLogTypes.h:75
XnLogWriterBase
Definition: XnLogWriterBase.h:33
XnLogWriterBase::Register
XnStatus Register()
Definition: XnLogWriterBase.h:69
xnLogUnregisterLogWriter
XN_C_API void XN_C_DECL xnLogUnregisterLogWriter(XnLogWriter *pWriter)
XnLogWriterBase::WriteUnformatted
virtual void WriteUnformatted(const XnChar *strMessage)=0
XnLogWriter::WriteUnformatted
void(* WriteUnformatted)(const XnChar *strMessage, void *pCookie)
Definition: XnLogTypes.h:74
XnLogWriterBase::OnClosing
virtual void OnClosing()
Definition: XnLogWriterBase.h:106