001package org.intellimate.izou.util;
002
003import org.apache.logging.log4j.Level;
004import org.apache.logging.log4j.LogManager;
005import org.apache.logging.log4j.spi.AbstractLogger;
006import org.intellimate.izou.identification.Identifiable;
007import org.intellimate.izou.identification.IdentificationManager;
008import org.intellimate.izou.main.Main;
009
010/**
011 * The base class for each IzouModule
012 * FIXME: What is an izou module exactly?
013 *
014 * @author Leander Kurscheidt
015 * @version 1.0
016 */
017public abstract class IzouModule implements MainProvider, Identifiable {
018    private static final String FQCN = IzouModule.class.getName();
019    private String ID = this.getClass().getCanonicalName();
020    protected Main main;
021    protected final AbstractLogger log = (AbstractLogger) LogManager.getLogger(this.getClass());
022
023    public IzouModule(Main main) {
024        this(main, true);
025    }
026
027    public IzouModule(Main main, boolean register) {
028        this.main = main;
029        if (register) {
030            if (!IdentificationManager.getInstance().registerIdentification(this)) {
031                log.fatal("unable to register! " + getClass().getCanonicalName());
032            }
033        }
034    }
035
036    /**
037     * returns the instance of Main
038     *
039     * @return Main
040     */
041    @Override
042    public Main getMain() {
043        return main;
044    }
045
046    /**
047     * Used to log messages at debug level
048     *
049     * @param msg the message
050     * @param e   the Throwable
051     */
052    @Override
053    public void debug(String msg, Throwable e) {
054        log.logIfEnabled(FQCN, Level.DEBUG, null, msg, e);
055    }
056
057    /**
058     * Used to log messages at debug level
059     *
060     * @param msg the message
061     */
062    @Override
063    public void debug(String msg) {
064        log.logIfEnabled(FQCN, Level.DEBUG, null, msg, (Object) null);
065    }
066
067    /**
068     * Used to log messages at error level
069     *
070     * @param msg the message
071     * @param e   the Throwable
072     */
073    @Override
074    public void error(String msg, Throwable e) {
075        log.logIfEnabled(FQCN, Level.ERROR, null, msg, e);
076    }
077
078    /**
079     * Used to log messages at error level
080     *
081     * @param msg the message
082     */
083    @Override
084    public void error(String msg) {
085        log.logIfEnabled(FQCN, Level.ERROR, null, msg, (Object) null);
086    }
087
088    /**
089     * Used to log messages at fatal level
090     *
091     * @param msg the message
092     */
093    @Override
094    public void fatal(String msg) {
095        log.logIfEnabled(FQCN, Level.FATAL, null, msg, (Object) null);
096    }
097
098    /**
099     * Used to log messages at fatal level
100     *
101     * @param msg the message
102     * @param e   the Throwable
103     */
104    @Override
105    public void fatal(String msg, Throwable e) {
106        log.logIfEnabled(FQCN, Level.FATAL, null, msg, e);
107    }
108
109    /**
110     * An ID must always be unique.
111     * A Class like Activator or OutputPlugin can just provide their .class.getCanonicalName()
112     * If you have to implement this interface multiple times, just concatenate unique Strings to
113     * .class.getCanonicalName()
114     *
115     * @return A String containing an ID
116     */
117    @Override
118    public String getID() {
119        return ID;
120    }
121}