1 /* 2 * Created on 16-Jan-2005 3 * Created by Paul Gardner 4 * Copyright (C) Azureus Software, Inc, All Rights Reserved. 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License 8 * as published by the Free Software Foundation; either version 2 9 * of the License, or (at your option) any later version. 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 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, write to the Free Software 16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 17 * 18 */ 19 20 package com.aelitis.azureus.core.dht.impl; 21 22 23 /** 24 * @author parg 25 * 26 */ 27 28 import java.util.*; 29 30 import org.gudy.azureus2.core3.util.ByteArrayHashMap; 31 import org.gudy.azureus2.core3.util.ByteFormatter; 32 import org.gudy.azureus2.core3.util.HashWrapper; 33 34 import com.aelitis.azureus.core.dht.DHTLogger; 35 import com.aelitis.azureus.core.dht.transport.DHTTransportContact; 36 import com.aelitis.azureus.core.dht.transport.DHTTransportValue; 37 38 public class 39 DHTLog 40 { 41 public static final boolean GLOBAL_BLOOM_TRACE = false; 42 public static final boolean LOCAL_BLOOM_TRACE = false; 43 public static final boolean CONTACT_VERIFY_TRACE = false; 44 public static final boolean TRACE_VERSIONS = false; 45 46 static{ 47 if ( GLOBAL_BLOOM_TRACE ){ 48 System.out.println( "**** DHTLog: global bloom trace on ****" ); 49 } 50 if ( LOCAL_BLOOM_TRACE ){ 51 System.out.println( "**** DHTLog: local bloom trace on ****" ); 52 } 53 if ( CONTACT_VERIFY_TRACE ){ 54 System.out.println( "**** DHTLog: contact verify trace on ****" ); 55 } 56 if ( TRACE_VERSIONS ){ 57 System.out.println( "**** DHTTransportStats: tracing protocol versions ****" ); 58 } 59 } 60 61 62 public static boolean logging_on = false; 63 64 private static DHTLogger logger; 65 66 protected static void setLogging( boolean on )67 setLogging( 68 boolean on ) 69 { 70 logging_on = on; 71 } 72 73 public static boolean isOn()74 isOn() 75 { 76 return( logging_on ); 77 } 78 79 public static void log( String str )80 log( 81 String str ) 82 { 83 if ( logging_on ){ 84 85 if ( logger != null ){ 86 87 logger.log( str ); 88 89 }else{ 90 91 System.out.println( str ); 92 } 93 } 94 } 95 96 public static void setLogger( DHTLogger l )97 setLogger( 98 DHTLogger l ) 99 { 100 logger = l; 101 } 102 103 104 public static String getString( byte[] b )105 getString( 106 byte[] b ) 107 { 108 if ( logging_on ){ 109 110 return( getString2(b)); 111 112 113 }else{ 114 115 return( "" ); 116 } 117 } 118 119 public static String getString2( byte[] b )120 getString2( 121 byte[] b ) 122 { 123 String res = ByteFormatter.nicePrint(b); 124 125 if ( res.length() > 8 ){ 126 127 res = res.substring(0,8)+"..."; 128 } 129 130 return( res ); 131 } 132 133 public static String getFullString( byte[] b )134 getFullString( 135 byte[] b ) 136 { 137 return( ByteFormatter.nicePrint(b)); 138 } 139 140 public static String getString( HashWrapper w )141 getString( 142 HashWrapper w ) 143 { 144 if ( logging_on ){ 145 146 return( getString( w.getHash())); 147 148 }else{ 149 return( "" ); 150 } 151 } 152 153 public static String getString( DHTTransportContact[] contacts )154 getString( 155 DHTTransportContact[] contacts ) 156 { 157 if ( logging_on ){ 158 159 StringBuilder sb = new StringBuilder( 128 ); 160 sb.append( "{" ); 161 162 for (int i=0;i<contacts.length;i++){ 163 164 if ( i > 0 ){ 165 sb.append( "," ); 166 } 167 sb.append( getString(contacts[i].getID())); 168 } 169 170 sb.append( "}" ); 171 172 return( sb.toString()); 173 }else{ 174 return( "" ); 175 } 176 } 177 178 public static String getString( DHTTransportContact contact )179 getString( 180 DHTTransportContact contact ) 181 { 182 if ( logging_on ){ 183 return( contact.getString()); 184 }else{ 185 return( "" ); 186 } 187 } 188 189 public static String getString( List l )190 getString( 191 List l ) 192 { 193 if ( logging_on ){ 194 StringBuilder sb = new StringBuilder( 128 ); 195 sb.append( "{" ); 196 197 for (int i=0;i<l.size();i++){ 198 199 if ( i > 0 ){ 200 sb.append( "," ); 201 } 202 sb.append(getString((DHTTransportContact)l.get(i))); 203 } 204 205 sb.append( "}" ); 206 207 return( sb.toString()); 208 }else{ 209 return( "" ); 210 } 211 } 212 213 public static String getString( Set s )214 getString( 215 Set s ) 216 { 217 if ( logging_on ){ 218 StringBuilder sb = new StringBuilder( 128 ); 219 sb.append( "{" ); 220 221 Iterator it = s.iterator(); 222 223 while( it.hasNext()){ 224 225 if ( sb.length() > 1 ){ 226 sb.append( "," ); 227 } 228 sb.append( getString((DHTTransportContact)it.next())); 229 } 230 231 sb.append( "}" ); 232 233 return( sb.toString()); 234 }else{ 235 return( "" ); 236 } 237 } 238 239 public static String getString( Map s )240 getString( 241 Map s ) 242 { 243 if ( logging_on ){ 244 StringBuilder sb = new StringBuilder( 128 ); 245 sb.append( "{" ); 246 247 Iterator it = s.keySet().iterator(); 248 249 while( it.hasNext()){ 250 251 if ( sb.length() > 1 ){ 252 sb.append( "," ); 253 } 254 sb.append( getString((HashWrapper)it.next())); 255 } 256 257 sb.append( "}" ); 258 259 return( sb.toString()); 260 }else{ 261 return( "" ); 262 } 263 } 264 265 public static String getString( ByteArrayHashMap<?> s )266 getString( 267 ByteArrayHashMap<?> s ) 268 { 269 if ( logging_on ){ 270 StringBuilder sb = new StringBuilder( 128 ); 271 sb.append( "{" ); 272 273 List<byte[]> keys = s.keys(); 274 275 for ( byte[] key: keys ){ 276 277 if ( sb.length() > 1 ){ 278 sb.append( "," ); 279 } 280 sb.append( getString( key )); 281 } 282 283 sb.append( "}" ); 284 285 return( sb.toString()); 286 }else{ 287 return( "" ); 288 } 289 } 290 291 public static String getString( DHTTransportValue[] values )292 getString( 293 DHTTransportValue[] values ) 294 { 295 if ( logging_on ){ 296 297 if ( values == null ){ 298 299 return( "<null>"); 300 } 301 302 StringBuilder sb = new StringBuilder(256); 303 304 for (int i=0;i<values.length;i++){ 305 306 if ( i > 0 ){ 307 sb.append( "," ); 308 } 309 getString( sb, values[i] ); 310 } 311 return( sb.toString()); 312 }else{ 313 return( "" ); 314 } 315 } 316 317 public static void getString( StringBuilder sb, DHTTransportValue value )318 getString( 319 StringBuilder sb, 320 DHTTransportValue value ) 321 { 322 if ( logging_on ){ 323 324 if ( value == null ){ 325 326 sb.append( "<null>" ); 327 328 }else{ 329 330 sb.append( getString( value.getValue())); 331 sb.append( " <" ); 332 sb.append( value.isLocal()?"loc":"rem" ); 333 sb.append( ",flag=" ); 334 sb.append( Integer.toHexString(value.getFlags())); 335 sb.append( ",life=" ); 336 sb.append( value.getLifeTimeHours()); 337 sb.append( ",rep=" ); 338 sb.append( Integer.toHexString( value.getReplicationControl())); 339 sb.append( ",orig=" ); 340 sb.append( value.getOriginator().getExternalAddress()); 341 sb.append( ">"); 342 } 343 } 344 } 345 346 public static String getString( DHTTransportValue value )347 getString( 348 DHTTransportValue value ) 349 { 350 if ( logging_on ){ 351 352 if ( value == null ){ 353 354 return( "<null>"); 355 } 356 357 return( getString( value.getValue()) + " <" + (value.isLocal()?"loc":"rem" ) + ",flag=" + Integer.toHexString(value.getFlags()) + ",life=" + value.getLifeTimeHours() + ",rep=" + Integer.toHexString( value.getReplicationControl())+",orig=" + value.getOriginator().getExternalAddress() +">" ); 358 }else{ 359 return( "" ); 360 } 361 } 362 } 363