001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *      Delbosc Benoit
018 */
019package org.nuxeo.common.logging;
020
021import org.apache.commons.logging.Log;
022import org.apache.commons.logging.LogFactory;
023
024/**
025 * Helper to log information that can be displayed using plantuml to render sequence UML diagram.
026 *
027 * @since 8.1
028 * @deprecated since 11.1 use {@link io.opencensus.trace.Tracing} instead.
029 */
030@Deprecated(since = "11.1")
031public class SequenceTracer {
032
033    private static final Log log = LogFactory.getLog(SequenceTracer.class);
034    private static final String PREFIX = "@@ ";
035    private static final String DEFAULT_COLOR = "#white";
036    private static final int MESSAGE_MAX_LEN = 250;
037
038    // Utility class.
039    private SequenceTracer() {
040    }
041
042    /**
043     * Mark an event.
044     */
045    @Deprecated(since = "11.1")
046    public static void mark(String message) {
047        if (!log.isDebugEnabled()) {
048            return;
049        }
050        final String tn = getThreadName();
051        log.debug(PREFIX + tn + " -> " + tn + ": " + message);
052    }
053
054    /**
055     * Mark the beginning of an action
056     */
057    @Deprecated(since = "11.1")
058    public static void start(String message) {
059        start(message, DEFAULT_COLOR);
060    }
061
062    /**
063     * Mark the beginning of an action
064     */
065    @Deprecated(since = "11.1")
066    public static void start(String message, String color) {
067        if (!log.isDebugEnabled()) {
068            return;
069        }
070        final String tn = getThreadName();
071        log.debug(PREFIX + tn + " -> " + tn + ": " + format(message) + "\n" + PREFIX + "activate " + tn + " " +
072                color);
073    }
074
075    /**
076     * Mark the beginning of an action initiated by the caller.
077     */
078    @Deprecated(since = "11.1")
079    public static void startFrom(final String callerThread, final String message) {
080        startFrom(callerThread, message, DEFAULT_COLOR);
081    }
082
083    /**
084     * Mark the beginning of an action initiated by the caller.
085     */
086    @Deprecated(since = "11.1")
087    public static void startFrom(final String callerThread, final String message, final String color) {
088        if (!log.isDebugEnabled()) {
089            return;
090        }
091        final String tn = getThreadName();
092        log.debug(PREFIX + sanitize(callerThread) + " o--> " + tn + ": Initiate\n" + PREFIX
093                + tn + " -> " + tn + ": " + format(message) + "\n" + PREFIX
094                + "activate " + tn + " " + color);
095    }
096
097    private static String format(String message) {
098        String ret = sanitize(message).replace(", ", ",\\n");
099        ret = insertNewLine(ret);
100        return ret;
101    }
102
103    private static String sanitize(String message) {
104        String ret = message.replace("-", "_").replace(":", "_");
105        if (ret.length() > MESSAGE_MAX_LEN) {
106            ret = ret.substring(0, MESSAGE_MAX_LEN) + "...";
107        }
108        return ret;
109    }
110
111    private static String insertNewLine(String message) {
112        return String.join("\\n", message.split("(?<=\\G.{40})"));
113    }
114
115    /**
116     * Mark the end of the previous action.
117     */
118    @Deprecated(since = "11.1")
119    public static void stop(String message) {
120        if (!log.isDebugEnabled()) {
121            return;
122        }
123        final String tn = getThreadName();
124        log.debug(PREFIX + tn + " -> " + tn + ": " + format(message) + "\n" + PREFIX + "deactivate " + tn);
125    }
126
127    /**
128     * Mark the last action as failure
129     */
130    @Deprecated(since = "11.1")
131    public static void destroy(String message) {
132        if (!log.isDebugEnabled()) {
133            return;
134        }
135        final String tn = getThreadName();
136        log.debug(PREFIX + tn + " -> " + tn + ": " + format(message) + "\n" + PREFIX + "destroy " + tn);
137    }
138
139    /**
140     * Add a note on the current thread
141     */
142    @Deprecated(since = "11.1")
143    public static void addNote(String message) {
144        if (!log.isDebugEnabled()) {
145            return;
146        }
147        log.debug(PREFIX + "note right of " + getThreadName() + ": " + message);
148    }
149
150    /**
151     * Link from source to current thread.
152     */
153    @Deprecated(since = "11.1")
154    public static void addRelation(String source, String message) {
155        if (!log.isDebugEnabled()) {
156            return;
157        }
158        log.debug(PREFIX + source + " --> " + getThreadName() + ": " + format(message));
159    }
160
161    /**
162     * Get the thread name sanitized for plantuml
163     */
164    @Deprecated(since = "11.1")
165    public static String getThreadName() {
166        return sanitize(Thread.currentThread().getName());
167    }
168
169    @Deprecated(since = "11.1")
170    public static boolean isEnabled() {
171        return log.isDebugEnabled();
172    }
173}