<commit_before>/*
 * Copyright (c) 2012 <NAME>, <NAME>
 * 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:
 * <NAME> - 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();
    }

}<commit_msg>Improve logging style in AwareParametricMonitor<commit_after>/*
 * Copyright (c) 2012 <NAME>, <NAME>
 * 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:
 * <NAME> - 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 % 10000 == 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;
    }

    public Logger getLogger() {
	return stats;
    }

    @Override
    public void reset() {
	stats.log(Level.INFO, "EVENTS: totalCount=" + timestamp);
	stats.log(
		Level.INFO,
		String.format("BINDINGS: created=%d / collected=%d / stored(still)=%d",
			nodeManager.getOrphanedMonitorsCount(), nodeManager.getCollectedMonitorsCount(),
			bindingStore.size()));
	stats.log(Level.INFO, String.format("NODES: created=%d", nodeManager.getCreatedCount()));
	stats.log(
		Level.INFO,
		String.format("MONITORS: orphaned=%d / collected=%d", nodeManager.getOrphanedMonitorsCount(),
			nodeManager.getCollectedMonitorsCount()));
	super.reset();
    }

}
{'fraction_non_alphanumeric': 0.11977256052824652, 'fraction_numerical': 0.012472487160674981, 'mean_word_length': 3.9170423805229935, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5451564', 'n_tokens_mistral': 1721, 'n_tokens_neox': 1605, 'n_words': 397}