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