| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.commons.logging; |
| 34 | | | |
| 35 | | | import java.io.File; |
| 36 | | | import java.io.FileNotFoundException; |
| 37 | | | import java.io.FileOutputStream; |
| 38 | | | import java.io.PrintWriter; |
| 39 | | | import java.util.ArrayList; |
| 40 | | | import java.util.Arrays; |
| 41 | | | import java.util.Iterator; |
| 42 | | | import java.util.List; |
| 43 | | | import java.util.StringTokenizer; |
| 44 | | | |
| 45 | | | import org.apache.commons.cli.CommandLine; |
| 46 | | | import org.apache.commons.cli.GnuParser; |
| 47 | | | import org.apache.commons.cli.HelpFormatter; |
| 48 | | | import org.apache.commons.cli.Option; |
| 49 | | | import org.apache.commons.cli.OptionBuilder; |
| 50 | | | import org.apache.commons.cli.Options; |
| 51 | | | import org.apache.commons.cli.ParseException; |
| 52 | | | import org.jcoderz.commons.ArgumentMalformedException; |
| 53 | | | import org.jcoderz.commons.types.Date; |
| 54 | | | import org.jcoderz.commons.types.Period; |
| 55 | | | |
| 56 | | | |
| 57 | | | |
| 58 | | | |
| 59 | | | |
| 60 | | | |
| 61 | | | |
| 62 | | | |
| 63 | | (1) | |
| 64 | | | |
| 65 | | | |
| 66 | | | |
| 67 | | (2) | public final class LogViewer |
| 68 | | | implements Runnable |
| 69 | | | { |
| 70 | | | |
| 71 | 100 | | public static final String LINE_SEPARATOR |
| 72 | | | = System.getProperty("line.separator"); |
| 73 | | | |
| 74 | 100 | | private static final String DEFAULT_DIR = "." + File.separator + "log"; |
| 75 | | | private static final String DEFAULT_FILE = "log.out"; |
| 76 | | | |
| 77 | | | |
| 78 | | | private static final int LOGFILE_POLL_INTERVAL = 100; |
| 79 | | | |
| 80 | 100 | | private static final String [] EMPTY_STRINGS = new String[0]; |
| 81 | | | |
| 82 | | | |
| 83 | | | |
| 84 | | | |
| 85 | | | private static final int LEVEL_MESSAGE_STACKTRACE = 2; |
| 86 | | | |
| 87 | | | |
| 88 | | | |
| 89 | | | private static final int LEVEL_EXCEPTION_STACKTRACE = 1; |
| 90 | | | |
| 91 | | | |
| 92 | | | |
| 93 | | | private static final int DATE_MIN_LEN = 10; |
| 94 | | | |
| 95 | | | |
| 96 | | | private static final int PERIOD_TOKEN_COUNT = 2; |
| 97 | | | |
| 98 | | | |
| 99 | | | |
| 100 | | (3) | |
| 101 | | | |
| 102 | | | |
| 103 | | | |
| 104 | | | |
| 105 | 100 | | private static final Option LOGFILE_OPTION = OptionBuilder.hasArg() |
| 106 | | | .withArgName("logfile").withDescription( |
| 107 | | | "open file <logfile> instead of log.out") |
| 108 | | | .withValueSeparator().withLongOpt("logFile").create("F"); |
| 109 | | | |
| 110 | 100 | | private static final Option LOGDIR_OPTION = OptionBuilder.hasArg() |
| 111 | | | .withArgName("logdir").withDescription( |
| 112 | | | "find log files within <logdir> instead of ./log") |
| 113 | | | .withValueSeparator().withLongOpt("logDir").create("D"); |
| 114 | | | |
| 115 | 100 | | private static final Option LINES_OPTION = OptionBuilder.hasArg() |
| 116 | | | .withArgName("lines").withDescription( |
| 117 | | | "display <lines> last log records instead of 25") |
| 118 | | | .withValueSeparator().withLongOpt("lines").create("l"); |
| 119 | | | |
| 120 | 100 | | private static final Option DATE_OPTION = OptionBuilder.hasOptionalArgs() |
| 121 | | | .withArgName("dateFrom,dateTo").withDescription( |
| 122 | | | "display date [and search for given date/timestamp range. " |
| 123 | | | + "The following date formats are supported: " |
| 124 | | | + "<yyyy-MM-dd|yyyy-MM-dd'T'HH:mm:ss'Z'|" |
| 125 | | | + "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'>. One of both dates may be omitted].") |
| 126 | | | .withValueSeparator().withLongOpt("date").create("d"); |
| 127 | | | |
| 128 | 100 | | private static final Option TIME_OPTION = new Option("t", "timestamp", |
| 129 | | | false, "display timestamp."); |
| 130 | | | |
| 131 | 100 | | private static final Option BATCH_OPTION = new Option("b", "batch", false, |
| 132 | | | "batch mode, terminate if end of log file reached."); |
| 133 | | | |
| 134 | 100 | | private static final Option OUTFILE_OPTION = OptionBuilder.hasArg() |
| 135 | | | .withArgName("file").withDescription("send output to file") |
| 136 | | | .withValueSeparator().withLongOpt("outfile").create("o"); |
| 137 | | | |
| 138 | 100 | | private static final Option THREADID_OPTION = OptionBuilder.hasOptionalArgs() |
| 139 | | | .withArgName("tid1,tid2,...").withDescription( |
| 140 | | | "display thread id [and filter for given thread ids") |
| 141 | | | .withValueSeparator().withLongOpt("thread").create("T"); |
| 142 | | | |
| 143 | 100 | | private static final Option XML_OPTION |
| 144 | | | = new Option("xml", "output in xml format."); |
| 145 | | | |
| 146 | 100 | | private static final Option STACKTRACE_OPTION = OptionBuilder.hasArgs() |
| 147 | | | .withArgName("{0|1|2}").withDescription("display stack trace details; " |
| 148 | | | + "0: no stacktrace, 1: only for exceptions (default), " |
| 149 | | | + "2: all stack trace") |
| 150 | | | .withValueSeparator().create("stack"); |
| 151 | | | |
| 152 | 100 | | private static final Option IMPACT_OPTION = OptionBuilder.hasOptionalArgs() |
| 153 | | | .withArgName("bi1,bi2,...").withDescription("display business impact " |
| 154 | | | + "[and filter for given business impact ids]") |
| 155 | | | .withValueSeparator().withLongOpt("impact").create("i"); |
| 156 | | | |
| 157 | 100 | | private static final Option CATEGORY_OPTION = OptionBuilder.hasOptionalArgs() |
| 158 | | | .withArgName("cat1,cat2,...").withDescription("display category " |
| 159 | | | + "[and filter for given categories]") |
| 160 | | | .withValueSeparator().withLongOpt("cat").create("c"); |
| 161 | | | |
| 162 | 100 | | private static final Option LEVEL_OPTION = OptionBuilder.hasOptionalArgs() |
| 163 | | | .withArgName("lev1,lev2,...").withDescription("display log level " |
| 164 | | | + "[and filter for given levels]") |
| 165 | | | .withValueSeparator().withLongOpt("level").create("L"); |
| 166 | | | |
| 167 | 100 | | private static final Option STANDARD_OPTION = OptionBuilder |
| 168 | | | .withDescription("set standard mode, same as -t -i -c -L -stack 1") |
| 169 | | | .withLongOpt("standard").create("s"); |
| 170 | | | |
| 171 | | | |
| 172 | | | |
| 173 | | | |
| 174 | | | |
| 175 | | | |
| 176 | | | |
| 177 | | | |
| 178 | | | |
| 179 | | | |
| 180 | | | |
| 181 | | | |
| 182 | | | |
| 183 | | | |
| 184 | | | |
| 185 | | | |
| 186 | | | |
| 187 | | | |
| 188 | | | |
| 189 | | | |
| 190 | | | |
| 191 | | | |
| 192 | | | |
| 193 | | | |
| 194 | | | |
| 195 | | | |
| 196 | | | |
| 197 | | | |
| 198 | | | |
| 199 | | | |
| 200 | | | |
| 201 | | | |
| 202 | | | |
| 203 | | | |
| 204 | | | |
| 205 | | | |
| 206 | | | |
| 207 | | | |
| 208 | | | |
| 209 | | | |
| 210 | | | |
| 211 | | | |
| 212 | | | |
| 213 | | | |
| 214 | | | |
| 215 | | | |
| 216 | | | |
| 217 | | | |
| 218 | | | |
| 219 | | | |
| 220 | | | |
| 221 | | | |
| 222 | | | |
| 223 | | | |
| 224 | | | |
| 225 | | | |
| 226 | | | |
| 227 | | | |
| 228 | | | |
| 229 | | | |
| 230 | | | private PrintWriter mOut; |
| 231 | | | |
| 232 | | | private Options mOptions; |
| 233 | | | |
| 234 | | | private CommandLine mCommandLine; |
| 235 | | | private DisplayOptions mDisplayOptions; |
| 236 | 0 | | private final List mFilters = new ArrayList(); |
| 237 | | | private LogPrinter mDisplay; |
| 238 | | | |
| 239 | | | private LogReader mLogReader; |
| 240 | | | |
| 241 | | | private LogViewer () |
| 242 | 0 | | { |
| 243 | 0 | | installOptions(); |
| 244 | 0 | | } |
| 245 | | | |
| 246 | | | |
| 247 | | | |
| 248 | | | |
| 249 | | | |
| 250 | | | @param |
| 251 | | | |
| 252 | | | public static void main (String[] argv) |
| 253 | | | { |
| 254 | 0 | | final LogViewer logConsole = new LogViewer(); |
| 255 | | | |
| 256 | 0 | | if (argv.length < 1) |
| 257 | | | { |
| 258 | 0 | | logConsole.printUsage(); |
| 259 | | | } |
| 260 | | | else |
| 261 | | | { |
| 262 | | | try |
| 263 | | | { |
| 264 | 0 | | logConsole.readCommandLineArgs(argv); |
| 265 | 0 | | logConsole.init(); |
| 266 | 0 | | logConsole.run(); |
| 267 | | | } |
| 268 | 0 | | catch (Exception ex) |
| 269 | | | { |
| 270 | 0 | | Throwable th = ex; |
| 271 | 0 | | System.err.println("Caught exception: " + th); |
| 272 | 0 | | while (th != null) |
| 273 | | | { |
| 274 | 0 | | th.printStackTrace(); |
| 275 | 0 | | th = th.getCause(); |
| 276 | 0 | | if (th != null) |
| 277 | | | { |
| 278 | 0 | | System.err.println("Caused by: " + th); |
| 279 | | | } |
| 280 | | | } |
| 281 | | | } |
| 282 | | | finally |
| 283 | | | { |
| 284 | 0 | | logConsole.close(); |
| 285 | 0 | | } |
| 286 | | | } |
| 287 | 0 | | } |
| 288 | | | |
| 289 | | | private void init () |
| 290 | | | throws ArgumentMalformedException, java.text.ParseException |
| 291 | | | { |
| 292 | 0 | | initDisplayOptions(); |
| 293 | 0 | | initDisplayFilters(); |
| 294 | 0 | | setInput(); |
| 295 | 0 | | setOutput(); |
| 296 | 0 | | installDisplay(); |
| 297 | 0 | | } |
| 298 | | | |
| 299 | | | private void initDisplayFilters () |
| 300 | | | throws ArgumentMalformedException, java.text.ParseException |
| 301 | | | { |
| 302 | 0 | | initThreadIdFilter(); |
| 303 | 0 | | initBusinessImpactFilter(); |
| 304 | 0 | | initCategoryFilter(); |
| 305 | 0 | | initLogLevelFilter(); |
| 306 | 0 | | initPeriodFilter(); |
| 307 | 0 | | } |
| 308 | | | |
| 309 | | | private void initThreadIdFilter () |
| 310 | | | { |
| 311 | 0 | | if (mCommandLine.hasOption(THREADID_OPTION.getOpt())) |
| 312 | | | { |
| 313 | 0 | | final String[] threadIds = parseOptionValues( |
| 314 | | | mCommandLine.getOptionValues(THREADID_OPTION.getOpt())); |
| 315 | 0 | | if ((threadIds != null) && (threadIds.length > 0)) |
| 316 | | | { |
| 317 | 0 | | final List ids = new ArrayList(); |
| 318 | 0 | | for (int i = 0; i < threadIds.length; ++i) |
| 319 | | | { |
| 320 | 0 | | ids.add(Long.valueOf(threadIds[i])); |
| 321 | | | } |
| 322 | 0 | | mFilters.add(new ThreadIdFilter(ids)); |
| 323 | | | } |
| 324 | | | } |
| 325 | 0 | | } |
| 326 | | | |
| 327 | | | private void initBusinessImpactFilter () |
| 328 | | | { |
| 329 | 0 | | if (mCommandLine.hasOption(IMPACT_OPTION.getOpt())) |
| 330 | | | { |
| 331 | 0 | | final String[] impacts = parseOptionValues( |
| 332 | | | mCommandLine.getOptionValues(IMPACT_OPTION.getOpt())); |
| 333 | 0 | | if ((impacts != null) && (impacts.length > 0)) |
| 334 | | | { |
| 335 | 0 | | mFilters.add(new BusinessImpactFilter(Arrays.asList(impacts))); |
| 336 | | | } |
| 337 | | | } |
| 338 | 0 | | } |
| 339 | | | |
| 340 | | | private void initCategoryFilter () |
| 341 | | | { |
| 342 | 0 | | if (mCommandLine.hasOption(CATEGORY_OPTION.getOpt())) |
| 343 | | | { |
| 344 | 0 | | final String[] cats = parseOptionValues( |
| 345 | | | mCommandLine.getOptionValues(CATEGORY_OPTION.getOpt())); |
| 346 | 0 | | if ((cats != null) && (cats.length > 0)) |
| 347 | | | { |
| 348 | 0 | | mFilters.add(new CategoryFilter(Arrays.asList(cats))); |
| 349 | | | } |
| 350 | | | } |
| 351 | 0 | | } |
| 352 | | | |
| 353 | | | private void initLogLevelFilter () |
| 354 | | | { |
| 355 | 0 | | if (mCommandLine.hasOption(LEVEL_OPTION.getOpt())) |
| 356 | | | { |
| 357 | 0 | | final String[] levels = parseOptionValues( |
| 358 | | | mCommandLine.getOptionValues(LEVEL_OPTION.getOpt())); |
| 359 | 0 | | if ((levels != null) && (levels.length > 0)) |
| 360 | | | { |
| 361 | 0 | | mFilters.add(new LevelFilter(Arrays.asList(levels))); |
| 362 | | | } |
| 363 | | | } |
| 364 | 0 | | } |
| 365 | | | |
| 366 | | | private void initPeriodFilter () |
| 367 | | | throws ArgumentMalformedException, java.text.ParseException |
| 368 | | | { |
| 369 | 0 | | if (mCommandLine.hasOption(DATE_OPTION.getOpt())) |
| 370 | | | { |
| 371 | 0 | | final Period [] periods = getPeriodsFromOptionValues( |
| 372 | | | mCommandLine.getOptionValues(DATE_OPTION.getOpt())); |
| 373 | 0 | | if ((periods != null) && (periods.length > 0)) |
| 374 | | | { |
| 375 | 0 | | mFilters.add(new PeriodFilter(periods)); |
| 376 | | | } |
| 377 | | | } |
| 378 | 0 | | } |
| 379 | | | |
| 380 | | | static Period [] getPeriodsFromOptionValues (String [] optionValues) |
| 381 | | | throws ArgumentMalformedException, java.text.ParseException |
| 382 | | | { |
| 383 | 100 | | Period [] result = new Period[0]; |
| 384 | 100 | | final List vals = new ArrayList(); |
| 385 | 100 | | for (int i = 0; i < optionValues.length; i++) |
| 386 | | | { |
| 387 | 100 | | final List values = new ArrayList(); |
| 388 | 100 | | final StringTokenizer tokens = new StringTokenizer( |
| 389 | | | optionValues[i], ","); |
| 390 | 100 | | while (tokens.hasMoreTokens()) |
| 391 | | | { |
| 392 | 100 | | values.add(tokens.nextToken()); |
| 393 | | | } |
| 394 | | | |
| 395 | 100 | | String dateFrom = null; |
| 396 | 100 | | String dateTo = null; |
| 397 | 100 | | if (values.size() != PERIOD_TOKEN_COUNT) |
| 398 | | | { |
| 399 | 100 | | if (values.size() == 1 && optionValues[i].startsWith(",", 0)) |
| 400 | | | { |
| 401 | 100 | | dateTo = (String) values.get(0); |
| 402 | | | } |
| 403 | 100 | | else if (values.size() == 1) |
| 404 | | | { |
| 405 | 100 | | dateFrom = (String) values.get(0); |
| 406 | | | } |
| 407 | | | else |
| 408 | | | { |
| 409 | 0 | | throw new LoggingException("Illegal time interval has been " |
| 410 | | | + "specified in the command line " + optionValues[i]); |
| 411 | | | } |
| 412 | | | } |
| 413 | | | else |
| 414 | | | { |
| 415 | 100 | | dateFrom = (String) values.get(0); |
| 416 | 100 | | dateTo = (String) values.get(1); |
| 417 | | | } |
| 418 | | | |
| 419 | 100 | | final Period period = Period.createPeriod(getDateFrom(dateFrom), |
| 420 | | | getDateTo(dateTo)); |
| 421 | | | |
| 422 | 100 | | vals.add(period); |
| 423 | | | } |
| 424 | | | |
| 425 | 100 | | result = (Period []) vals.toArray(result); |
| 426 | | | |
| 427 | 100 | (4) | return result; |
| 428 | | | } |
| 429 | | | |
| 430 | | | static Date getDateFrom (String str) |
| 431 | | | throws java.text.ParseException |
| 432 | | | { |
| 433 | 100 | | return getDateFromOptValue(str, true); |
| 434 | | | } |
| 435 | | | |
| 436 | | | static Date getDateTo (String str) |
| 437 | | | throws java.text.ParseException |
| 438 | | | { |
| 439 | 100 | | return getDateFromOptValue(str, false); |
| 440 | | | } |
| 441 | | | |
| 442 | | | static Date getDateFromOptValue (String str, boolean dateFrom) |
| 443 | | | throws java.text.ParseException |
| 444 | | | { |
| 445 | | | final Date result; |
| 446 | 100 | | if (str == null || str.length() == 0) |
| 447 | | | { |
| 448 | 100 | | result = dateFrom ? Date.OLD_DATE : Date.FUTURE_DATE; |
| 449 | | | } |
| 450 | | | else |
| 451 | | | { |
| 452 | 100 | | if (str.length() == DATE_MIN_LEN) |
| 453 | | | { |
| 454 | 100 | | result = Date.fromString(str + "Z", Date.DATE_FORMAT); |
| 455 | | | } |
| 456 | 100 | | else if (str.length() == DATE_MIN_LEN + 1) |
| 457 | | | { |
| 458 | 100 | | result = Date.fromString(str, Date.DATE_FORMAT); |
| 459 | | | } |
| 460 | | | else |
| 461 | | | { |
| 462 | 100 | | result = Date.fromString(str); |
| 463 | | | } |
| 464 | | | } |
| 465 | 100 | | return result; |
| 466 | | | } |
| 467 | | | |
| 468 | | | |
| 469 | | | |
| 470 | | | <code></code> |
| 471 | | | |
| 472 | | | |
| 473 | | | @param |
| 474 | | | |
| 475 | | | @return |
| 476 | | | |
| 477 | | | private String [] parseOptionValues (final String [] optionValues) |
| 478 | | | { |
| 479 | 0 | | String[] rc = null; |
| 480 | 0 | | if (optionValues != null) |
| 481 | | | { |
| 482 | 0 | | final List values = new ArrayList(); |
| 483 | 0 | | for (int i = 0; i < optionValues.length; ++i) |
| 484 | | | { |
| 485 | 0 | | final StringTokenizer tokens = new StringTokenizer( |
| 486 | | | optionValues[i], "\t ,"); |
| 487 | 0 | | while (tokens.hasMoreTokens()) |
| 488 | | | { |
| 489 | 0 | | values.add(tokens.nextToken()); |
| 490 | | | } |
| 491 | | | } |
| 492 | 0 | | rc = (String[]) values.toArray(EMPTY_STRINGS); |
| 493 | | | } |
| 494 | 0 | | return rc; |
| 495 | | | } |
| 496 | | | |
| 497 | | | |
| 498 | | | |
| 499 | | | |
| 500 | | (5) | private void initDisplayOptions () |
| 501 | | | { |
| 502 | 0 | | mDisplayOptions = new DisplayOptions(); |
| 503 | 0 | | if (mCommandLine.hasOption(THREADID_OPTION.getOpt())) |
| 504 | | | { |
| 505 | 0 | | mDisplayOptions.displayThreadId(true); |
| 506 | | | } |
| 507 | 0 | | if (mCommandLine.hasOption(TIME_OPTION.getOpt())) |
| 508 | | | { |
| 509 | 0 | | mDisplayOptions.displayTimestamp(true); |
| 510 | | | } |
| 511 | 0 | | if (mCommandLine.hasOption(DATE_OPTION.getOpt())) |
| 512 | | | { |
| 513 | 0 | | mDisplayOptions.displayTimestamp(true); |
| 514 | | | } |
| 515 | 0 | | if (mCommandLine.hasOption(IMPACT_OPTION.getOpt())) |
| 516 | | | { |
| 517 | 0 | | mDisplayOptions.displayBusinessImpact(true); |
| 518 | | | } |
| 519 | 0 | | if (mCommandLine.hasOption(CATEGORY_OPTION.getOpt())) |
| 520 | | | { |
| 521 | 0 | | mDisplayOptions.displayCategory(true); |
| 522 | | | } |
| 523 | 0 | | if (mCommandLine.hasOption(LEVEL_OPTION.getOpt())) |
| 524 | | | { |
| 525 | 0 | | mDisplayOptions.displayLoggerLevel(true); |
| 526 | | | } |
| 527 | 0 | | if (mCommandLine.hasOption(STACKTRACE_OPTION.getOpt())) |
| 528 | | | { |
| 529 | 0 | | setStackTraceDetails(); |
| 530 | | | } |
| 531 | 0 | | if (mCommandLine.hasOption(STANDARD_OPTION.getOpt())) |
| 532 | | | { |
| 533 | 0 | | setStandardDisplay(mDisplayOptions); |
| 534 | | | } |
| 535 | 0 | | } |
| 536 | | | |
| 537 | | | private void setStandardDisplay (final DisplayOptions dp) |
| 538 | | | { |
| 539 | 0 | | dp.displayTimestamp(true); |
| 540 | 0 | | dp.displayBusinessImpact(true); |
| 541 | 0 | | dp.displayCategory(true); |
| 542 | 0 | | dp.displayLoggerLevel(true); |
| 543 | 0 | | dp.displayStackTrace(true); |
| 544 | 0 | | dp.displayMessageStackTrace(false); |
| 545 | 0 | | } |
| 546 | | | |
| 547 | | | private void setStackTraceDetails () |
| 548 | | | { |
| 549 | 0 | | final String[] details = parseOptionValues( |
| 550 | | | mCommandLine.getOptionValues(STACKTRACE_OPTION.getOpt())); |
| 551 | 0 | | if ((details != null) && (details.length > 0)) |
| 552 | | | { |
| 553 | 0 | | final int level = Integer.parseInt(details[0]); |
| 554 | 0 | | mDisplayOptions.displayMessageStackTrace( |
| 555 | | | level >= LEVEL_MESSAGE_STACKTRACE); |
| 556 | 0 | | mDisplayOptions.displayStackTrace( |
| 557 | | | level >= LEVEL_EXCEPTION_STACKTRACE); |
| 558 | | | } |
| 559 | 0 | | } |
| 560 | | | |
| 561 | | | private void setInput () |
| 562 | | | throws LoggingException |
| 563 | | | { |
| 564 | 0 | | final String logDir = mCommandLine.getOptionValue( |
| 565 | | | LOGDIR_OPTION.getOpt(), DEFAULT_DIR); |
| 566 | | | |
| 567 | 0 | | if ((logDir == null) || (logDir.length() == 0)) |
| 568 | | | { |
| 569 | 0 | | throw new LoggingException("Undefined log directory."); |
| 570 | | | } |
| 571 | | | |
| 572 | 0 | | final String fileName = logDir + File.separator |
| 573 | | | + mCommandLine.getOptionValue( |
| 574 | | | LOGFILE_OPTION.getOpt(), DEFAULT_FILE); |
| 575 | | | |
| 576 | | | final LogReader logReader; |
| 577 | | | try |
| 578 | | | { |
| 579 | 0 | | logReader = new LogReader(fileName); |
| 580 | 0 | | setFilters(logReader); |
| 581 | 0 | | mLogReader = logReader; |
| 582 | | | } |
| 583 | 0 | | catch (InstantiationException ex) |
| 584 | | | { |
| 585 | 0 | | throw new LoggingException("Error instantiating a LogReader for file " |
| 586 | | | + fileName, ex); |
| 587 | 0 | | } |
| 588 | 0 | | } |
| 589 | | | |
| 590 | | | private void setOutput () |
| 591 | | | throws LoggingException |
| 592 | | | { |
| 593 | 0 | | if (mCommandLine.hasOption(OUTFILE_OPTION.getOpt())) |
| 594 | | | { |
| 595 | 0 | | final String fileName = mCommandLine.getOptionValue( |
| 596 | | | OUTFILE_OPTION.getOpt()); |
| 597 | | | try |
| 598 | | | { |
| 599 | 0 | | final File file = new File(fileName); |
| 600 | 0 | | mOut = new PrintWriter(new FileOutputStream(file)); |
| 601 | | | } |
| 602 | 0 | | catch (FileNotFoundException ex) |
| 603 | | | { |
| 604 | 0 | | throw new LoggingException( |
| 605 | | | "Could not open the output file " + fileName, ex); |
| 606 | 0 | | } |
| 607 | 0 | | } |
| 608 | | | else |
| 609 | | | { |
| 610 | 0 | | mOut = new PrintWriter(System.out); |
| 611 | | | } |
| 612 | 0 | | } |
| 613 | | | |
| 614 | | | |
| 615 | | | |
| 616 | | | |
| 617 | | | private void close () |
| 618 | | | { |
| 619 | 0 | | if (mOut != null) |
| 620 | | | { |
| 621 | 0 | | mOut.flush(); |
| 622 | 0 | | mOut.close(); |
| 623 | | | } |
| 624 | 0 | | if (mLogReader != null) |
| 625 | | | { |
| 626 | 0 | | mLogReader.close(); |
| 627 | | | } |
| 628 | 0 | | } |
| 629 | | | |
| 630 | | | |
| 631 | | | |
| 632 | | | |
| 633 | | | |
| 634 | | | |
| 635 | | | @see |
| 636 | | | |
| 637 | | | public void run () |
| 638 | | | { |
| 639 | | | try |
| 640 | | | { |
| 641 | 0 | | if (mCommandLine.hasOption(BATCH_OPTION.getOpt())) |
| 642 | | | { |
| 643 | 0 | | runBatchMode(); |
| 644 | | | } |
| 645 | | | else |
| 646 | | | { |
| 647 | 0 | | runLiveMode(); |
| 648 | | | } |
| 649 | | | } |
| 650 | 0 | | catch (Exception ex) |
| 651 | | | { |
| 652 | 0 | | System.err.println("Caught exception while viewing log file: " + ex); |
| 653 | 0 | | ex.printStackTrace(); |
| 654 | 0 | | } |
| 655 | 0 | | } |
| 656 | | | |
| 657 | | | |
| 658 | | | |
| 659 | | | |
| 660 | | | private void runLiveMode () |
| 661 | | | { |
| 662 | | | while (true) |
| 663 | | | { |
| 664 | 0 | | LogFileEntry logRecord = null; |
| 665 | | | |
| 666 | 0 | | if (mLogReader.available()) |
| 667 | | | { |
| 668 | | | do |
| 669 | | | { |
| 670 | 0 | | logRecord = mLogReader.readLogFileEntry(); |
| 671 | 0 | | if ((logRecord != null) && (mDisplay != null)) |
| 672 | | | { |
| 673 | 0 | | mDisplay.print(mOut, logRecord); |
| 674 | 0 | | logRecord.release(); |
| 675 | | | } |
| 676 | | | } |
| 677 | 0 | | while (logRecord != null); |
| 678 | 0 | | mOut.flush(); |
| 679 | | | } |
| 680 | | | |
| 681 | | | try |
| 682 | | | { |
| 683 | 0 | | Thread.sleep(LOGFILE_POLL_INTERVAL); |
| 684 | | | } |
| 685 | 0 | | catch (InterruptedException e) |
| 686 | | | { |
| 687 | | | |
| 688 | 0 | | } |
| 689 | 0 | | } |
| 690 | | | } |
| 691 | | | |
| 692 | | | |
| 693 | | | |
| 694 | | | |
| 695 | | | private void runBatchMode () |
| 696 | | | { |
| 697 | 0 | | LogFileEntry logRecord = null; |
| 698 | | | do |
| 699 | | | { |
| 700 | 0 | | logRecord = mLogReader.readLogFileEntry(); |
| 701 | 0 | | if ((logRecord != null) && (mDisplay != null)) |
| 702 | | | { |
| 703 | 0 | | mDisplay.print(mOut, logRecord); |
| 704 | 0 | | logRecord.release(); |
| 705 | | | } |
| 706 | | | } |
| 707 | 0 | | while (logRecord != null); |
| 708 | | | |
| 709 | 0 | | mOut.flush(); |
| 710 | 0 | | } |
| 711 | | | |
| 712 | | | private void installOptions () |
| 713 | | | { |
| 714 | 0 | | mOptions = new Options(); |
| 715 | | | |
| 716 | 0 | | mOptions.addOption(LOGFILE_OPTION); |
| 717 | 0 | | mOptions.addOption(LOGDIR_OPTION); |
| 718 | | | |
| 719 | 0 | | mOptions.addOption(BATCH_OPTION); |
| 720 | 0 | | mOptions.addOption(OUTFILE_OPTION); |
| 721 | 0 | | mOptions.addOption(XML_OPTION); |
| 722 | 0 | | mOptions.addOption(STACKTRACE_OPTION); |
| 723 | 0 | | mOptions.addOption(DATE_OPTION); |
| 724 | 0 | | mOptions.addOption(TIME_OPTION); |
| 725 | 0 | | mOptions.addOption(THREADID_OPTION); |
| 726 | 0 | | mOptions.addOption(IMPACT_OPTION); |
| 727 | 0 | | mOptions.addOption(CATEGORY_OPTION); |
| 728 | 0 | | mOptions.addOption(LEVEL_OPTION); |
| 729 | 0 | | mOptions.addOption(STANDARD_OPTION); |
| 730 | 0 | | } |
| 731 | | | |
| 732 | | | private void readCommandLineArgs (final String[] args) |
| 733 | | | throws ParseException |
| 734 | | | { |
| 735 | 0 | | mCommandLine = new GnuParser().parse(mOptions, args, true); |
| 736 | 0 | | } |
| 737 | | | |
| 738 | | | private void installDisplay () |
| 739 | | | throws LoggingException |
| 740 | | | { |
| 741 | 0 | | if (mCommandLine.hasOption(XML_OPTION.getOpt())) |
| 742 | | | { |
| 743 | | | try |
| 744 | | | { |
| 745 | 0 | | mDisplay = new XmlPrinter(); |
| 746 | | | } |
| 747 | 0 | | catch (InstantiationException ex) |
| 748 | | | { |
| 749 | 0 | | throw new LoggingException("Error instantiating an XmlPrinter", ex); |
| 750 | 0 | | } |
| 751 | | | } |
| 752 | | | else |
| 753 | | | { |
| 754 | 0 | | mDisplay = new BasicPrinter(); |
| 755 | | | } |
| 756 | 0 | | mDisplay.setDisplayOptions(mDisplayOptions); |
| 757 | 0 | | } |
| 758 | | | |
| 759 | | | |
| 760 | | | |
| 761 | | | |
| 762 | | | |
| 763 | | | @param |
| 764 | | | |
| 765 | | (6) | |
| 766 | | | |
| 767 | | | private void setFilters (final LogReader logReader) |
| 768 | | | { |
| 769 | 0 | | for (final Iterator iter = mFilters.iterator(); iter.hasNext(); ) |
| 770 | | | { |
| 771 | 0 | | logReader.addFilter((Filter) iter.next()); |
| 772 | | | } |
| 773 | 0 | | } |
| 774 | | | |
| 775 | | | private void printUsage () |
| 776 | | | { |
| 777 | 0 | | new HelpFormatter().printHelp( |
| 778 | | | "java org.jcoderz.commons.LogViewer", mOptions); |
| 779 | 0 | | } |
| 780 | | | } |