package de.desy.tine.server.logger;
import java.io.IOException;
import java.util.Vector;
import de.desy.tine.client.TLink;
import de.desy.tine.client.TLinkCallback;
import de.desy.tine.client.TLinkFactory;
import de.desy.tine.dataUtils.TDataType;
import de.desy.tine.definitions.TAccess;
import de.desy.tine.definitions.TErrorList;
import de.desy.tine.definitions.TMode;
import de.desy.tine.server.equipment.TEquipmentModuleFactory;
public class ClsLog implements TLinkCallback
{
private static TLinkFactory tlf = null;
private static TLinkFactory getLinkFactory()
{
if (tlf == null) tlf = TLinkFactory.getInstance();
return tlf;
}
private static TEquipmentModuleFactory temf = null;
private static TEquipmentModuleFactory getModuleFactory()
{
if (temf == null) temf = TEquipmentModuleFactory.getInstance();
return temf;
}
public static enum ClogStatus
{
CLOG_STATUS_NONE,
CLOG_STATUS_INFO,
CLOG_STATUS_WARN,
CLOG_STATUS_ERR
};
public static enum ClogPriority
{
CLOG_PRIORITY_NONE,
CLOG_PRIORITY_USEFUL,
CLOG_PRIORITY_IMPORTANT,
CLOG_PRIORITY_URGENT
}
private static ClsLog instance = new ClsLog();
public static synchronized int log(String text,String context,String tag,String caller,ClogPriority priority,ClogStatus status)
{
TFecLog.log(text); // log it in fec log also !
if (TLinkFactory.isRunningAsServer())
{
if (context == null) context = getModuleFactory().getFecContext();
if (tag == null) tag = getModuleFactory().getFecSubsystem();
}
else
{
if (context == null) context = "DEFAULT";
if (tag == null) tag = "";
}
if (caller == null) caller = getLinkFactory().getUserName();
if (caller == null) caller = System.getProperty("user.name");
Clog[] clog = new Clog[1];
clog[0] = new Clog(text,context,tag,caller,priority.ordinal(),status.ordinal());
TDataType clgt = new TDataType(clog);
TLink tl = new TLink("/SITE/CLOG/Logger","NewEntry",null,clgt,TAccess.CA_WRITE|TAccess.CA_RETRY);
int id = tl.attach(TMode.CM_SINGLE, instance, 1000);
return id < 0 ? -id : 0;
}
public static int log(String text)
{
ClogPriority priority = ClsLog.ClogPriority.CLOG_PRIORITY_USEFUL;
ClogStatus status = ClsLog.ClogStatus.CLOG_STATUS_INFO;
return log(text,null,null,null,priority,status);
}
public static int log(String text,ClogPriority priority,ClogStatus status)
{
return log(text,null,null,null,priority,status);
}
public void callback(TLink link)
{
if (link.getLinkStatus() != 0)
{
TFecLog.log("clslog missed entry : " + link.getLastError());
}
}
public static Clog[] getEntries(long start,long stop) throws IOException
{
return getEntries(start,stop,0);
}
public static Clog[] getEntries(long start,long stop,int maximumNumberOfEntries) throws IOException
{
ClogFilter[] f = new ClogFilter[1];
f[0] = new ClogFilter(start,stop);
return getEntries(f,maximumNumberOfEntries);
}
private static final int nice_query_size = 1000;
private static Clog[] clg = new Clog[nice_query_size];
private static boolean initialized = false;
public static Clog[] getEntries(ClogFilter[] fltr) throws IOException
{
return getEntries(fltr,0);
}
public static synchronized Clog[] getEntries(ClogFilter[] fltr,int maximumNumberOfEntries) throws IOException
{
if (!initialized)
{ // construct the local query buffer
for (int i=0; i<nice_query_size; i++) clg[i] = new Clog();
initialized = true;
}
TDataType fltt = fltr == null ? null : new TDataType(fltr);
TDataType clgt = new TDataType(clg);
TLink tl = new TLink("/SITE/CLOG/Logger", "Entries",clgt,fltt,TAccess.CA_READ);
int cc = tl.execute(1000, true);
tl.close();
if (cc != 0)
throw new IOException(TErrorList.getErrorString(cc) + " while contacting the central logging server");
int siz = clgt.getCompletionLength();
if (siz == 0) return null; // nothing came back
boolean stopQuery = false;
if (maximumNumberOfEntries <= 0) maximumNumberOfEntries = 10000000;
Vector<Clog> v = new Vector<Clog>(siz);
for (int i=0; i<siz && !stopQuery; i++)
{
clg[i].reset();
v.add(new Clog(clg[i]));
if (v.size() >= maximumNumberOfEntries) stopQuery = true;
}
while (siz == nice_query_size && !stopQuery)
{ // the query buffer was filled -> need to keep going ?
long startt = clg[siz-1].getTimeLogged();
if (fltr == null)
{ // no filter entered -> make one
fltr = new ClogFilter[1];
fltr[0] = new ClogFilter(startt,System.currentTimeMillis());
}
for (ClogFilter f : fltr) f.setTimeStart(startt); // reset all the start times
clgt = new TDataType(clg);
fltt = new TDataType(fltr);
tl = new TLink("/SITE/CLOG/Logger", "Entries",clgt,fltt,TAccess.CA_READ);
cc = tl.execute(1000, true);
tl.close();
if (cc != 0)
throw new IOException(TErrorList.getErrorString(cc) + " while contacting the central logging server");
siz = clgt.getCompletionLength();
for (int i=0; i<siz && !stopQuery; i++)
{
clg[i].reset();
v.add(new Clog(clg[i]));
if (v.size() >= maximumNumberOfEntries) stopQuery = true;
}
}
return (Clog[]) v.toArray(new Clog[1]);
}
}