1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
|
2 /* |
|
3 * Copyright (c) 2006 INRIA |
|
4 * All rights reserved. |
|
5 * |
|
6 * This program is free software; you can redistribute it and/or modify |
|
7 * it under the terms of the GNU General Public License version 2 as |
|
8 * published by the Free Software Foundation; |
|
9 * |
|
10 * This program is distributed in the hope that it will be useful, |
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 * GNU General Public License for more details. |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License |
|
16 * along with this program; if not, write to the Free Software |
|
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
18 * |
|
19 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr> |
|
20 */ |
|
21 #include <list> |
|
22 #include <utility> |
|
23 #include <iostream> |
|
24 #include "debug.h" |
|
25 #include "assert.h" |
|
26 #include "ns3/core-config.h" |
|
27 #include "fatal-error.h" |
|
28 |
|
29 #ifdef HAVE_STDLIB_H |
|
30 #include <stdlib.h> |
|
31 #endif |
|
32 |
|
33 namespace ns3 { |
|
34 |
|
35 typedef std::list<std::pair <std::string, DebugComponent *> > ComponentList; |
|
36 typedef std::list<std::pair <std::string, DebugComponent *> >::iterator ComponentListI; |
|
37 |
|
38 static |
|
39 ComponentList *GetComponentList (void) |
|
40 { |
|
41 static ComponentList components; |
|
42 return &components; |
|
43 } |
|
44 |
|
45 |
|
46 static bool g_firstDebug = true; |
|
47 |
|
48 void |
|
49 DebugComponentEnableEnvVar (void) |
|
50 { |
|
51 #ifdef HAVE_GETENV |
|
52 char *envVar = getenv("NS_DEBUG"); |
|
53 if (envVar == 0) |
|
54 { |
|
55 return; |
|
56 } |
|
57 bool allFound = true; |
|
58 std::string env = envVar; |
|
59 std::string::size_type cur = 0; |
|
60 std::string::size_type next = 0; |
|
61 while (true) |
|
62 { |
|
63 next = env.find_first_of (";", cur); |
|
64 std::string tmp = std::string (env, cur, next); |
|
65 { |
|
66 /* The following code is a workaround for a bug in the g++ |
|
67 * c++ string library. Its goal is to remove any trailing ';' |
|
68 * from the string even though there should not be any in |
|
69 * it. This code should be safe even if the bug is not there. |
|
70 */ |
|
71 std::string::size_type trailing = tmp.find_first_of (";"); |
|
72 tmp = tmp.substr (0, trailing); |
|
73 } |
|
74 if (tmp.size () == 0) |
|
75 { |
|
76 break; |
|
77 } |
|
78 bool found = false; |
|
79 ComponentList *components = GetComponentList (); |
|
80 for (ComponentListI i = components->begin (); |
|
81 i != components->end (); |
|
82 i++) |
|
83 { |
|
84 if (i->first.compare (tmp) == 0) |
|
85 { |
|
86 found = true; |
|
87 i->second->Enable (); |
|
88 break; |
|
89 } |
|
90 } |
|
91 if (!found) |
|
92 { |
|
93 allFound = false; |
|
94 } |
|
95 if (next == std::string::npos) |
|
96 { |
|
97 break; |
|
98 } |
|
99 cur = next + 1; |
|
100 if (cur >= env.size ()) |
|
101 { |
|
102 break; |
|
103 } |
|
104 } |
|
105 if (allFound) |
|
106 { |
|
107 g_firstDebug = true; |
|
108 } |
|
109 |
|
110 #endif |
|
111 } |
|
112 |
|
113 |
|
114 DebugComponent::DebugComponent (char const * name) |
|
115 : m_isEnabled (false) |
|
116 { |
|
117 ComponentList *components = GetComponentList (); |
|
118 for (ComponentListI i = components->begin (); |
|
119 i != components->end (); |
|
120 i++) |
|
121 { |
|
122 NS_ASSERT (i->first != name); |
|
123 } |
|
124 components->push_back (std::make_pair (name, this)); |
|
125 } |
|
126 bool |
|
127 DebugComponent::IsEnabled (void) |
|
128 { |
|
129 if (g_firstDebug) |
|
130 { |
|
131 DebugComponentEnableEnvVar (); |
|
132 } |
|
133 return m_isEnabled; |
|
134 } |
|
135 void |
|
136 DebugComponent::Enable (void) |
|
137 { |
|
138 m_isEnabled = true; |
|
139 } |
|
140 void |
|
141 DebugComponent::Disable (void) |
|
142 { |
|
143 m_isEnabled = false; |
|
144 } |
|
145 |
|
146 void |
|
147 DebugComponentEnable (char const *name) |
|
148 { |
|
149 ComponentList *components = GetComponentList (); |
|
150 for (ComponentListI i = components->begin (); |
|
151 i != components->end (); |
|
152 i++) |
|
153 { |
|
154 if (i->first.compare (name) == 0) |
|
155 { |
|
156 i->second->Enable (); |
|
157 break; |
|
158 } |
|
159 } |
|
160 } |
|
161 void |
|
162 DebugComponentDisable (char const *name) |
|
163 { |
|
164 ComponentList *components = GetComponentList (); |
|
165 for (ComponentListI i = components->begin (); |
|
166 i != components->end (); |
|
167 i++) |
|
168 { |
|
169 if (i->first.compare (name) == 0) |
|
170 { |
|
171 i->second->Disable (); |
|
172 break; |
|
173 } |
|
174 } |
|
175 } |
|
176 |
|
177 |
|
178 void |
|
179 DebugComponentPrintList (void) |
|
180 { |
|
181 ComponentList *components = GetComponentList (); |
|
182 for (ComponentListI i = components->begin (); |
|
183 i != components->end (); |
|
184 i++) |
|
185 { |
|
186 std::cout << i->first << "=" << (i->second->IsEnabled ()?"enabled":"disabled") << std::endl; |
|
187 } |
|
188 } |
|
189 |
|
190 }; // namespace ns3 |
|
191 |
|
192 |
|