001/**
002 * Logback: the reliable, generic, fast and flexible logging framework. Copyright (C) 1999-2015, QOS.ch. All rights
003 * reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under either the terms of the Eclipse Public License
006 * v1.0 as published by the Eclipse Foundation
007 *
008 * or (per the licensee's choosing)
009 *
010 * under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation.
011 */
012package ch.qos.logback.core.rolling;
013
014import java.io.File;
015import java.time.Instant;
016import java.util.Date;
017
018import ch.qos.logback.core.joran.spi.NoAutoStart;
019import ch.qos.logback.core.rolling.helper.TimeBasedArchiveRemover;
020
021/**
022 * Default implementation of {@link TimeBasedFileNamingAndTriggeringPolicy}
023 * interface extending {@link TimeBasedFileNamingAndTriggeringPolicyBase}. This class is intended to be nested
024 * within a {@link TimeBasedRollingPolicy}.
025 *
026 *
027 * @author Ceki Gülcü
028 *
029 * @param <E>
030 */
031@NoAutoStart
032public class DefaultTimeBasedFileNamingAndTriggeringPolicy<E> extends TimeBasedFileNamingAndTriggeringPolicyBase<E> {
033
034    @Override
035    public void start() {
036        super.start();
037        if (!super.isErrorFree())
038            return;
039        if (tbrp.fileNamePattern.hasIntegerTokenCOnverter()) {
040            addError("Filename pattern [" + tbrp.fileNamePattern
041                    + "] contains an integer token converter, i.e. %i, INCOMPATIBLE with this configuration. Remove it.");
042            return;
043        }
044
045        archiveRemover = new TimeBasedArchiveRemover(tbrp.fileNamePattern, rc);
046        archiveRemover.setContext(context);
047        started = true;
048    }
049
050    public boolean isTriggeringEvent(File activeFile, final E event) {
051        long currentTime = getCurrentTime();
052        long localNextCheck = atomicNextCheck.get();
053        if (currentTime >= localNextCheck) {
054            long nextCheck = computeNextCheck(currentTime);
055            atomicNextCheck.set(nextCheck);
056            Instant instantOfElapsedPeriod = dateInCurrentPeriod;
057            addInfo("Elapsed period: " + instantOfElapsedPeriod.toString());
058            this.elapsedPeriodsFileName = tbrp.fileNamePatternWithoutCompSuffix.convert(instantOfElapsedPeriod);
059            setDateInCurrentPeriod(currentTime);
060            return true;
061        } else {
062            return false;
063        }
064    }
065
066    @Override
067    public String toString() {
068        return "c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy";
069    }
070
071}