/*
 * Copyright (c) 2012 Mateusz Parzonka, Eric Bodden
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Mateusz Parzonka - initial API and implementation
 */
package prm4j.indexing.realtime;

import java.util.logging.FileHandler;
import java.util.logging.Formatter;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

import prm4j.api.Event;
import prm4j.indexing.staticdata.EventContext;
import prm4j.indexing.staticdata.MetaNode;
import prm4j.spec.Spec;

public class AwareParametricMonitor extends DefaultParametricMonitor {

    private final Logger logger = getFileLogger("logs/memory.log");
    private final Logger stats = getFileLogger("logs/stats.log");

    public AwareParametricMonitor(MetaNode metaTree, EventContext eventContext, Spec spec) {
	super(metaTree, eventContext, spec);
    }

    @Override
    public synchronized void processEvent(Event event) {
	super.processEvent(event);
	if (timestamp % 100 == 0) {
	    logger.log(Level.INFO, timestamp
		    + " : "
		    + (((double) (Runtime.getRuntime().totalMemory() / 1024) / 1024) - ((double) (Runtime.getRuntime()
			    .freeMemory() / 1024) / 1024)));
	}
    }

    /**
     * A simple file logger which outputs only the message.
     *
     * @param fileName
     *            path to the output file
     * @return the logger
     */
    private static Logger getFileLogger(String fileName) {
	final Logger logger = Logger.getLogger(fileName);
	try {
	    logger.setUseParentHandlers(false);
	    Handler handler = new FileHandler(fileName, true);
	    handler.setFormatter(new Formatter() {
		@Override
		public String format(LogRecord record) {
		    return record.getMessage() + "\n";
		}
	    });
	    logger.addHandler(handler);
	} catch (Exception e) {
	    throw new RuntimeException(e);
	}
	return logger;
    }

    @Override
    public void reset() {
	stats.log(Level.INFO, "Created nodes: " + nodeManager.getCreatedCount());
	stats.log(Level.INFO, "Orphaned monitors: " + nodeManager.getOrphanedMonitorsCount());
	stats.log(Level.INFO, "Collected monitors: " + nodeManager.getCollectedMonitorsCount());
	stats.log(Level.INFO, "Created bindings: " + bindingStore.getCreatedBindingsCount());
	stats.log(Level.INFO, "Collected bindings: " + bindingStore.getCollectedBindingsCount());
	stats.log(Level.INFO, "Stored bindings: " + bindingStore.size());
	super.reset();
    }

}

@@ -1218,16 +1218,18 @@
 mp %25 100
+00
  == 0) %7B
@@ -2089,32 +2089,86 @@
  logger;%0A    %7D%0A%0A
+    public Logger getLogger() %7B%0A%09return stats;%0A    %7D%0A%0A
     @Override%0A  
@@ -2219,56 +2219,40 @@
 O, %22
-Created nodes: %22 + nodeManager.getCreatedCount()
+EVENTS: totalCount=%22 + timestamp
 );%0A%09
@@ -2253,32 +2253,35 @@
 mp);%0A%09stats.log(
+%0A%09%09
 Level.INFO, %22Orp
@@ -2279,121 +2279,125 @@
 NFO,
- %22Orphaned monitors: %22 + nodeManager.getOrphanedMonitorsCount());%0A%09stats.log(Level.INFO, %22Collected monitors: %22 +
+%0A%09%09String.format(%22BINDINGS: created=%25d / collected=%25d / stored(still)=%25d%22,%0A%09%09%09nodeManager.getOrphanedMonitorsCount(),
  nod
@@ -2424,32 +2424,57 @@
 dMonitorsCount()
+,%0A%09%09%09bindingStore.size())
 );%0A%09stats.log(Le
@@ -2487,43 +2487,54 @@
 FO, 
-%22Created bindings: %22 + bindingStore
+String.format(%22NODES: created=%25d%22, nodeManager
 .get
@@ -2540,32 +2540,24 @@
 tCreated
-Bindings
 Count())
 ;%0A%09stats
@@ -2540,32 +2540,33 @@
 tCreatedCount())
+)
 ;%0A%09stats.log(Lev
@@ -2554,32 +2554,35 @@
 )));%0A%09stats.log(
+%0A%09%09
 Level.INFO, %22Col
@@ -2580,140 +2580,147 @@
 NFO,
- %22Collected bindings: %22 + bindingStore.getCollectedBindingsCount());%0A%09stats.log(Level.INFO, %22Stored bindings: %22 + bindingStore.size(
+%0A%09%09String.format(%22MONITORS: orphaned=%25d / collected=%25d%22, nodeManager.getOrphanedMonitorsCount(),%0A%09%09%09nodeManager.getCollectedMonitorsCount()
 ));%0A

Improve logging style in AwareParametricMonitor