01: /*
02: * Copyright (C) The Apache Software Foundation. All rights reserved.
03: *
04: * This software is published under the terms of the Apache Software License
05: * version 1.1, a copy of which has been included with this distribution in
06: * the LICENSE file.
07: */
08: package org.jivesoftware.util.log.output.io.rotate;
09:
10: import java.io.File;
11:
12: /**
13: * Rotation stragety based on size written to log file.
14: *
15: * @author <a href="mailto:bh22351@i-one.at">Bernhard Huber</a>
16: */
17: public class RotateStrategyBySize implements RotateStrategy {
18: private long m_maxSize;
19: private long m_currentSize;
20:
21: /**
22: * Rotate logs by size.
23: * By default do log rotation after writing approx. 1MB of messages
24: */
25: public RotateStrategyBySize() {
26: this (1024 * 1024);
27: }
28:
29: /**
30: * Rotate logs by size.
31: *
32: * @param maxSize rotate after writing max_size [byte] of messages
33: */
34: public RotateStrategyBySize(final long maxSize) {
35: m_currentSize = 0;
36: m_maxSize = maxSize;
37: }
38:
39: /**
40: * reset log size written so far.
41: */
42: public void reset() {
43: m_currentSize = 0;
44: }
45:
46: /**
47: * Check if now a log rotation is neccessary.
48: *
49: * @param data the last message written to the log system
50: * @return boolean return true if log rotation is neccessary, else false
51: */
52: public boolean isRotationNeeded(final String data, final File file) {
53: m_currentSize += data.length();
54: if (m_currentSize >= m_maxSize) {
55: m_currentSize = 0;
56: return true;
57: } else {
58: return false;
59: }
60: }
61: }
|