// Path: modules/core/src/main/java/de/tobifleig/lxc/util/ByteLimitOutputStream.java
// public class ByteLimitOutputStream extends FilterOutputStream {
// 
//     private int bytesLeft;
//     private Runnable limitReachedAction;
// 
//     public ByteLimitOutputStream(OutputStream out, int byteLimit, Runnable limitReachedAction) {
//         super(out);
//         this.bytesLeft = byteLimit;
//         this.limitReachedAction = limitReachedAction;
//     }
// 
//     @Override
//     public void write(int b) throws IOException {
//         if (bytesLeft <= 0) {
//             limitReachedAction.run();
//             return;
//         }
//         super.write(b);
//         bytesLeft -= 1;
//     }
// 
//     @Override
//     public void write(byte[] b, int off, int len) throws IOException {
//         if (bytesLeft < len) {
//             bytesLeft = 0; // prevent retry with smaller buffer
//             limitReachedAction.run();
//             return;
//         }
//         int leftPreSuper = bytesLeft;
//         super.write(b, off, len);
//         // subclass may call write(int) multiple times, avoid subtracting the limit multiple times
//         if (bytesLeft != leftPreSuper - len) {
//             bytesLeft -= len;
//         }
//     }
// }


import de.tobifleig.lxc.util.ByteLimitOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
                File rotateVictim = new File(targetDir,"lxc_oldlog" + (i + 1) + ".log");
                if (rotateDown.exists()) {
                    if (rotateVictim.exists()) {
                        rotateVictim.delete();
                    }
                    rotateDown.renameTo(rotateVictim);
                }
            }
            File previous = new File(targetDir,"lxc_oldlog1.log");
            if (previous.exists()) {
                previous.delete();
            }
            current.renameTo(previous);
        }
        if (!current.exists()) {
            try {
                current.createNewFile();
            } catch (IOException ex) {
                System.err.println("Cannot write to logfile \"lxc.log\"");
            }
        }
        if (!current.canWrite()) {
            System.err.println("Cannot write to logfile \"lxc.log\"");
            return null;
        }
        return current;
    }

    private static PrintStream createSizeLimitPrinter(File logFile) throws FileNotFoundException {
        final FileOutputStream fileOut = new FileOutputStream(logFile);
// Path: modules/core/src/main/java/de/tobifleig/lxc/util/ByteLimitOutputStream.java
// public class ByteLimitOutputStream extends FilterOutputStream {
// 
//     private int bytesLeft;
//     private Runnable limitReachedAction;
// 
//     public ByteLimitOutputStream(OutputStream out, int byteLimit, Runnable limitReachedAction) {
//         super(out);
//         this.bytesLeft = byteLimit;
//         this.limitReachedAction = limitReachedAction;
//     }
// 
//     @Override
//     public void write(int b) throws IOException {
//         if (bytesLeft <= 0) {
//             limitReachedAction.run();
//             return;
//         }
//         super.write(b);
//         bytesLeft -= 1;
//     }
// 
//     @Override
//     public void write(byte[] b, int off, int len) throws IOException {
//         if (bytesLeft < len) {
//             bytesLeft = 0; // prevent retry with smaller buffer
//             limitReachedAction.run();
//             return;
//         }
//         int leftPreSuper = bytesLeft;
//         super.write(b, off, len);
//         // subclass may call write(int) multiple times, avoid subtracting the limit multiple times
//         if (bytesLeft != leftPreSuper - len) {
//             bytesLeft -= len;
//         }
//     }
// }


// Path: modules/core/src/main/java/de/tobifleig/lxc/log/LXCLogBackend.java
import de.tobifleig.lxc.util.ByteLimitOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;

                File rotateVictim = new File(targetDir,"lxc_oldlog" + (i + 1) + ".log");
                if (rotateDown.exists()) {
                    if (rotateVictim.exists()) {
                        rotateVictim.delete();
                    }
                    rotateDown.renameTo(rotateVictim);
                }
            }
            File previous = new File(targetDir,"lxc_oldlog1.log");
            if (previous.exists()) {
                previous.delete();
            }
            current.renameTo(previous);
        }
        if (!current.exists()) {
            try {
                current.createNewFile();
            } catch (IOException ex) {
                System.err.println("Cannot write to logfile \"lxc.log\"");
            }
        }
        if (!current.canWrite()) {
            System.err.println("Cannot write to logfile \"lxc.log\"");
            return null;
        }
        return current;
    }

    private static PrintStream createSizeLimitPrinter(File logFile) throws FileNotFoundException {
        final FileOutputStream fileOut = new FileOutputStream(logFile);
        return new PrintStream(new ByteLimitOutputStream(fileOut, byteLimit, new Runnable() {