sbDebugUtils.h
Go to the documentation of this file.
1 /*
2  *=BEGIN SONGBIRD GPL
3  *
4  * This file is part of the Songbird web player.
5  *
6  * Copyright(c) 2005-2010 POTI, Inc.
7  * http://www.songbirdnest.com
8  *
9  * This file may be licensed under the terms of of the
10  * GNU General Public License Version 2 (the ``GPL'').
11  *
12  * Software distributed under the License is distributed
13  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
14  * express or implied. See the GPL for the specific language
15  * governing rights and limitations.
16  *
17  * You should have received a copy of the GPL along with this
18  * program. If not, go to http://www.gnu.org/licenses/gpl.html
19  * or write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  *=END SONGBIRD GPL
23  */
24 
25 //
26 // Currently, this is only used to define the SB_UNUSED_IN_RELEASE macro,
27 // which is used for variables that don't get referenced in release builds.
28 //
29 // The common case seen throughout our codebase is:
30 // nsresult rv = sbFoo(&foo, &bar);
31 // NS_ASSERTION(NS_ENSURE_SUCCESS(rv, rv))
32 //
33 // This will generate an error in release builds from the warning for an
34 // unused rv if DISABLE_DEADLY_WARNINGS isn't on (because NS_ASSERTION()s
35 // get removed.
36 //
37 // Code like this gets changed to:
38 // nsresult SB_UNUSED_IN_RELEASE(rv) =
39 // sbFoo(&foo, &bar);
40 // NS_ASSERTION(NS_SUCCEEDED(rv), "Oh no!")
41 //
42 
43 #ifndef SBDEBUGUTILS_H_
44 #define SBDEBUGUTILS_H_
45 
46 #include <prlog.h>
47 
48 #if defined(__GNUC__)
49  #if defined(DEBUG)
50  #define SB_UNUSED_IN_RELEASE(decl) decl
51  #else
52  #define SB_UNUSED_IN_RELEASE(decl) decl __attribute__((unused))
53  #endif
54 #else
55  #define SB_UNUSED_IN_RELEASE(decl) decl
56 #endif
57 
58 #if defined(_MSC_VER)
59  #if !defined(DEBUG)
60  // Disable warnings about unused local variables
61  #pragma warning(disable: 4101)
62  #endif
63 #endif
64 
65 #if PR_LOGGING
66  namespace {
67  static PRLogModuleInfo* gPRLogModule = nsnull;
68 
69  #define SB_PRLOG_SETUP(aModuleName) \
70  PR_BEGIN_MACRO \
71  if (!gPRLogModule) \
72  gPRLogModule = PR_NewLogModule(#aModuleName); \
73  PR_END_MACRO
74 
75  #define TRACE(fmt, ...) \
76  PR_BEGIN_MACRO \
77  if (PR_LOG_TEST(gPRLogModule, PR_LOG_DEBUG)) { \
78  PR_LogPrint("%s: " fmt, __FUNCTION__, ##__VA_ARGS__); \
79  } \
80  PR_END_MACRO
81 
82  #define LOG(fmt, ...) \
83  PR_BEGIN_MACRO \
84  if (PR_LOG_TEST(gPRLogModule, PR_LOG_WARN)) { \
85  PR_LogPrint("%s: " fmt, __FUNCTION__, ##__VA_ARGS__); \
86  } \
87  PR_END_MACRO
88 
89  struct _sbTraceFunctionClass {
90  _sbTraceFunctionClass(const char* function) {
91  mFunction = function;
92  }
93  ~_sbTraceFunctionClass() {
94  if (PR_LOG_TEST(gPRLogModule, PR_LOG_DEBUG)) {
95  PR_LogPrint("%s() <--", mFunction);
96  }
97  }
98  const char* mFunction;
99  };
100 
101  #if defined(__GNUC__)
102  #define __FUNCTION__ __PRETTY_FUNCTION__
103  #endif
104 
105  #define TRACE_FUNCTION_VAR_NAME2(x) SB_TRACE_FUNCTION_VAR_ ## x
106  #define TRACE_FUNCTION_VAR_NAME(x) TRACE_FUNCTION_VAR_NAME2(x)
107  #define TRACE_FUNCTION(fmt, ...) \
108  if (PR_LOG_TEST(gPRLogModule, PR_LOG_DEBUG)) { \
109  PR_LogPrint("%s(" fmt ") -->", __FUNCTION__, ##__VA_ARGS__); \
110  } \
111  _sbTraceFunctionClass TRACE_FUNCTION_VAR_NAME(__LINE__)(__FUNCTION__)
112  };
113 
114 #else
115  #define SB_PRLOG_SETUP(x) PR_BEGIN_MACRO PR_END_MACRO
116  #define TRACE(...) PR_BEGIN_MACRO PR_END_MACRO
117  #define LOG(...) PR_BEGIN_MACRO PR_END_MACRO
118  #define TRACE_FUNCTION(...) PR_BEGIN_MACRO PR_END_MACRO
119 #endif
120 
121 #endif /* SBDEBUGUTILS_H_ */