File: XnCallStackLogger.h

package info (click to toggle)
openni2 2.2.0.33%2Bdfsg-15
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 22,232 kB
  • sloc: cpp: 111,183; ansic: 35,511; sh: 10,542; python: 1,313; java: 952; makefile: 575; xml: 12
file content (183 lines) | stat: -rw-r--r-- 5,461 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*****************************************************************************
*                                                                            *
*  PrimeSense PSCommon Library                                               *
*  Copyright (C) 2012 PrimeSense Ltd.                                        *
*                                                                            *
*  This file is part of PSCommon.                                            *
*                                                                            *
*  Licensed under the Apache License, Version 2.0 (the "License");           *
*  you may not use this file except in compliance with the License.          *
*  You may obtain a copy of the License at                                   *
*                                                                            *
*      http://www.apache.org/licenses/LICENSE-2.0                            *
*                                                                            *
*  Unless required by applicable law or agreed to in writing, software       *
*  distributed under the License is distributed on an "AS IS" BASIS,         *
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  *
*  See the License for the specific language governing permissions and       *
*  limitations under the License.                                            *
*                                                                            *
*****************************************************************************/
#ifndef _XN_CALL_STACK_LOGGER_H_
#define _XN_CALL_STACK_LOGGER_H_
//---------------------------------------------------------------------------
// Includes
//---------------------------------------------------------------------------
#include <XnOS.h>
#include <stdlib.h>
#include <string.h>

//---------------------------------------------------------------------------
// Types
//---------------------------------------------------------------------------
namespace xnl
{

#define CALL_STACK_MAX_LENGTH		100
#define CALL_STACK_MESSAGE_LENGTH	100

// Class for logging call stacks.
// Must be declared with following parameters:
//   NUM_ELEMENTS - maximum number of elements that can be logged (must be added first).
//   HISTORY_LENGTH - number of call stack histories to be stored (cyclic buffer).
//   CALL_STACK_DEPTH - maximum depth of call stack (max number of functions in stack).
template <int NUM_ELEMENTS, int HISTORY_LENGTH, int CALL_STACK_DEPTH>
class CallStackLogger
{
public:
	// Single function in stack.
	typedef XnChar XnStackFunction[CALL_STACK_MAX_LENGTH];

	typedef struct
	{
		// Thread identifier.
		XN_THREAD_ID tid;

		// User message.
		XnChar msg[CALL_STACK_MESSAGE_LENGTH];

		// Timestamp.
		XnUInt64 timestamp;

		// Call stack.
		XnStackFunction stack[CALL_STACK_DEPTH];

	} CallStack;

	typedef struct  
	{
		// Pointer to element for which the call stack was created.
		void* pElementPtr;

		// Call stack history (cyclic).
		CallStack history[HISTORY_LENGTH];

		// Index in the history to next empty slot.
		int historyIndex;

	} Element;

public:

	CallStackLogger()
	{
		memset(&m_elements, 0, sizeof(m_elements));
	}

	// Add an element to the list of elements.
	bool Add(void* pElement)
	{
		for (int i = 0; i < NUM_ELEMENTS; ++i)
		{
			if (m_elements[i].pElementPtr == NULL)
			{
				m_elements[i].pElementPtr = pElement;
				return true;
			}
			else if (m_elements[i].pElementPtr == pElement)
			{
				return true;
			}
		}

		return false;
	}

	void Remove(void* pElement)
	{
		for (int i = 0; i < NUM_ELEMENTS; ++i)
		{
			if (m_elements[i].pElementPtr == pElement)
			{
				m_elements[i].pElementPtr = NULL;
				return;
			}
		}
	}

	// Log the call stack. May fail if no more space for element.
	void Log(void* pElement, const char* format, ...)
	{
		// Verify valid element.
		if (pElement == NULL)
		{
			return;
		}

		// Find the element in the list.
		for (int i = 0; i < NUM_ELEMENTS; ++i)
		{
			if (m_elements[i].pElementPtr == pElement)
			{
				CallStack* pCallStack = &m_elements[i].history[m_elements[i].historyIndex];

				// Prepare the pointers to stack funcs.
				XnChar* pstrStackFuncs[CALL_STACK_DEPTH];
				for (int j = 0; j < CALL_STACK_DEPTH; ++j)
				{
					pstrStackFuncs[j] = pCallStack->stack[j];
				}

				// Get the call stack.
				int nFuncs = CALL_STACK_DEPTH;
				xnOSGetCurrentCallStack(1/*self*/, pstrStackFuncs, sizeof(XnStackFunction), &nFuncs);

				// Clear unused stack entries.
				for (int k = nFuncs; k < CALL_STACK_DEPTH; ++k)
				{
					pCallStack->stack[k][0] = '\0';
				}

				// Get the thread ID.
				xnOSGetCurrentThreadID(&pCallStack->tid);

				// Build message string.
				va_list args;
				va_start(args, format);
				vsnprintf(pCallStack->msg, sizeof(pCallStack->msg)-1, format, args);
				va_end(args);

				// Get the timestamp.
				xnOSGetHighResTimeStamp(&pCallStack->timestamp);

				// Increment history
				m_elements[i].historyIndex = (m_elements[i].historyIndex+1) % HISTORY_LENGTH;

				// Mark the next element in history as blank (to make finding wrap-around easier).
				CallStack* pNextCallStack = &m_elements[i].history[m_elements[i].historyIndex];
				pNextCallStack->tid = 0;
				pNextCallStack->msg[0] = '\0';
				pNextCallStack->timestamp = 0;

				break;
			}
		}
	}

public:
	Element m_elements[NUM_ELEMENTS];
};

} // xnl

#endif // _XN_CALL_STACK_LOGGER_H_