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.IOException;
021import java.io.Writer;
022
023import org.apache.logging.log4j.Level;
024import org.apache.logging.log4j.Marker;
025import org.apache.logging.log4j.io.CharStreamLogger;
026import org.apache.logging.log4j.spi.ExtendedLogger;
027
028/**
029 * Internal class that exists primiarly to allow location calculations to work.
030 * @since 2.12
031 */
032public class InternalWriter extends Writer {
033
034    private final CharStreamLogger logger;
035    private final String fqcn;
036
037    public InternalWriter(final ExtendedLogger logger, final String fqcn, final Level level, final Marker marker) {
038        this.logger = new CharStreamLogger(logger, level, marker);
039        this.fqcn = fqcn;
040    }
041
042    @Override
043    public void close() throws IOException {
044        this.logger.close(this.fqcn);
045    }
046
047    @Override
048    public void flush() throws IOException {
049        // do nothing
050    }
051
052    @Override
053    public String toString() {
054        return this.getClass().getSimpleName() + "[fqcn=" + this.fqcn + ", logger=" + this.logger + "]";
055    }
056
057    @Override
058    public void write(final char[] cbuf) throws IOException {
059        this.logger.put(this.fqcn, cbuf, 0, cbuf.length);
060    }
061
062    @Override
063    public void write(final char[] cbuf, final int off, final int len) throws IOException {
064        this.logger.put(this.fqcn, cbuf, off, len);
065    }
066
067    @Override
068    public void write(final int c) throws IOException {
069        this.logger.put(this.fqcn, (char) c);
070    }
071
072    @Override
073    public void write(final String str) throws IOException {
074        this.logger.put(this.fqcn, str, 0, str.length());
075    }
076
077    @Override
078    public void write(final String str, final int off, final int len) throws IOException {
079        this.logger.put(this.fqcn, str, off, len);
080    }
081}