001/* 
002 * Copyright 2015 Alexander Nozik.
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 */
016package hep.dataforge.io.log;
017
018import hep.dataforge.io.log.Logable.LogTag;
019import static java.lang.String.format;
020import java.time.LocalDateTime;
021import java.time.format.DateTimeFormatter;
022import java.util.ArrayList;
023import java.util.List;
024
025/**
026 * <p>
027 * LogEntry class.</p>
028 *
029 * @author Alexander Nozik
030 * @version $Id: $Id
031 */
032public class LogEntry {
033
034    protected DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm:ss.SSS");
035    private final List<String> trace = new ArrayList<>();
036    private final String message;
037    private final LogTag tag;
038    private final LocalDateTime time;
039
040    private LogEntry(LogEntry entry, String traceAdd) {
041        this.trace.addAll(entry.trace);
042        if (traceAdd != null && !traceAdd.isEmpty()) {
043            this.trace.add(0, traceAdd);
044        }
045        this.message = entry.message;
046        this.tag = entry.tag;
047        this.time = entry.time;
048    }
049
050    /**
051     * <p>Getter for the field <code>tag</code>.</p>
052     *
053     * @return 
054     */
055    public LogTag getTag() {
056        return tag;
057    }
058
059    /**
060     * <p>
061     * Constructor for LogEntry.</p>
062     *
063     * @param time 
064     * @param message 
065     * @param tag
066     */
067    public LogEntry(LocalDateTime time, String message, LogTag tag) {
068        this.time = time;
069        this.message = message;
070        this.tag = tag;
071    }
072
073    /**
074     * <p>Constructor for LogEntry.</p>
075     *
076     * @param message 
077     * @param tag
078     */
079    public LogEntry(String message, LogTag tag) {
080        this.time = LocalDateTime.now();
081        this.message = message;
082        this.tag = tag;
083    }
084
085    /**
086     * <p>
087     * Constructor for LogEntry.</p>
088     *
089     * @param time a {@link java.util.Date} object.
090     * @param message a {@link java.lang.String} object.
091     */
092    public LogEntry(LocalDateTime time, String message) {
093        this.time = time;
094        this.message = message;
095        this.tag = LogTag.INFO;
096    }
097
098    /**
099     * <p>
100     * Constructor for LogEntry.</p>
101     *
102     * @param message a {@link java.lang.String} object.
103     */
104    public LogEntry(String message) {
105        this.time = LocalDateTime.now();
106        this.message = message;
107        this.tag = LogTag.INFO;
108    }
109
110    /**
111     * <p>
112     * Getter for the field <code>message</code>.</p>
113     *
114     * @return the message
115     */
116    public String getMessage() {
117        return message;
118    }
119
120    /**
121     * <p>
122     * Getter for the field <code>time</code>.</p>
123     *
124     * @return the time
125     */
126    public LocalDateTime getTime() {
127        return time;
128    }
129
130    /**
131     * <p>pushTrace.</p>
132     *
133     * @param toTrace a {@link java.lang.String} object.
134     * @return a {@link hep.dataforge.io.log.LogEntry} object.
135     */
136    public LogEntry pushTrace(String toTrace) {
137        return new LogEntry(this, toTrace);
138    }
139
140    /** {@inheritDoc}
141     * @return  */
142    @Override
143    public String toString() {
144        String traceStr = String.join(".", trace);
145        if (tag.equals(LogTag.INFO)) {
146            if (traceStr.isEmpty()) {
147                return format("(%s) %s", dateFormat.format(time), message);
148            } else {
149                return format("(%s) %s: %s", dateFormat.format(time), traceStr, message);
150            }
151        } else {
152            if (traceStr.isEmpty()) {
153                return format("(%s) [%s] %s", dateFormat.format(time), tag.name(), message);
154            } else {
155                return format("(%s) [%s] %s: %s", dateFormat.format(time), tag.name(), traceStr, message);
156            }
157        }
158    }
159
160}