001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.rolling;
015
016import ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP.Usage;
017import ch.qos.logback.core.util.FileSize;
018
019public class SizeAndTimeBasedRollingPolicy<E> extends TimeBasedRollingPolicy<E> {
020
021    FileSize maxFileSize;
022
023    @Override
024    public void start() {
025        SizeAndTimeBasedFNATP<E> sizeAndTimeBasedFNATP = new SizeAndTimeBasedFNATP<E>(Usage.EMBEDDED);
026        if (maxFileSize == null) {
027            addError("maxFileSize property is mandatory.");
028            return;
029        } else {
030            addInfo("Archive files will be limited to [" + maxFileSize + "] each.");
031        }
032
033        sizeAndTimeBasedFNATP.setMaxFileSize(maxFileSize);
034        timeBasedFileNamingAndTriggeringPolicy = sizeAndTimeBasedFNATP;
035
036        if (!isUnboundedTotalSizeCap() && totalSizeCap.getSize() < maxFileSize.getSize()) {
037            addError("totalSizeCap of [" + totalSizeCap + "] is smaller than maxFileSize [" + maxFileSize
038                    + "] which is non-sensical");
039            return;
040        }
041
042        // most work is done by the parent
043        super.start();
044    }
045
046    public void setMaxFileSize(FileSize aMaxFileSize) {
047        this.maxFileSize = aMaxFileSize;
048    }
049
050    @Override
051    public String toString() {
052        return "c.q.l.core.rolling.SizeAndTimeBasedRollingPolicy@" + this.hashCode();
053    }
054}