Click here to Skip to main content
13,351,896 members (78,312 online)
Click here to Skip to main content


36 bookmarked
Posted 10 Feb 2004

XBitArray - a non-MFC C++ class to manipulate bits in a bit array.

, 10 Feb 2004
XBitArray provides functions to set, test, and find bits in an array of bytes.
// XTrace.h  Version 1.2
// Author:       Paul Mclachlan
// Modified by:  Hans Dietrich
// Version 1.2:  added TRACEPOINT macro
// Version 1.1:  added Unicode support
//               added optional thread id to output string
//               added option to enable/disable full path
//               added TRACERECT macro
//               changed name to avoid conflicts with Paul's class.
// This code was taken from article by Paul Mclachlan, "Getting around
// the need for a vararg #define just to automatically use __FILE__ and
// __LINE__ in a TRACE macro".  For original article, see
// XTrace.h is a drop-in replacement for MFC's TRACE facility.  It has no
// dependency on MFC.  It is thread-safe and uses no globals or statics.
// It optionally adds source module/line number and thread id to each line
// of TRACE output.  To control these features, use the following defines:
// XTrace.h also provides the TRACERECT and TRACEPOINT macros, which output 
// the contents of a RECT struct and a POINT struct.  
// In Release builds, no output will be produced.

#ifndef XTRACE_H
#define XTRACE_H

#include <stdarg.h>
#include <stdio.h>
#include <windows.h>
#include <tchar.h>

#pragma warning(push)
#pragma warning(disable : 4127)			// conditional expression is constant

#define XTRACE_SHOW_FULLPATH	FALSE	// FALSE = only show base name of file
#define XTRACE_SHOW_THREAD_ID	TRUE	// TRUE = include thread id in output

class xtracing_output_debug_string
	xtracing_output_debug_string(LPCTSTR lpszFile, int line) :

	void operator() (LPCTSTR lpszFormat, ...)
		va_list va;
		va_start(va, lpszFormat);


		// add the __FILE__ and __LINE__ to the front
		LPTSTR cp = (LPTSTR) m_file;

			cp = _tcsrchr(m_file, _T('\\'));
			if (cp)

			if (_sntprintf(buf1, BUFFER_SIZE-1, _T("%s(%d) : [%X] %s"),
						cp, m_line, GetCurrentThreadId(), lpszFormat) < 0)
				buf1[BUFFER_SIZE-1] = _T('\0');
			if (_sntprintf(buf1, BUFFER_SIZE-1, _T("%s(%d) : %s"),
						cp, m_line, lpszFormat) < 0)
				buf1[BUFFER_SIZE-1] = _T('\0');

		// format the message as requested
		if (_vsntprintf(buf2, BUFFER_SIZE-1, buf1, va) < 0)
			buf2[BUFFER_SIZE-1] = _T('\0');


		// write it out

	LPCTSTR m_file;
	int     m_line;
	enum    { BUFFER_SIZE = 4096 };

#undef TRACE

#define _DEBUGnow

#ifdef _DEBUG
#define TRACE (xtracing_output_debug_string(_T(__FILE__), __LINE__ ))
#define TRACEERROR (xtracing_output_debug_string(_T(__FILE__), __LINE__ ))
#define TRACE ((void)0)
#define TRACEERROR ((void)0)

#define TRACERECT(r) TRACE(_T(#r) _T(":  left = %d  top = %d  right = %d  bottom = %d\n"), \
                           (r).left, (r).top, (r).right, (r).bottom)

#define TRACEPOINT(p) TRACE(_T(#p) _T(":  x = %d  y = %d\n"), (p).x, (p).y)

#pragma warning(pop)

#endif //XTRACE_H

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Hans Dietrich
Software Developer (Senior) Hans Dietrich Software
United States United States
I attended St. Michael's College of the University of Toronto, with the intention of becoming a priest. A friend in the University's Computer Science Department got me interested in programming, and I have been hooked ever since.

Recently, I have moved to Los Angeles where I am doing consulting and development work.

For consulting and custom software development, please see

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 10 Feb 2004
Article Copyright 2004 by Hans Dietrich
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid