| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * |
|---|
| 4 | * Copyright 2006, The jCoderZ.org Project. All rights reserved. |
|---|
| 5 | * |
|---|
| 6 | * Redistribution and use in source and binary forms, with or without |
|---|
| 7 | * modification, are permitted provided that the following conditions are |
|---|
| 8 | * met: |
|---|
| 9 | * |
|---|
| 10 | * * Redistributions of source code must retain the above copyright |
|---|
| 11 | * notice, this list of conditions and the following disclaimer. |
|---|
| 12 | * * Redistributions in binary form must reproduce the above |
|---|
| 13 | * copyright notice, this list of conditions and the following |
|---|
| 14 | * disclaimer in the documentation and/or other materials |
|---|
| 15 | * provided with the distribution. |
|---|
| 16 | * * Neither the name of the jCoderZ.org Project nor the names of |
|---|
| 17 | * its contributors may be used to endorse or promote products |
|---|
| 18 | * derived from this software without specific prior written |
|---|
| 19 | * permission. |
|---|
| 20 | * |
|---|
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND |
|---|
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|---|
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|---|
| 24 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS |
|---|
| 25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|---|
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|---|
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
|---|
| 28 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|---|
| 29 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|---|
| 30 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|---|
| 31 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 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 | * This implements a viewer for existing log files. The output generated by |
|---|
| 60 | * this should never be viewed using this, since the format might be different |
|---|
| 61 | * to a 'real' log file. |
|---|
| 62 | * |
|---|
| 63 | * TODO: A lot of functionality is still to implement (behaviour according to |
|---|
| 64 | * command line options). |
|---|
| 65 | * |
|---|
| 66 | */ |
|---|
| 67 | public final class LogViewer |
|---|
| 68 | implements Runnable |
|---|
| 69 | { |
|---|
| 70 | /** Line separator to be used in output files. */ |
|---|
| 71 | public static final String LINE_SEPARATOR |
|---|
| 72 | = System.getProperty("line.separator"); |
|---|
| 73 | |
|---|
| 74 | private static final String DEFAULT_DIR = "." + File.separator + "log"; |
|---|
| 75 | private static final String DEFAULT_FILE = "log.out"; |
|---|
| 76 | |
|---|
| 77 | /** Time interval [ms] used for polling the log file for new data. */ |
|---|
| 78 | private static final int LOGFILE_POLL_INTERVAL = 100; |
|---|
| 79 | |
|---|
| 80 | private static final String [] EMPTY_STRINGS = new String[0]; |
|---|
| 81 | |
|---|
| 82 | /** |
|---|
| 83 | * Level for displaying message stack trace. |
|---|
| 84 | */ |
|---|
| 85 | private static final int LEVEL_MESSAGE_STACKTRACE = 2; |
|---|
| 86 | /** |
|---|
| 87 | * Level for displaying exception stack trace. |
|---|
| 88 | */ |
|---|
| 89 | private static final int LEVEL_EXCEPTION_STACKTRACE = 1; |
|---|
| 90 | /** |
|---|
| 91 | * The min length of the date argument. |
|---|
| 92 | */ |
|---|
| 93 | private static final int DATE_MIN_LEN = 10; |
|---|
| 94 | |
|---|
| 95 | /** Number of tokens expected for a period. */ |
|---|
| 96 | private static final int PERIOD_TOKEN_COUNT = 2; |
|---|
| 97 | |
|---|
| 98 | /** |
|---|
| 99 | * Command line options. |
|---|
| 100 | * TODO: Extend options and implement usage. |
|---|
| 101 | * |
|---|
| 102 | * The way OptionsBuilder is used is as proposed by the authors |
|---|
| 103 | * of the commonscli package, but cause checkstyle warnings here... |
|---|
| 104 | */ |
|---|
| 105 | 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 | 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 | 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 | 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 | private static final Option TIME_OPTION = new Option("t", "timestamp", |
|---|
| 129 | false, "display timestamp."); |
|---|
| 130 | |
|---|
| 131 | private static final Option BATCH_OPTION = new Option("b", "batch", false, |
|---|
| 132 | "batch mode, terminate if end of log file reached."); |
|---|
| 133 | |
|---|
| 134 | private static final Option OUTFILE_OPTION = OptionBuilder.hasArg() |
|---|
| 135 | .withArgName("file").withDescription("send output to file") |
|---|
| 136 | .withValueSeparator().withLongOpt("outfile").create("o"); |
|---|
| 137 | |
|---|
| 138 | 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 | private static final Option XML_OPTION |
|---|
| 144 | = new Option("xml", "output in xml format."); |
|---|
| 145 | |
|---|
| 146 | 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 | 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 | 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 | 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 | 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 | System.err.println( |
|---|
| 173 | " -P[id] PID " |
|---|
| 174 | + "display process ID [and search for given process ID]"); |
|---|
| 175 | System.err.println( |
|---|
| 176 | " -r[ecno] [MIN[,MAX]] " |
|---|
| 177 | + "display record number [and search for given record range]"); |
|---|
| 178 | System.err.println( |
|---|
| 179 | " -e[rrordetails] LEVEL " |
|---|
| 180 | + "set error details to given level"); |
|---|
| 181 | System.err.println( |
|---|
| 182 | " -tr[acedetails] LEVEL " |
|---|
| 183 | + "set trace details to given level"); |
|---|
| 184 | System.err.println( |
|---|
| 185 | " -s " |
|---|
| 186 | + "standard mode: same as -t -r -P -IP"); |
|---|
| 187 | System.err.println( |
|---|
| 188 | " -f[ormats] (e|t) " |
|---|
| 189 | + "display only error or trace messages"); |
|---|
| 190 | System.err.println( |
|---|
| 191 | " -lev[el] [<lev1>,[<lev2>]] " |
|---|
| 192 | + "display message level [and search for given level range]"); |
|---|
| 193 | System.err.println( |
|---|
| 194 | " -logger display logger name"); |
|---|
| 195 | System.err.println(" -class display class name"); |
|---|
| 196 | System.err.println( |
|---|
| 197 | " -method display method name"); |
|---|
| 198 | System.err.println( |
|---|
| 199 | " -enc[oding] <encoding> " |
|---|
| 200 | + "define the encoding for standard output,"); |
|---|
| 201 | System.err.println( |
|---|
| 202 | " " |
|---|
| 203 | + "e.g. cp437 (PC US) or cp850 (PC latin1 with Euro)"); |
|---|
| 204 | System.err.println( |
|---|
| 205 | " " |
|---|
| 206 | + "Note: use CHCP to find out the codepage used by DOS."); |
|---|
| 207 | |
|---|
| 208 | -log [-l[ines]=NO] - display NO last lines, if |
|---|
| 209 | NO is -1, ALL lines will be displayed, |
|---|
| 210 | default: use the last 25 records |
|---|
| 211 | combine other filter options with -l |
|---|
| 212 | [-P[id=PID]] [with PID] |
|---|
| 213 | [-X[id=TxID]] [with TxID] |
|---|
| 214 | [-S[essionID=SID]] [with SID] |
|---|
| 215 | [-IP] [with IP] |
|---|
| 216 | [-p[rogram=NAME]] [with NAME] |
|---|
| 217 | [-r[ecno]] [show record number] |
|---|
| 218 | [-e[rrordetails]=LEVEL] [set errordetails to level 'LEVEL'] |
|---|
| 219 | [-tr[acedetails]=LEVEL] [set tracedetails to level 'LEVEL'] |
|---|
| 220 | [-s] [standard: same as -t -r -P -IP] |
|---|
| 221 | [-f[ormats]=(e|t)] [choose only error or trace to be |
|---|
| 222 | displayed] |
|---|
| 223 | -lev[el] [<lev1>,[<lev2>]] display message level [and search for |
|---|
| 224 | given level range] |
|---|
| 225 | -logger display logger name |
|---|
| 226 | -class display class name |
|---|
| 227 | -method display method name |
|---|
| 228 | -enc[oding] <encoding> define the encoding for standard, |
|---|
| 229 | */ |
|---|
| 230 | private PrintWriter mOut; |
|---|
| 231 | |
|---|
| 232 | private Options mOptions; |
|---|
| 233 | |
|---|
| 234 | private CommandLine mCommandLine; |
|---|
| 235 | private DisplayOptions mDisplayOptions; |
|---|
| 236 | private final List mFilters = new ArrayList(); |
|---|
| 237 | private LogPrinter mDisplay; |
|---|
| 238 | |
|---|
| 239 | private LogReader mLogReader; |
|---|
| 240 | |
|---|
| 241 | private LogViewer () |
|---|
| 242 | { |
|---|
| 243 | installOptions(); |
|---|
| 244 | } |
|---|
| 245 | |
|---|
| 246 | /** |
|---|
| 247 | * The main entry method. Installs a new instance of this, which reads the |
|---|
| 248 | * supplied log file. |
|---|
| 249 | * |
|---|
| 250 | * @param argv Contains the command line arguments. |
|---|
| 251 | */ |
|---|
| 252 | public static void main (String[] argv) |
|---|
| 253 | { |
|---|
| 254 | final LogViewer logConsole = new LogViewer(); |
|---|
| 255 | |
|---|
| 256 | if (argv.length < 1) |
|---|
| 257 | { |
|---|
| 258 | logConsole.printUsage(); |
|---|
| 259 | } |
|---|
| 260 | else |
|---|
| 261 | { |
|---|
| 262 | try |
|---|
| 263 | { |
|---|
| 264 | logConsole.readCommandLineArgs(argv); |
|---|
| 265 | logConsole.init(); |
|---|
| 266 | logConsole.run(); |
|---|
| 267 | } |
|---|
| 268 | catch (Exception ex) |
|---|
| 269 | { |
|---|
| 270 | Throwable th = ex; |
|---|
| 271 | System.err.println("Caught exception: " + th); |
|---|
| 272 | while (th != null) |
|---|
| 273 | { |
|---|
| 274 | th.printStackTrace(); |
|---|
| 275 | th = th.getCause(); |
|---|
| 276 | if (th != null) |
|---|
| 277 | { |
|---|
| 278 | System.err.println("Caused by: " + th); |
|---|
| 279 | } |
|---|
| 280 | } |
|---|
| 281 | } |
|---|
| 282 | finally |
|---|
| 283 | { |
|---|
| 284 | logConsole.close(); |
|---|
| 285 | } |
|---|
| 286 | } |
|---|
| 287 | } |
|---|
| 288 | |
|---|
| 289 | private void init () |
|---|
| 290 | throws ArgumentMalformedException, java.text.ParseException |
|---|
| 291 | { |
|---|
| 292 | initDisplayOptions(); |
|---|
| 293 | initDisplayFilters(); |
|---|
| 294 | setInput(); |
|---|
| 295 | setOutput(); |
|---|
| 296 | installDisplay(); |
|---|
| 297 | } |
|---|
| 298 | |
|---|
| 299 | private void initDisplayFilters () |
|---|
| 300 | throws ArgumentMalformedException, java.text.ParseException |
|---|
| 301 | { |
|---|
| 302 | initThreadIdFilter(); |
|---|
| 303 | initBusinessImpactFilter(); |
|---|
| 304 | initCategoryFilter(); |
|---|
| 305 | initLogLevelFilter(); |
|---|
| 306 | initPeriodFilter(); |
|---|
| 307 | } |
|---|
| 308 | |
|---|
| 309 | private void initThreadIdFilter () |
|---|
| 310 | { |
|---|
| 311 | if (mCommandLine.hasOption(THREADID_OPTION.getOpt())) |
|---|
| 312 | { |
|---|
| 313 | final String[] threadIds = parseOptionValues( |
|---|
| 314 | mCommandLine.getOptionValues(THREADID_OPTION.getOpt())); |
|---|
| 315 | if ((threadIds != null) && (threadIds.length > 0)) |
|---|
| 316 | { |
|---|
| 317 | final List ids = new ArrayList(); |
|---|
| 318 | for (int i = 0; i < threadIds.length; ++i) |
|---|
| 319 | { |
|---|
| 320 | ids.add(Long.valueOf(threadIds[i])); |
|---|
| 321 | } |
|---|
| 322 | mFilters.add(new ThreadIdFilter(ids)); |
|---|
| 323 | } |
|---|
| 324 | } |
|---|
| 325 | } |
|---|
| 326 | |
|---|
| 327 | private void initBusinessImpactFilter () |
|---|
| 328 | { |
|---|
| 329 | if (mCommandLine.hasOption(IMPACT_OPTION.getOpt())) |
|---|
| 330 | { |
|---|
| 331 | final String[] impacts = parseOptionValues( |
|---|
| 332 | mCommandLine.getOptionValues(IMPACT_OPTION.getOpt())); |
|---|
| 333 | if ((impacts != null) && (impacts.length > 0)) |
|---|
| 334 | { |
|---|
| 335 | mFilters.add(new BusinessImpactFilter(Arrays.asList(impacts))); |
|---|
| 336 | } |
|---|
| 337 | } |
|---|
| 338 | } |
|---|
| 339 | |
|---|
| 340 | private void initCategoryFilter () |
|---|
| 341 | { |
|---|
| 342 | if (mCommandLine.hasOption(CATEGORY_OPTION.getOpt())) |
|---|
| 343 | { |
|---|
| 344 | final String[] cats = parseOptionValues( |
|---|
| 345 | mCommandLine.getOptionValues(CATEGORY_OPTION.getOpt())); |
|---|
| 346 | if ((cats != null) && (cats.length > 0)) |
|---|
| 347 | { |
|---|
| 348 | mFilters.add(new CategoryFilter(Arrays.asList(cats))); |
|---|
| 349 | } |
|---|
| 350 | } |
|---|
| 351 | } |
|---|
| 352 | |
|---|
| 353 | private void initLogLevelFilter () |
|---|
| 354 | { |
|---|
| 355 | if (mCommandLine.hasOption(LEVEL_OPTION.getOpt())) |
|---|
| 356 | { |
|---|
| 357 | final String[] levels = parseOptionValues( |
|---|
| 358 | mCommandLine.getOptionValues(LEVEL_OPTION.getOpt())); |
|---|
| 359 | if ((levels != null) && (levels.length > 0)) |
|---|
| 360 | { |
|---|
| 361 | mFilters.add(new LevelFilter(Arrays.asList(levels))); |
|---|
| 362 | } |
|---|
| 363 | } |
|---|
| 364 | } |
|---|
| 365 | |
|---|
| 366 | private void initPeriodFilter () |
|---|
| 367 | throws ArgumentMalformedException, java.text.ParseException |
|---|
| 368 | { |
|---|
| 369 | if (mCommandLine.hasOption(DATE_OPTION.getOpt())) |
|---|
| 370 | { |
|---|
| 371 | final Period [] periods = getPeriodsFromOptionValues( |
|---|
| 372 | mCommandLine.getOptionValues(DATE_OPTION.getOpt())); |
|---|
| 373 | if ((periods != null) && (periods.length > 0)) |
|---|
| 374 | { |
|---|
| 375 | mFilters.add(new PeriodFilter(periods)); |
|---|
| 376 | } |
|---|
| 377 | } |
|---|
| 378 | } |
|---|
| 379 | |
|---|
| 380 | static Period [] getPeriodsFromOptionValues (String [] optionValues) |
|---|
| 381 | throws ArgumentMalformedException, java.text.ParseException |
|---|
| 382 | { |
|---|
| 383 | Period [] result = new Period[0]; |
|---|
| 384 | final List vals = new ArrayList(); |
|---|
| 385 | for (int i = 0; i < optionValues.length; i++) |
|---|
| 386 | { |
|---|
| 387 | final List values = new ArrayList(); |
|---|
| 388 | final StringTokenizer tokens = new StringTokenizer( |
|---|
| 389 | optionValues[i], ","); |
|---|
| 390 | while (tokens.hasMoreTokens()) |
|---|
| 391 | { |
|---|
| 392 | values.add(tokens.nextToken()); |
|---|
| 393 | } |
|---|
| 394 | |
|---|
| 395 | String dateFrom = null; |
|---|
| 396 | String dateTo = null; |
|---|
| 397 | if (values.size() != PERIOD_TOKEN_COUNT) |
|---|
| 398 | { |
|---|
| 399 | if (values.size() == 1 && optionValues[i].startsWith(",", 0)) |
|---|
| 400 | { |
|---|
| 401 | dateTo = (String) values.get(0); |
|---|
| 402 | } |
|---|
| 403 | else if (values.size() == 1) |
|---|
| 404 | { |
|---|
| 405 | dateFrom = (String) values.get(0); |
|---|
| 406 | } |
|---|
| 407 | else |
|---|
| 408 | { |
|---|
| 409 | throw new LoggingException("Illegal time interval has been " |
|---|
| 410 | + "specified in the command line " + optionValues[i]); |
|---|
| 411 | } |
|---|
| 412 | } |
|---|
| 413 | else |
|---|
| 414 | { |
|---|
| 415 | dateFrom = (String) values.get(0); |
|---|
| 416 | dateTo = (String) values.get(1); |
|---|
| 417 | } |
|---|
| 418 | |
|---|
| 419 | final Period period = Period.createPeriod(getDateFrom(dateFrom), |
|---|
| 420 | getDateTo(dateTo)); |
|---|
| 421 | |
|---|
| 422 | vals.add(period); |
|---|
| 423 | } |
|---|
| 424 | |
|---|
| 425 | result = (Period []) vals.toArray(result); |
|---|
| 426 | |
|---|
| 427 | return result; |
|---|
| 428 | } |
|---|
| 429 | |
|---|
| 430 | static Date getDateFrom (String str) |
|---|
| 431 | throws java.text.ParseException |
|---|
| 432 | { |
|---|
| 433 | return getDateFromOptValue(str, true); |
|---|
| 434 | } |
|---|
| 435 | |
|---|
| 436 | static Date getDateTo (String str) |
|---|
| 437 | throws java.text.ParseException |
|---|
| 438 | { |
|---|
| 439 | 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 | if (str == null || str.length() == 0) |
|---|
| 447 | { |
|---|
| 448 | result = dateFrom ? Date.OLD_DATE : Date.FUTURE_DATE; |
|---|
| 449 | } |
|---|
| 450 | else |
|---|
| 451 | { |
|---|
| 452 | if (str.length() == DATE_MIN_LEN) |
|---|
| 453 | { |
|---|
| 454 | result = Date.fromString(str + "Z", Date.DATE_FORMAT); |
|---|
| 455 | } |
|---|
| 456 | else if (str.length() == DATE_MIN_LEN + 1) |
|---|
| 457 | { |
|---|
| 458 | result = Date.fromString(str, Date.DATE_FORMAT); |
|---|
| 459 | } |
|---|
| 460 | else |
|---|
| 461 | { |
|---|
| 462 | result = Date.fromString(str); |
|---|
| 463 | } |
|---|
| 464 | } |
|---|
| 465 | return result; |
|---|
| 466 | } |
|---|
| 467 | |
|---|
| 468 | /** |
|---|
| 469 | * Splits the strings contained within the supplied array into single values. |
|---|
| 470 | * Each String of <code>optionValues</code> might contain a comma separated |
|---|
| 471 | * value list, which is split by this. |
|---|
| 472 | * |
|---|
| 473 | * @param optionValues |
|---|
| 474 | * |
|---|
| 475 | * @return String array containing the split values. Might be null. |
|---|
| 476 | */ |
|---|
| 477 | private String [] parseOptionValues (final String [] optionValues) |
|---|
| 478 | { |
|---|
| 479 | String[] rc = null; |
|---|
| 480 | if (optionValues != null) |
|---|
| 481 | { |
|---|
| 482 | final List values = new ArrayList(); |
|---|
| 483 | for (int i = 0; i < optionValues.length; ++i) |
|---|
| 484 | { |
|---|
| 485 | final StringTokenizer tokens = new StringTokenizer( |
|---|
| 486 | optionValues[i], "\t ,"); |
|---|
| 487 | while (tokens.hasMoreTokens()) |
|---|
| 488 | { |
|---|
| 489 | values.add(tokens.nextToken()); |
|---|
| 490 | } |
|---|
| 491 | } |
|---|
| 492 | rc = (String[]) values.toArray(EMPTY_STRINGS); |
|---|
| 493 | } |
|---|
| 494 | return rc; |
|---|
| 495 | } |
|---|
| 496 | |
|---|
| 497 | /** |
|---|
| 498 | * |
|---|
| 499 | */ |
|---|
| 500 | private void initDisplayOptions () |
|---|
| 501 | { |
|---|
| 502 | mDisplayOptions = new DisplayOptions(); |
|---|
| 503 | if (mCommandLine.hasOption(THREADID_OPTION.getOpt())) |
|---|
| 504 | { |
|---|
| 505 | mDisplayOptions.displayThreadId(true); |
|---|
| 506 | } |
|---|
| 507 | if (mCommandLine.hasOption(TIME_OPTION.getOpt())) |
|---|
| 508 | { |
|---|
| 509 | mDisplayOptions.displayTimestamp(true); |
|---|
| 510 | } |
|---|
| 511 | if (mCommandLine.hasOption(DATE_OPTION.getOpt())) |
|---|
| 512 | { |
|---|
| 513 | mDisplayOptions.displayTimestamp(true); |
|---|
| 514 | } |
|---|
| 515 | if (mCommandLine.hasOption(IMPACT_OPTION.getOpt())) |
|---|
| 516 | { |
|---|
| 517 | mDisplayOptions.displayBusinessImpact(true); |
|---|
| 518 | } |
|---|
| 519 | if (mCommandLine.hasOption(CATEGORY_OPTION.getOpt())) |
|---|
| 520 | { |
|---|
| 521 | mDisplayOptions.displayCategory(true); |
|---|
| 522 | } |
|---|
| 523 | if (mCommandLine.hasOption(LEVEL_OPTION.getOpt())) |
|---|
| 524 | { |
|---|
| 525 | mDisplayOptions.displayLoggerLevel(true); |
|---|
| 526 | } |
|---|
| 527 | if (mCommandLine.hasOption(STACKTRACE_OPTION.getOpt())) |
|---|
| 528 | { |
|---|
| 529 | setStackTraceDetails(); |
|---|
| 530 | } |
|---|
| 531 | if (mCommandLine.hasOption(STANDARD_OPTION.getOpt())) |
|---|
| 532 | { |
|---|
| 533 | setStandardDisplay(mDisplayOptions); |
|---|
| 534 | } |
|---|
| 535 | } |
|---|
| 536 | |
|---|
| 537 | private void setStandardDisplay (final DisplayOptions dp) |
|---|
| 538 | { |
|---|
| 539 | dp.displayTimestamp(true); |
|---|
| 540 | dp.displayBusinessImpact(true); |
|---|
| 541 | dp.displayCategory(true); |
|---|
| 542 | dp.displayLoggerLevel(true); |
|---|
| 543 | dp.displayStackTrace(true); |
|---|
| 544 | dp.displayMessageStackTrace(false); |
|---|
| 545 | } |
|---|
| 546 | |
|---|
| 547 | private void setStackTraceDetails () |
|---|
| 548 | { |
|---|
| 549 | final String[] details = parseOptionValues( |
|---|
| 550 | mCommandLine.getOptionValues(STACKTRACE_OPTION.getOpt())); |
|---|
| 551 | if ((details != null) && (details.length > 0)) |
|---|
| 552 | { |
|---|
| 553 | final int level = Integer.parseInt(details[0]); |
|---|
| 554 | mDisplayOptions.displayMessageStackTrace( |
|---|
| 555 | level >= LEVEL_MESSAGE_STACKTRACE); |
|---|
| 556 | mDisplayOptions.displayStackTrace( |
|---|
| 557 | level >= LEVEL_EXCEPTION_STACKTRACE); |
|---|
| 558 | } |
|---|
| 559 | } |
|---|
| 560 | |
|---|
| 561 | private void setInput () |
|---|
| 562 | throws LoggingException |
|---|
| 563 | { |
|---|
| 564 | final String logDir = mCommandLine.getOptionValue( |
|---|
| 565 | LOGDIR_OPTION.getOpt(), DEFAULT_DIR); |
|---|
| 566 | |
|---|
| 567 | if ((logDir == null) || (logDir.length() == 0)) |
|---|
| 568 | { |
|---|
| 569 | throw new LoggingException("Undefined log directory."); |
|---|
| 570 | } |
|---|
| 571 | |
|---|
| 572 | final String fileName = logDir + File.separator |
|---|
| 573 | + mCommandLine.getOptionValue( |
|---|
| 574 | LOGFILE_OPTION.getOpt(), DEFAULT_FILE); |
|---|
| 575 | |
|---|
| 576 | final LogReader logReader; |
|---|
| 577 | try |
|---|
| 578 | { |
|---|
| 579 | logReader = new LogReader(fileName); |
|---|
| 580 | setFilters(logReader); |
|---|
| 581 | mLogReader = logReader; |
|---|
| 582 | } |
|---|
| 583 | catch (InstantiationException ex) |
|---|
| 584 | { |
|---|
| 585 | throw new LoggingException("Error instantiating a LogReader for file " |
|---|
| 586 | + fileName, ex); |
|---|
| 587 | } |
|---|
| 588 | } |
|---|
| 589 | |
|---|
| 590 | private void setOutput () |
|---|
| 591 | throws LoggingException |
|---|
| 592 | { |
|---|
| 593 | if (mCommandLine.hasOption(OUTFILE_OPTION.getOpt())) |
|---|
| 594 | { |
|---|
| 595 | final String fileName = mCommandLine.getOptionValue( |
|---|
| 596 | OUTFILE_OPTION.getOpt()); |
|---|
| 597 | try |
|---|
| 598 | { |
|---|
| 599 | final File file = new File(fileName); |
|---|
| 600 | mOut = new PrintWriter(new FileOutputStream(file)); |
|---|
| 601 | } |
|---|
| 602 | catch (FileNotFoundException ex) |
|---|
| 603 | { |
|---|
| 604 | throw new LoggingException( |
|---|
| 605 | "Could not open the output file " + fileName, ex); |
|---|
| 606 | } |
|---|
| 607 | } |
|---|
| 608 | else |
|---|
| 609 | { |
|---|
| 610 | mOut = new PrintWriter(System.out); |
|---|
| 611 | } |
|---|
| 612 | } |
|---|
| 613 | |
|---|
| 614 | /** |
|---|
| 615 | * Closes the log console. Closes the output stream. |
|---|
| 616 | */ |
|---|
| 617 | private void close () |
|---|
| 618 | { |
|---|
| 619 | if (mOut != null) |
|---|
| 620 | { |
|---|
| 621 | mOut.flush(); |
|---|
| 622 | mOut.close(); |
|---|
| 623 | } |
|---|
| 624 | if (mLogReader != null) |
|---|
| 625 | { |
|---|
| 626 | mLogReader.close(); |
|---|
| 627 | } |
|---|
| 628 | } |
|---|
| 629 | |
|---|
| 630 | /** |
|---|
| 631 | * The run method. |
|---|
| 632 | * Reads the log file and prints the log records until end of file is reached |
|---|
| 633 | * (batch mode) or until it is terminated. |
|---|
| 634 | * |
|---|
| 635 | * @see java.lang.Runnable#run() |
|---|
| 636 | */ |
|---|
| 637 | public void run () |
|---|
| 638 | { |
|---|
| 639 | try |
|---|
| 640 | { |
|---|
| 641 | if (mCommandLine.hasOption(BATCH_OPTION.getOpt())) |
|---|
| 642 | { |
|---|
| 643 | runBatchMode(); |
|---|
| 644 | } |
|---|
| 645 | else |
|---|
| 646 | { |
|---|
| 647 | runLiveMode(); |
|---|
| 648 | } |
|---|
| 649 | } |
|---|
| 650 | catch (Exception ex) |
|---|
| 651 | { |
|---|
| 652 | System.err.println("Caught exception while viewing log file: " + ex); |
|---|
| 653 | ex.printStackTrace(); |
|---|
| 654 | } |
|---|
| 655 | } |
|---|
| 656 | |
|---|
| 657 | /** |
|---|
| 658 | * Runs the live mode. |
|---|
| 659 | */ |
|---|
| 660 | private void runLiveMode () |
|---|
| 661 | { |
|---|
| 662 | while (true) |
|---|
| 663 | { |
|---|
| 664 | LogFileEntry logRecord = null; |
|---|
| 665 | |
|---|
| 666 | if (mLogReader.available()) |
|---|
| 667 | { |
|---|
| 668 | do |
|---|
| 669 | { |
|---|
| 670 | logRecord = mLogReader.readLogFileEntry(); |
|---|
| 671 | if ((logRecord != null) && (mDisplay != null)) |
|---|
| 672 | { |
|---|
| 673 | mDisplay.print(mOut, logRecord); |
|---|
| 674 | logRecord.release(); |
|---|
| 675 | } |
|---|
| 676 | } |
|---|
| 677 | while (logRecord != null); |
|---|
| 678 | mOut.flush(); |
|---|
| 679 | } |
|---|
| 680 | |
|---|
| 681 | try |
|---|
| 682 | { |
|---|
| 683 | Thread.sleep(LOGFILE_POLL_INTERVAL); |
|---|
| 684 | } |
|---|
| 685 | catch (InterruptedException e) |
|---|
| 686 | { |
|---|
| 687 | // ignore |
|---|
| 688 | } |
|---|
| 689 | } |
|---|
| 690 | } |
|---|
| 691 | |
|---|
| 692 | /** |
|---|
| 693 | * Runs the batch mode. |
|---|
| 694 | */ |
|---|
| 695 | private void runBatchMode () |
|---|
| 696 | { |
|---|
| 697 | LogFileEntry logRecord = null; |
|---|
| 698 | do |
|---|
| 699 | { |
|---|
| 700 | logRecord = mLogReader.readLogFileEntry(); |
|---|
| 701 | if ((logRecord != null) && (mDisplay != null)) |
|---|
| 702 | { |
|---|
| 703 | mDisplay.print(mOut, logRecord); |
|---|
| 704 | logRecord.release(); |
|---|
| 705 | } |
|---|
| 706 | } |
|---|
| 707 | while (logRecord != null); |
|---|
| 708 | |
|---|
| 709 | mOut.flush(); |
|---|
| 710 | } |
|---|
| 711 | |
|---|
| 712 | private void installOptions () |
|---|
| 713 | { |
|---|
| 714 | mOptions = new Options(); |
|---|
| 715 | |
|---|
| 716 | mOptions.addOption(LOGFILE_OPTION); |
|---|
| 717 | mOptions.addOption(LOGDIR_OPTION); |
|---|
| 718 | // mOptions.addOption(LINES_OPTION); |
|---|
| 719 | mOptions.addOption(BATCH_OPTION); |
|---|
| 720 | mOptions.addOption(OUTFILE_OPTION); |
|---|
| 721 | mOptions.addOption(XML_OPTION); |
|---|
| 722 | mOptions.addOption(STACKTRACE_OPTION); |
|---|
| 723 | mOptions.addOption(DATE_OPTION); |
|---|
| 724 | mOptions.addOption(TIME_OPTION); |
|---|
| 725 | mOptions.addOption(THREADID_OPTION); |
|---|
| 726 | mOptions.addOption(IMPACT_OPTION); |
|---|
| 727 | mOptions.addOption(CATEGORY_OPTION); |
|---|
| 728 | mOptions.addOption(LEVEL_OPTION); |
|---|
| 729 | mOptions.addOption(STANDARD_OPTION); |
|---|
| 730 | } |
|---|
| 731 | |
|---|
| 732 | private void readCommandLineArgs (final String[] args) |
|---|
| 733 | throws ParseException |
|---|
| 734 | { |
|---|
| 735 | mCommandLine = new GnuParser().parse(mOptions, args, true); |
|---|
| 736 | } |
|---|
| 737 | |
|---|
| 738 | private void installDisplay () |
|---|
| 739 | throws LoggingException |
|---|
| 740 | { |
|---|
| 741 | if (mCommandLine.hasOption(XML_OPTION.getOpt())) |
|---|
| 742 | { |
|---|
| 743 | try |
|---|
| 744 | { |
|---|
| 745 | mDisplay = new XmlPrinter(); |
|---|
| 746 | } |
|---|
| 747 | catch (InstantiationException ex) |
|---|
| 748 | { |
|---|
| 749 | throw new LoggingException("Error instantiating an XmlPrinter", ex); |
|---|
| 750 | } |
|---|
| 751 | } |
|---|
| 752 | else |
|---|
| 753 | { |
|---|
| 754 | mDisplay = new BasicPrinter(); |
|---|
| 755 | } |
|---|
| 756 | mDisplay.setDisplayOptions(mDisplayOptions); |
|---|
| 757 | } |
|---|
| 758 | |
|---|
| 759 | /** |
|---|
| 760 | * Sets the filters for the supplied LogReader as specified in the command |
|---|
| 761 | * line options. |
|---|
| 762 | * |
|---|
| 763 | * @param logReader The LogReader for which to set the filters |
|---|
| 764 | * |
|---|
| 765 | * TODO Implement Me |
|---|
| 766 | */ |
|---|
| 767 | private void setFilters (final LogReader logReader) |
|---|
| 768 | { |
|---|
| 769 | for (final Iterator iter = mFilters.iterator(); iter.hasNext(); ) |
|---|
| 770 | { |
|---|
| 771 | logReader.addFilter((Filter) iter.next()); |
|---|
| 772 | } |
|---|
| 773 | } |
|---|
| 774 | |
|---|
| 775 | private void printUsage () |
|---|
| 776 | { |
|---|
| 777 | new HelpFormatter().printHelp( |
|---|
| 778 | "java org.jcoderz.commons.LogViewer", mOptions); |
|---|
| 779 | } |
|---|
| 780 | } |
|---|