/** * */ package com.perforce.p4javademo; import java.io.File; import java.io.FileOutputStream; import java.io.PrintStream; import java.util.Date; import com.perforce.p4java.Log; import com.perforce.p4java.server.IOptionsServer; import com.perforce.p4java.server.callback.ILogCallback; /** * P4Java Log class callback demo class. The output of this * demo will (of course) vary greatly depending on the particular * circumstances of running it. In any case, the idea is * simply to show potential users how to use the log callback -- * check the log file and see what's in it. Under normal * circumstances, there should at least be some INFO messages, but * this version may produce nothing at all, or a large number of * messages depending on circumstances. */ public class LogDemo extends P4JavaDemo { /** * What we're going to call the log file. Change this * to whatever you want, but be careful of overwriting * existing files... */ private static final String LOG_FILE_NAME = "/tmp/.p4javademolog"; public static void main(String[] args) { LogCallback logCallback = null; IOptionsServer server = null; try { logCallback = new LogCallback(new PrintStream( new FileOutputStream(new File(LOG_FILE_NAME), true))); Log.setLogCallback(logCallback); server = getOptionsServer(null, null); server.disconnect(); } catch (Exception exc) { System.err.println(exc.getLocalizedMessage()); } } /** * Simple example P4Java ILogCallback implementation.<p> * * Note that this example does not attempt to prevent concurrent * calls to the various methods here (there's no need in the example * app), but real versions might want to implement synchronized access * or methods (with suitable attention being paid to deadlock and * blocking issues, etc.). */ public static class LogCallback implements ILogCallback { private LogTraceLevel traceLevel = LogTraceLevel.NONE; // Don't want tracing... private PrintStream outStream = null; public LogCallback(PrintStream outStream) { this.outStream = outStream; } public void setTracelLevel(LogTraceLevel traceLevel) { this.traceLevel = traceLevel; } public LogTraceLevel getTraceLevel() { return this.traceLevel; } public void internalError(String errorString) { if (this.outStream != null) { } } public void internalException(Throwable thr) { if (thr != null) { printMessage(thr.getLocalizedMessage(), "EXCEPTION"); thr.printStackTrace(this.outStream); } } public void internalInfo(String infoString) { printMessage(infoString, "INFO"); } public void internalStats(String statsString) { printMessage(statsString, "STATS"); } public void internalTrace(LogTraceLevel traceLevel, String traceMessage) { // Note: tracing does not work for normal P4Java GA releases, so you // should not see any calls to this method unless you have a "special" // release... printMessage(traceMessage, "TRACE"); } public void internalWarn(String warnString) { printMessage(warnString, "WARNING"); } private void printMessage(String msg, String pfx) { if (msg != null) { this.outStream.println(new Date() + " (" + pfx + "): " + msg); } } } }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#1 | 19903 | stuartrowe |
Branching //guest/perforce_software/p4java/... to //guest/stuartrowe/p4java/... |
||
//guest/perforce_software/p4java/main/samples/basic/src/com/perforce/p4javademo/LogDemo.java | |||||
#1 | 12851 | Matt Attaway | Reorganize content to match Workshop branch naming standards | ||
//guest/perforce_software/p4java/samples/basic/src/com/perforce/p4javademo/LogDemo.java | |||||
#2 | 7778 | jkovisto | Updated for 2010.1 Release | ||
#1 | 7534 | hreid | Added P4Java basic demo samples to public depot for 9.2 beta release. |