1 /*
2 * ------------------------------------------------------------------------------
3 * Hermes FTP Server
4 * Copyright (c) 2005-2007 Lars Behnke
5 * ------------------------------------------------------------------------------
6 *
7 * This file is part of Hermes FTP Server.
8 *
9 * Hermes FTP Server is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * Hermes FTP Server is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with Hermes FTP Server; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 * ------------------------------------------------------------------------------
23 */
24
25 package net.sf.hermesftp.utils;
26
27 import java.io.OutputStream;
28 import java.io.PrintWriter;
29 import java.io.Writer;
30
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33
34 /***
35 * Helper class that logs server responses.
36 *
37 * @author Lars Behnke
38 */
39 public class LoggingWriter
40 extends PrintWriter {
41
42 private static final int LOG_LINE_LENGTH = 80;
43
44 private static Log log = LogFactory.getLog(LoggingWriter.class);
45
46 /***
47 * Constructor.
48 *
49 * @param out The output stream.
50 * @param flush Automatic flush
51 */
52 public LoggingWriter(OutputStream out, boolean flush) {
53 super(out, flush);
54 }
55
56 /***
57 * Constructor.
58 *
59 * @param out The output writer.
60 * @param flush Automatic flush
61 */
62 public LoggingWriter(Writer out, boolean flush) {
63 super(out, flush);
64 }
65
66 /***
67 * {@inheritDoc}
68 */
69 public void println(String text) {
70 if (log.isDebugEnabled()) {
71 String x;
72 if (text != null && text.length() >= LOG_LINE_LENGTH) {
73 x = text.substring(0, LOG_LINE_LENGTH) + " [" + (text.length() - LOG_LINE_LENGTH)
74 + " more chars]";
75 } else {
76 x = text;
77 }
78 log.debug("-->: " + x);
79 }
80 super.println(text);
81 // if (checkError()) {
82 // log.debug("Writing to control stream failed.");
83 // }
84 }
85 }