001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.logging.log4j.io.internal;
019
020import java.io.FilterWriter;
021import java.io.IOException;
022import java.io.Writer;
023
024import org.apache.logging.log4j.Level;
025import org.apache.logging.log4j.Marker;
026import org.apache.logging.log4j.io.CharStreamLogger;
027import org.apache.logging.log4j.spi.ExtendedLogger;
028
029/**
030 * Internal class that exists primarily to allow location calculation to work.
031 *
032 * @since 2.12
033 */
034public class InternalFilterWriter extends FilterWriter {
035
036    private final CharStreamLogger logger;
037    private final String fqcn;
038
039    public InternalFilterWriter(final Writer out, final ExtendedLogger logger, final String fqcn, final Level level,
040                                 final Marker marker) {
041        super(out);
042        this.logger = new CharStreamLogger(logger, level, marker);
043        this.fqcn = fqcn;
044    }
045
046    @Override
047    public void close() throws IOException {
048        this.out.close();
049        this.logger.close(this.fqcn);
050    }
051
052    @Override
053    public void flush() throws IOException {
054        this.out.flush();
055    }
056
057    @Override
058    public String toString() {
059        return "{writer=" + this.out + '}';
060    }
061
062    @Override
063    public void write(final char[] cbuf) throws IOException {
064        this.out.write(cbuf);
065        this.logger.put(this.fqcn, cbuf, 0, cbuf.length);
066    }
067
068    @Override
069    public void write(final char[] cbuf, final int off, final int len) throws IOException {
070        this.out.write(cbuf, off, len);
071        this.logger.put(this.fqcn, cbuf, off, len);
072    }
073
074    @Override
075    public void write(final int c) throws IOException {
076        this.out.write(c);
077        this.logger.put(this.fqcn, (char) c);
078    }
079
080    @Override
081    public void write(final String str) throws IOException {
082        this.out.write(str);
083        this.logger.put(this.fqcn, str, 0, str.length());
084    }
085
086    @Override
087    public void write(final String str, final int off, final int len) throws IOException {
088        this.out.write(str, off, len);
089        this.logger.put(this.fqcn, str, off, len);
090    }
091}