Browse Source

Code tidy + new methods in PrismFileLog.

git-svn-id: https://www.prismmodelchecker.org/svn/prism/prism/trunk@8074 bbc10eb1-c90d-0410-af57-cb519fbb1720
master
Dave Parker 12 years ago
parent
commit
7b8b7832a4
  1. 91
      prism/src/prism/PrismFileLog.java

91
prism/src/prism/PrismFileLog.java

@ -26,113 +26,172 @@
package prism; package prism;
/**
* PrismLog object that writes all output to a file.
*/
public class PrismFileLog extends PrismLog public class PrismFileLog extends PrismLog
{ {
String filename;
long fp;
boolean stdout;
/** Filename (or "stdout") */
protected String filename;
/** Native file pointer, cast to a long */
protected long fp;
/** Are we writing to stdout? */
protected boolean stdout;
/**
* Pointless constructor. Don't call this.
*/
public PrismFileLog() public PrismFileLog()
{ {
filename = ""; filename = "";
fp = 0; fp = 0;
} }
public PrismFileLog(String s)
/**
* Create a PRISM log which will write to {@code filename}, overwriting any previous contents.
* @param filename Filename of log file
*/
public PrismFileLog(String filename)
{
open(filename);
}
/**
* Create a PRISM log which will write to {@code filename}, appending to an existing file if requested.
* @param filename Filename of log file
* @param append Append to the existing file?
*/
public PrismFileLog(String filename, boolean append)
{
open(filename, append);
}
/**
* Create a PRISM log which will write to {@code filename}, overwriting any previous contents.
* Throw a PRISM exception if there is a problem opening the file for writing.
* @param filename Filename of log file
*/
public static PrismFileLog create(String filename) throws PrismException
{ {
open(s);
return create(filename, false);
} }
public PrismFileLog(String s, boolean append)
/**
* Create a PRISM log which will write to {@code filename}, appending to an existing file if requested.
* Throw a PRISM exception if there is a problem opening the file for writing.
* @param filename Filename of log file
* @param append Append to the existing file?
*/
public static PrismFileLog create(String filename, boolean append) throws PrismException
{ {
open(s, append);
PrismFileLog log = new PrismFileLog(filename, append);
if (!log.ready()) {
throw new PrismException("Could not open file \"" + filename + "\" for output");
}
return log;
} }
public void open(String s)
public void open(String filename)
{ {
open (s, false);
open (filename, false);
} }
public void open(String s, boolean append)
public void open(String filename, boolean append)
{ {
filename = s;
if (s.equals("stdout")) {
this.filename = filename;
if (filename.equals("stdout")) {
fp = PrismNative.PN_GetStdout(); fp = PrismNative.PN_GetStdout();
stdout = true; stdout = true;
} }
else { else {
fp = append ? PrismNative.PN_OpenFileAppend(s) : PrismNative.PN_OpenFile(s);
fp = append ? PrismNative.PN_OpenFileAppend(filename) : PrismNative.PN_OpenFile(filename);
stdout = false; stdout = false;
} }
} }
// Methods for PrismLog
@Override
public boolean ready() public boolean ready()
{ {
return (fp != 0); return (fp != 0);
} }
@Override
public long getFilePointer() public long getFilePointer()
{ {
return fp; return fp;
} }
@Override
public void flush() public void flush()
{ {
PrismNative.PN_FlushFile(fp); PrismNative.PN_FlushFile(fp);
} }
@Override
public void close() public void close()
{ {
if (!stdout) PrismNative.PN_CloseFile(fp); if (!stdout) PrismNative.PN_CloseFile(fp);
} }
// Basic print methods
@Override
public void print(boolean b) public void print(boolean b)
{ {
printToLog("" + b); printToLog("" + b);
} }
@Override
public void print(char c) public void print(char c)
{ {
printToLog("" + c); printToLog("" + c);
} }
@Override
public void print(double d) public void print(double d)
{ {
printToLog("" + d); printToLog("" + d);
} }
@Override
public void print(float f) public void print(float f)
{ {
printToLog("" + f); printToLog("" + f);
} }
@Override
public void print(int i) public void print(int i)
{ {
printToLog("" + i); printToLog("" + i);
} }
@Override
public void print(long l) public void print(long l)
{ {
printToLog("" + l); printToLog("" + l);
} }
@Override
public void print(Object obj) public void print(Object obj)
{ {
printToLog("" + obj); printToLog("" + obj);
} }
@Override
public void print(String s) public void print(String s)
{ {
printToLog(s); printToLog(s);
} }
@Override
public void println() public void println()
{ {
printToLog("\n"); printToLog("\n");
} }
/**
* Do the actual write (via native code).
*/
private void printToLog(String s) private void printToLog(String s)
{ {
PrismNative.PN_PrintToFile(fp, s); PrismNative.PN_PrintToFile(fp, s);

Loading…
Cancel
Save