com.sta.mlogger.MLogEntry4Text Maven / Gradle / Ivy
package com.sta.mlogger;
import java.io.PrintWriter;
import java.util.StringTokenizer;
/**
* Name: MLogEntry4Text
* Description: Logger-Eintrag f?r formatierte Textausgabe in einen
* PrintWriter. Diese Klasse ist praktisch das einfachste Referenzbeispiel f?r
* MLogEntry.
*
* Copyright: Copyright (c) 2002-2004, 2009, 2017-2019, 2021
* Company: >StA-Soft<
* @author StA
* @version 1.1
*/
public class MLogEntry4Text extends MLogEntry
{
/**
* Der zu verwendende PrintWriter.
*/
private PrintWriter myPrintWriter;
//===========================================================================
/**
* Initialisierung eines MLogEntry4Text.
* @param pMinLevel minimaler LogLevel
* @param pMaxLevel maximaler LogLevel
* @param pPrintWriter zu verwendender PrintWriter
* @see #MLogEntry4Text(int, PrintWriter)
* @see #MLogEntry4Text(PrintWriter)
* @see #MLogEntry4Text()
*/
public MLogEntry4Text(int pMinLevel, int pMaxLevel, PrintWriter pPrintWriter)
{
super(pMinLevel, pMaxLevel);
myPrintWriter = pPrintWriter;
}
/**
* Initialisierung eines MLogEntry4Text.
* @param pMinLevel minimaler LogLevel
* @param pMaxLevel maximaler LogLevel
* @see #MLogEntry4Text(int, PrintWriter)
* @see #MLogEntry4Text(PrintWriter)
* @see #MLogEntry4Text()
*/
public MLogEntry4Text(int pMinLevel, int pMaxLevel)
{
this(pMinLevel, pMaxLevel, new PrintWriter(System.out));
}
/**
* Initialisierung eines MLogEntry4Text mit MinLevel = ERROR.
* @param pMaxLevel maximaler LogLevel
* @param pPrintWriter zu verwendender PrintWriter
* @see #MLogEntry4Text(int, int, PrintWriter)
* @see #MLogEntry4Text(PrintWriter)
* @see #MLogEntry4Text()
*/
public MLogEntry4Text(int pMaxLevel, PrintWriter pPrintWriter)
{
this(ERROR, pMaxLevel, pPrintWriter);
}
/**
* Initialisierung eines MLogEntry4Text mit MinLevel = ERROR.
* @param pMaxLevel maximaler LogLevel
* @see #MLogEntry4Text(int, int, PrintWriter)
* @see #MLogEntry4Text(PrintWriter)
* @see #MLogEntry4Text()
*/
public MLogEntry4Text(int pMaxLevel)
{
this(ERROR, pMaxLevel);
}
/**
* Initialisierung eines MLogEntry4Text mit MinLevel = ERROR und MaxLevel =
* ALL.
* @param pPrintWriter zu verwendender PrintWriter
* @see #MLogEntry4Text(int, int, PrintWriter)
* @see #MLogEntry4Text(int, PrintWriter)
* @see #MLogEntry4Text()
*/
public MLogEntry4Text(PrintWriter pPrintWriter)
{
this(ERROR, ALL, pPrintWriter);
}
/**
* Initialisierung eines MLogEntry4Text mit MinLevel = ERROR, MaxLevel = ALL
* und PrintWriter = new PrintWriter(System.out).
* @see #MLogEntry4Text(int, int, PrintWriter)
* @see #MLogEntry4Text(int, PrintWriter)
* @see #MLogEntry4Text(PrintWriter)
*/
public MLogEntry4Text()
{
this(ERROR, ALL);
}
//===========================================================================
/**
* Zu verwendenden PrintWriter vorgeben.
* @param pPrintWriter zu verwendender PrintWriter
*/
public void setPrintWriter(PrintWriter pPrintWriter)
{
myPrintWriter = pPrintWriter;
}
/**
* Aktuell verwendeten PrintWriter ermitteln.
* @return aktuell verwendeter PrintWriter
*/
public PrintWriter getPrintWriter()
{
return myPrintWriter;
}
//---------------------------------------------------------------------------
@Override
protected void println(String pStrg)
{
if ((pStrg != null) && (pStrg.contains("\n") || pStrg.contains("\r")))
{
StringTokenizer st = new StringTokenizer(pStrg, "\n\r");
while (st.hasMoreTokens())
{
String s = st.nextToken();
myPrintWriter.println(s);
}
}
else
{
myPrintWriter.println(pStrg);
}
myPrintWriter.flush();
}
@Override
protected void print(String pStrg)
{
/*
if ((pStrg != null) && (pStrg.contains("\n") || pStrg.contains("\r")))
{
StringTokenizer st = new StringTokenizer(pStrg, "\n\r");
while (st.hasMoreTokens())
{
String s = st.nextToken();
if (st.hasMoreTokens())
{
// falls noch mind. eine Zeile folgt: mit CrLf
myPrintWriter.println(s);
}
else
{
// letzte Zeile: ohne CrLf
myPrintWriter.print(s);
}
}
}
else
{
// falls nur eine Zeile: ohne CrLf
*/
myPrintWriter.print(pStrg);
/*
}
*/
myPrintWriter.flush();
}
}