WvStreams
trace.h
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2  *
3  * XPLC - Cross-Platform Lightweight Components
4  * Copyright (C) 2002, Pierre Phaneuf
5  * Copyright (C) 2002, Net Integration Technologies, Inc.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20  * USA
21  *
22  * As a special exception, you may use this file as part of a free
23  * software library without restriction. Specifically, if other files
24  * instantiate templates or use macros or inline functions from this
25  * file, or you compile this file and link it with other files to
26  * produce an executable, this file does not by itself cause the
27  * resulting executable to be covered by the GNU Lesser General Public
28  * License. This exception does not however invalidate any other
29  * reasons why the executable file might be covered by the GNU Lesser
30  * General Public License.
31  */
32 
33 #ifndef __XPLC_TRACE_H__
34 #define __XPLC_TRACE_H__
35 
36 #if defined(__GNUC__) && __GNUC__ > 3
37 # pragma GCC system_header
38 #endif
39 
40 #ifdef DEBUG
41 
42 #include <stdio.h>
43 
44 /*
45  * Mix-in template that trace constructors, destructors and refcount
46  * to stderr.
47  */
48 template<class Component>
49 class TraceComponent: public Component {
50 public:
51  TraceComponent() {
52  fprintf(stderr, "%s: instantiated (%p)\n", __PRETTY_FUNCTION__, this);
53  }
54  virtual unsigned int addRef() {
55  unsigned int refcount = Component::addRef();
56 
57  fprintf(stderr, "%s = %i (%p)\n", __PRETTY_FUNCTION__, refcount, this);
58 
59  return refcount;
60  }
61  virtual unsigned int release() {
62  unsigned int refcount = Component::release();
63 
64  fprintf(stderr, "%s = %i (%p)\n", __PRETTY_FUNCTION__, refcount, this);
65 
66  return refcount;
67  }
68  virtual ~TraceComponent() {
69  fprintf(stderr, "%s: destroyed (%p)\n", __PRETTY_FUNCTION__, this);
70  }
71 };
72 
73 #else /* DEBUG */
74 
75 #error "this header should not be used other than for debugging"
76 
77 #endif /* else DEBUG */
78 
79 #endif /* __XPLC_TRACE_H__ */