001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2006-2009 Sun Microsystems, Inc.
025 *      Portions Copyright 2013-2015 ForgeRock AS
026 */
027package org.opends.server.loggers;
028
029import java.io.PrintStream;
030import java.text.DateFormat;
031import java.text.SimpleDateFormat;
032
033import org.opends.server.admin.std.server.DebugLogPublisherCfg;
034import org.forgerock.opendj.config.server.ConfigException;
035import org.opends.server.core.ServerContext;
036import org.opends.server.types.DN;
037import org.opends.server.types.InitializationException;
038import org.opends.server.util.ServerConstants;
039
040/**
041 * The debug log publisher implementation that writes debug messages in a
042 * friendly for console output.
043 */
044public class ConsoleDebugLogPublisher extends
045    DebugLogPublisher<DebugLogPublisherCfg>
046{
047  /**
048   * The print stream where tracing will be sent.
049   */
050  private PrintStream err;
051
052  /**
053   * The format used for trace timestamps.
054   */
055  private DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss.SSS");
056
057  /**
058   * Constructs a new ConsoleDebugLogPublisher that writes debug messages
059   * to the given PrintStream.
060   * @param err The PrintStream to write messages to.
061   */
062  public ConsoleDebugLogPublisher(PrintStream err)
063  {
064    this.err = err;
065  }
066
067  /** {@inheritDoc} */
068  @Override
069  public void initializeLogPublisher(DebugLogPublisherCfg config, ServerContext serverContext)
070      throws ConfigException, InitializationException {
071    // This publisher is not configurable.
072  }
073
074  /** {@inheritDoc} */
075  @Override
076  public void trace(TraceSettings settings,
077                           String signature,
078                           String sourceLocation,
079                           String msg,
080                           StackTraceElement[] stackTrace)
081  {
082    String stack = null;
083    if(stackTrace != null)
084    {
085      stack = DebugStackTraceFormatter.formatStackTrace(stackTrace, settings.getStackDepth());
086    }
087    publish(msg, stack);
088  }
089
090  /** {@inheritDoc} */
091  @Override
092  public void traceException(TraceSettings settings,
093                          String signature,
094                          String sourceLocation,
095                          String msg,
096                          Throwable ex, StackTraceElement[] stackTrace)
097  {
098    String message = DebugMessageFormatter.format("%s caught={%s} %s(): %s",
099        new Object[] { msg, ex, signature, sourceLocation });
100
101    String stack = null;
102    if (stackTrace != null)
103    {
104      stack =
105          DebugStackTraceFormatter.formatStackTrace(ex, settings
106              .getStackDepth(), settings.isIncludeCause());
107    }
108    publish(message, stack);
109  }
110
111  /** {@inheritDoc} */
112  @Override
113  public void close()
114  {
115    // Nothing to do.
116  }
117
118
119  /**
120   * Publishes a record, optionally performing some "special" work:
121   * - injecting a stack trace into the message
122   */
123  private void publish(String msg, String stack)
124  {
125    StringBuilder buf = new StringBuilder();
126    // Emit the timestamp.
127    buf.append(dateFormat.format(System.currentTimeMillis()));
128    buf.append(" ");
129
130    // Emit the debug level.
131    buf.append("trace ");
132
133    // Emit message.
134    buf.append(msg);
135    buf.append(ServerConstants.EOL);
136
137    // Emit Stack Trace.
138    if(stack != null)
139    {
140      buf.append("\nStack Trace:\n");
141      buf.append(stack);
142    }
143
144    err.print(buf);
145  }
146
147  /** {@inheritDoc} */
148  @Override
149  public DN getDN()
150  {
151    // There is no configuration DN associated with this publisher.
152    return DN.NULL_DN;
153  }
154
155}