1 package org.grlea.log; 2 3 6 18 import java.text.MessageFormat ; 19 import java.util.Date ; 20 21 106 public class 107 SimpleLogger 108 { 109 112 private static final char[] BYTE_CHARS = 113 {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; 114 115 118 private final SimpleLog log; 119 120 123 private final Class sourceClass; 124 125 128 private final String className; 129 130 133 private final String classNameShort; 134 135 139 private Object instanceId; 140 141 144 private final boolean isInstanceDebugger; 145 146 150 private final boolean useLongName; 151 152 164 private String configName; 165 166 169 private DebugLevel debugLevel; 170 171 174 private boolean tracing; 175 176 187 public 188 SimpleLogger(Class sourceClass) 189 { 190 this(SimpleLog.defaultInstance(), sourceClass); 191 } 192 193 210 public 211 SimpleLogger(Class sourceClass, Object instanceId) 212 { 213 this(SimpleLog.defaultInstance(), sourceClass, instanceId); 214 } 215 216 233 public 234 SimpleLogger(Class sourceClass, boolean useLongName) 235 { 236 this(SimpleLog.defaultInstance(), sourceClass, useLongName); 237 } 238 239 261 public 262 SimpleLogger(Class sourceClass, Object instanceId, boolean useLongName) 263 { 264 this(SimpleLog.defaultInstance(), sourceClass, instanceId, useLongName); 265 } 266 267 282 public 283 SimpleLogger(SimpleLog log, Class sourceClass) 284 { 285 this(log, sourceClass, false); 286 } 287 288 308 public 309 SimpleLogger(SimpleLog log, Class sourceClass, Object instanceId) 310 { 311 this(log, sourceClass, instanceId, false); 312 } 313 314 334 public 335 SimpleLogger(SimpleLog log, Class sourceClass, boolean useLongName) 336 { 337 this(log, sourceClass, null, useLongName); 338 } 339 340 360 public 361 SimpleLogger(SimpleLog log, Class sourceClass, Object instanceId, boolean useLongName) 362 { 363 this.log = log; 364 this.sourceClass = sourceClass; 365 this.instanceId = instanceId; 366 this.isInstanceDebugger = instanceId != null; 367 this.useLongName = useLongName; 368 this.className = sourceClass.getName(); 369 this.classNameShort = createClassNameShort(sourceClass); 370 371 generateConfigName(); 372 373 log.register(this); 374 } 375 376 379 private void 380 generateConfigName() 381 { 382 String loggerName = sourceClass.getName(); 383 384 if (isInstanceDebugger) 385 { 386 loggerName = loggerName + '.' + instanceId; 387 } 388 389 if (loggerName.indexOf('$') != -1) 390 { 391 loggerName = loggerName.replace('$', '.'); 392 } 393 394 this.configName = loggerName; 395 } 396 397 404 private String 405 createClassNameShort(Class sourceClass) 406 { 407 if (className.indexOf('.') == -1) 408 { 409 return className; 410 } 411 412 if (sourceClass.getPackage() != null && sourceClass.getPackage().getName() != null) 413 { 414 return className.substring(sourceClass.getPackage().getName().length() + 1); 415 } 416 417 String packageName = className; 419 boolean packageNameIsAClass = true; 420 int lastPeriod = packageName.indexOf('.'); 421 while (packageNameIsAClass && lastPeriod != -1) 422 { 423 packageName = packageName.substring(0, lastPeriod); 424 try 425 { 426 Class.forName(packageName); 427 packageNameIsAClass = true; 428 } 429 catch (Throwable e) 430 { 431 packageNameIsAClass = false; 432 } 433 lastPeriod = packageName.lastIndexOf('.'); 434 } 435 436 if (packageName.length() == 0) 437 { 438 return className; 439 } 440 else 441 { 442 return className.substring(packageName.length() + 1); 443 } 444 } 445 446 450 public boolean 451 isInstanceDebugger() 452 { 453 return isInstanceDebugger; 454 } 455 456 460 public Object 461 getInstanceID() 462 { 463 return instanceId; 464 } 465 466 474 public void 475 setInstanceID(Object instanceId) 476 { 477 if (!isInstanceDebugger) 478 { 479 throw new IllegalStateException ("This is not an instance debugger."); 480 } 481 if (instanceId == null) 482 { 483 throw new IllegalArgumentException ("instanceId cannot be null."); 484 } 485 this.instanceId = instanceId; 486 487 generateConfigName(); 488 } 489 490 503 public String 504 getConfigName() 505 { 506 return configName; 507 } 508 509 512 public DebugLevel 513 getDebugLevel() 514 { 515 return debugLevel; 516 } 517 518 530 public boolean 531 wouldLog(DebugLevel level) 532 { 533 return log.isOutputting() && getDebugLevel().shouldLog(level); 534 } 535 536 543 public void 544 setDebugLevel(DebugLevel debugLevel) 545 { 546 if (debugLevel == null) 547 { 548 throw new IllegalArgumentException ("debugLevel cannot be null."); 549 } 550 this.debugLevel = debugLevel; 551 } 552 553 558 public boolean 559 isTracing() 560 { 561 return tracing; 562 } 563 564 569 public void 570 setTracing(boolean tracing) 571 { 572 this.tracing = tracing; 573 } 574 575 578 public Class 579 getSourceClass() 580 { 581 return sourceClass; 582 } 583 584 594 private Object [] 595 createData(DebugLevel level, int extraArguments) 596 { 597 Object [] result = new Object [5 + extraArguments]; 598 result[0] = new Date (); 599 result[1] = Thread.currentThread().getName(); 600 result[2] = useLongName ? className : classNameShort; 601 result[3] = instanceId; 602 result[4] = level; 603 return result; 604 } 605 606 613 public void 614 fatal(String message) 615 { 616 db(DebugLevel.L1_FATAL, message); 617 } 618 619 626 public void 627 error(String message) 628 { 629 db(DebugLevel.L2_ERROR, message); 630 } 631 632 639 public void 640 warn(String message) 641 { 642 db(DebugLevel.L3_WARN, message); 643 } 644 645 652 public void 653 info(String message) 654 { 655 db(DebugLevel.L4_INFO, message); 656 } 657 658 665 public void 666 debug(String message) 667 { 668 db(DebugLevel.L5_DEBUG, message); 669 } 670 671 678 public void 679 verbose(String message) 680 { 681 db(DebugLevel.L6_VERBOSE, message); 682 } 683 684 691 public void 692 ludicrous(String message) 693 { 694 db(DebugLevel.L7_LUDICROUS, message); 695 } 696 697 712 public void 713 db(DebugLevel level, String message) 714 { 715 if (!log.isOutputting() || !debugLevel.shouldLog(level)) 716 { 717 return; 718 } 719 720 Object [] data = createData(level, 1); 721 data[data.length - 1] = message; 722 MessageFormat format = 723 isInstanceDebugger ? log.getDebugInstanceFormat() : log.getDebugFormat(); 724 log.println(format.format(data)); 725 } 726 727 746 public void 747 dbo(DebugLevel level, String objectName, Object value) 748 { 749 if (wouldLog(level)) 750 { 751 if (value instanceof Object []) 752 { 753 dbo(level, objectName, (Object []) value); 754 } 755 else if (value instanceof byte[]) 756 { 757 dbo(level, objectName, (byte[]) value); 758 } 759 else if (value instanceof char[]) 760 { 761 dbo(level, objectName, (char[]) value); 762 } 763 else 764 { 765 dboNoCheck(level, objectName, value); 766 } 767 } 768 } 769 770 775 private void 776 dboNoCheck(DebugLevel level, String objectName, Object val) 777 { 778 Object [] data = createData(level, 2); 779 data[data.length - 2] = objectName; 780 data[data.length - 1] = val; 781 MessageFormat format = isInstanceDebugger ? log.getDebugObjectInstanceFormat() : 782 log.getDebugObjectFormat(); 783 log.println(format.format(data)); 784 } 785 786 796 public void 797 dbo(DebugLevel level, String objectName, Object [] val) 798 { 799 if (!log.isOutputting() || !debugLevel.shouldLog(level)) 800 { 801 return; 802 } 803 804 StringBuffer stringValue = new StringBuffer ("["); 805 for (int i = 0; i < val.length; i++) 806 { 807 if (i == 0) 808 { 809 stringValue.append(val[i]); 810 } 811 else 812 { 813 stringValue.append(", ").append(val[i]); 814 } 815 } 816 stringValue.append("]"); 817 818 dboNoCheck(level, objectName, stringValue); 819 } 820 821 826 public void 827 dbo(DebugLevel level, String objectName, short val) 828 { 829 if (!log.isOutputting() || !debugLevel.shouldLog(level)) 830 { 831 return; 832 } 833 834 dboNoCheck(level, objectName, String.valueOf(val)); 835 } 836 837 842 public void 843 dbo(DebugLevel level, String objectName, int val) 844 { 845 if (!log.isOutputting() || !debugLevel.shouldLog(level)) 846 { 847 return; 848 } 849 850 dboNoCheck(level, objectName, String.valueOf(val)); 851 } 852 853 858 public void 859 dbo(DebugLevel level, String objectName, long val) 860 { 861 if (!log.isOutputting() || !debugLevel.shouldLog(level)) 862 { 863 return; 864 } 865 866 dboNoCheck(level, objectName, String.valueOf(val)); 867 } 868 869 874 public void 875 dbo(DebugLevel level, String objectName, boolean val) 876 { 877 if (!log.isOutputting() || !debugLevel.shouldLog(level)) 878 { 879 return; 880 } 881 882 dboNoCheck(level, objectName, String.valueOf(val)); 883 } 884 885 890 public void 891 dbo(DebugLevel level, String objectName, float val) 892 { 893 if (!log.isOutputting() || !debugLevel.shouldLog(level)) 894 { 895 return; 896 } 897 898 dboNoCheck(level, objectName, String.valueOf(val)); 899 } 900 901 906 public void 907 dbo(DebugLevel level, String objectName, double val) 908 { 909 if (!log.isOutputting() || !debugLevel.shouldLog(level)) 910 { 911 return; 912 } 913 914 dboNoCheck(level, objectName, String.valueOf(val)); 915 } 916 917 922 public void 923 dbo(DebugLevel level, String objectName, byte val) 924 { 925 if (!log.isOutputting() || !debugLevel.shouldLog(level)) 926 { 927 return; 928 } 929 930 dboNoCheck(level, objectName, toString(val)); 931 } 932 933 938 public void 939 dbo(DebugLevel level, String objectName, byte[] val) 940 { 941 if (!log.isOutputting() || !debugLevel.shouldLog(level)) 942 { 943 return; 944 } 945 946 dboNoCheck(level, objectName, toString(val)); 947 } 948 949 954 public void 955 dbo(DebugLevel level, String objectName, char val) 956 { 957 if (!log.isOutputting() || !debugLevel.shouldLog(level)) 958 { 959 return; 960 } 961 962 dboNoCheck(level, objectName, String.valueOf(val)); 963 } 964 965 970 public void 971 dbo(DebugLevel level, String objectName, char[] val) 972 { 973 if (!log.isOutputting() || !debugLevel.shouldLog(level)) 974 { 975 return; 976 } 977 978 dboNoCheck(level, objectName, String.valueOf(val)); 979 } 980 981 987 public void 988 infoObject(String objectName, Object val) 989 { 990 dbo(DebugLevel.L4_INFO, objectName, val); 991 } 992 993 999 public void 1000 infoObject(String objectName, boolean val) 1001 { 1002 dbo(DebugLevel.L4_INFO, objectName, val); 1003 } 1004 1005 1011 public void 1012 infoObject(String objectName, int val) 1013 { 1014 dbo(DebugLevel.L4_INFO, objectName, val); 1015 } 1016 1017 1023 public void 1024 debugObject(String objectName, Object val) 1025 { 1026 dbo(DebugLevel.L5_DEBUG, objectName, val); 1027 } 1028 1029 1035 public void 1036 debugObject(String objectName, boolean val) 1037 { 1038 dbo(DebugLevel.L5_DEBUG, objectName, val); 1039 } 1040 1041 1047 public void 1048 debugObject(String objectName, int val) 1049 { 1050 dbo(DebugLevel.L5_DEBUG, objectName, val); 1051 } 1052 1053 1059 public void 1060 verboseObject(String objectName, Object val) 1061 { 1062 dbo(DebugLevel.L6_VERBOSE, objectName, val); 1063 } 1064 1065 1071 public void 1072 verboseObject(String objectName, boolean val) 1073 { 1074 dbo(DebugLevel.L6_VERBOSE, objectName, val); 1075 } 1076 1077 1083 public void 1084 verboseObject(String objectName, int val) 1085 { 1086 dbo(DebugLevel.L6_VERBOSE, objectName, val); 1087 } 1088 1089 1095 public void 1096 ludicrousObject(String objectName, Object val) 1097 { 1098 dbo(DebugLevel.L7_LUDICROUS, objectName, val); 1099 } 1100 1101 1107 public void 1108 ludicrousObject(String objectName, boolean val) 1109 { 1110 dbo(DebugLevel.L7_LUDICROUS, objectName, val); 1111 } 1112 1113 1119 public void 1120 ludicrousObject(String objectName, int val) 1121 { 1122 dbo(DebugLevel.L7_LUDICROUS, objectName, val); 1123 } 1124 1125 1135 public void 1136 dbe(DebugLevel level, Throwable t) 1137 { 1138 if (!log.isOutputting() || !debugLevel.shouldLog(level)) 1139 { 1140 return; 1141 } 1142 1143 Object [] data = createData(level, 1); 1144 data[data.length - 1] = t; 1145 MessageFormat format = isInstanceDebugger ? log.getDebugExceptionInstanceFormat() : 1146 log.getDebugExceptionFormat(); 1147 log.println(format.format(data)); 1148 } 1149 1150 1156 public void 1157 fatalException(Throwable t) 1158 { 1159 dbe(DebugLevel.L1_FATAL, t); 1160 } 1161 1162 1168 public void 1169 errorException(Throwable t) 1170 { 1171 dbe(DebugLevel.L2_ERROR, t); 1172 } 1173 1174 1180 public void 1181 warnException(Throwable t) 1182 { 1183 dbe(DebugLevel.L3_WARN, t); 1184 } 1185 1186 1194 public void 1195 entry(String methodName) 1196 { 1197 if (!log.isOutputting() || !tracing) 1198 { 1199 return; 1200 } 1201 1202 Object [] data = createData(DebugLevel.FAKE_TRACE, 1); 1203 data[data.length - 1] = methodName; 1204 MessageFormat format = isInstanceDebugger ? log.getEntryInstanceFormat() : 1205 log.getEntryFormat(); 1206 log.println(format.format(data)); 1207 } 1208 1209 1215 public void 1216 exit(String methodName) 1217 { 1218 if (!log.isOutputting() || !tracing) 1219 { 1220 return; 1221 } 1222 1223 Object [] data = createData(DebugLevel.FAKE_TRACE, 1); 1224 data[data.length - 1] = methodName; 1225 MessageFormat format = isInstanceDebugger ? log.getExitInstanceFormat() : 1226 log.getExitFormat(); 1227 log.println(format.format(data)); 1228 } 1229 1230 1237 private static String 1238 toString(byte b) 1239 { 1240 return "0x" + byteString(b); 1241 } 1242 1243 1250 private static String 1251 toString(byte[] bytes) 1252 { 1253 if (bytes == null) 1254 { 1255 return "null"; 1256 } 1257 1258 StringBuffer buf = new StringBuffer (bytes.length * 4); 1259 buf.append("0x["); 1260 boolean first = true; 1261 for (int i = 0; i < bytes.length; i++) 1262 { 1263 if (first && !(first = false)) 1264 { 1265 buf.append(byteString(bytes[i])); 1266 } 1267 else 1268 { 1269 buf.append(", ").append(byteString(bytes[i])); 1270 } 1271 } 1272 buf.append(']'); 1273 return buf.toString(); 1274 } 1275 1276 1283 private static String 1284 byteString(byte b) 1285 { 1286 return new String (new char[] {BYTE_CHARS[(b >> 4) & 0x0F], BYTE_CHARS[b & 0x0F]}); 1287 } 1288} | Popular Tags |