import com.plumtree.remote.logging.ILogger;
import com.plumtree.remote.logging.LogFactory;

/**
 * Demonstrates Plumtree EDK 5.2 logging API.
 */
public class LoggingCommandLineExample extends Thread
{
	private static final String INSTANCES_COMPONENT_NAME = "Instances";
	private static final String MAIN_LOOP_COMPONENT_NAME = "Main Loop";

	// characters legal for logging application names: ASCII alphanumerics and space plus . - and _.
	public static final String LOGGING_APPLICATION_NAME = "Logging_API_Example-1";

	// set to true to multicast log messages to local network
	// set to false to send message only listeners on local machine
	public static final boolean LOG_TO_NETWORK = true;

	private ILogger logger;              // instance logging class
	private static ILogger mainLogger;   // main component logging class

	// Suggested initialization for non-web applications is in a static block
	// in your application's main class or a logging utility class.
	static
	{
		// Don't attempt to re-initialize in case logging was already
		// initialized (for example, as part of an EDK-based web application).
		if (!LogFactory.isInitialized())
		{
			LogFactory.initialize(LOGGING_APPLICATION_NAME, LOG_TO_NETWORK);
		}

		System.out.print("Set your logging receiver to the \"server\" or \"application name\" ");
		System.out.println(LogFactory.getApplicationName());
		System.out.println("The logging component names are \"EDK\", \"" + MAIN_LOOP_COMPONENT_NAME + "\" and \"" + INSTANCES_COMPONENT_NAME + "\".");

		mainLogger = LogFactory.getLogger(MAIN_LOOP_COMPONENT_NAME, LoggingCommandLineExample.class);
	}


	public LoggingCommandLineExample(String instanceName)
	{
		setName(instanceName);
		this.logger = LogFactory.getLogger(INSTANCES_COMPONENT_NAME, LoggingCommandLineExample.class);
		mainLogger.info("Created new instance named {0}", instanceName);
	}


	public static void main(String[] args)
	{
		final String methodName = "main";
		mainLogger.functionBegin(methodName);

		// get a timestamp to measure performance of this function
		long performanceStartTicks = mainLogger.performanceBegin();

		mainLogger.action("Creating and starting instances");

		LoggingCommandLineExample bill = new LoggingCommandLineExample("Bill");
		bill.start();
		LoggingCommandLineExample larry = new LoggingCommandLineExample("Larry");
		larry.start();

		mainLogger.action("Done creating instances");

		// send log message with time since performanceBegin
		mainLogger.performanceEnd(methodName, performanceStartTicks);
		mainLogger.functionEnd(methodName);
	}


	// Each thread runs through a small test of logging messages and
	// interleaves work to the other by calling yield().
	public void run()
	{
		final String methodName = "run";

		// send log message that function is starting
		logger.functionBegin(methodName);

		// get a timestamp to measure performance of this function
		long performanceStartTicks = mainLogger.performanceBegin();

		yield();

		logger.action("Action log messages are on by default in the log receiver.");

		// demonstrates the available logging levels and shows use of token
		// substitution in format strings to construct messages.
		String levelDescriptionFormat = "{0} level messages are {1} by default in the log receiver.";

		// debug() through fatal() are increasingly serious warning messages.
		//   By comparison, the message types action, function, and performance
		//   are generally "info" or "debug" level, with Action perhaps representing
		//   a message more important than "Info".  Function and performance messages
		//   are off by default.
		logger.debug(levelDescriptionFormat, "Debug", "off");
		logger.info(levelDescriptionFormat, "Info",  "off");
		logger.warn(levelDescriptionFormat, "Warn",  "on");
		logger.error(levelDescriptionFormat, "Error", "on");
		logger.fatal(levelDescriptionFormat, "Fatal", "on");

		yield();

		// Exceptions may also be caught and logged, and may use token substitution.
		try
		{
			throw new InterruptedException(getName() + " was interrupted.");
		}
		catch (Exception eCaught)
		{
			logger.warn(eCaught, "Caught an exception from {0}. ", eCaught.getClass().getPackage().getName());
		}

		yield();

		// send log message with time since performanceBegin
		mainLogger.performanceEnd(methodName, performanceStartTicks);

		// send log message that function is ending
		logger.functionEnd(methodName);
	}
}
