All Downloads are FREE. Search and download functionalities are using the official Maven repository.

Alachisoft.NCache.Common.Logger.EventLogger Maven / Gradle / Ivy

There is a newer version: 5.3.3
Show newest version
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Alachisoft.NCache.Common.Logger;

import Alachisoft.NCache.Common.Enum.EventType;
import Alachisoft.NCache.Common.Event.NEvent;
import Alachisoft.NCache.Common.Event.NEventStart;
import Alachisoft.NCache.Common.Monitoring.EventViewerEvent;
import com.alachisoft.ncache.runtime.util.NCDateTime;

import java.net.SocketException;
import java.util.ArrayList;
import java.util.Date;

/**
 * @author ayesha_sana
 */
public class EventLogger implements NEventStart {
    private static long _instanceId = 0;
    private static IEventLogger _eventlogger = null;
    private static JLogger _fileLogger = null;
    private static boolean recognizedOS = true;
    private static String lastLoggedEvent;
    private static NEvent _nevent = new NEvent("OnEventLogEntry", _fileLogger);
    //    private static ArrayList logList;
//    private static Mutex _mutex;
    private static ArrayList _events;


    public static String getLastLoggedEvent() {
        return lastLoggedEvent;
    }

    public static void setLastLoggedEvent(String msg, String type) {
        EventLogger.lastLoggedEvent = type.concat(msg);
    }

    public static void Initialize(boolean isBridgeLog) {
//        _mutex = new Mutex();
        _events = new ArrayList();
//        logList = new ArrayList();
        _fileLogger = new JLogger();
        lastLoggedEvent = new String();
        if (_eventlogger == null) {

            _eventlogger = new JLinuxLogger();
            recognizedOS = false;


            try {
                _fileLogger.Initialize(LoggerNames.EventLogs, true);
            } catch (Exception ex) {
                //ex.printStackTrace();
            }
            _eventlogger.setLogger(_fileLogger);

            if (!recognizedOS) {
                _eventlogger.LogEvent("OS is not recognized: " + System.getProperty("os.name"), EventType.ERROR);
            }
        }
    }

    public static void registerEvents(String name) {
        synchronized (_nevent) {
            _nevent.addNEventListners(new NEventStart() {

                @Override
                public Object hanleEvent(Object... obj) throws SocketException, Exception {
                    return _events.add((EventViewerEvent) obj[0]);
                }
            }, null);
//            logList.add(name);
        }
    }

    public static void unregisterEvents(String name) {
        synchronized (_nevent) {
            _nevent.removeNEventListners(null);
        }
    }

    public synchronized static void LogEvent(String msg, EventType type) {
        LogEvent("NCache", msg, type, (short) -1, -1);
    }

    public synchronized static void LogEvent(String source, String msg, EventType type, short category, int eventid) {
        if (_eventlogger == null) {
            return;
        }
        EventLogger.setLastLoggedEvent(msg, type.toString());
        _eventlogger.LogEvent(msg, type);

        EventViewerEvent event = new EventViewerEvent();
        event.setInstanceId(eventid);
        event.setEventType(type);
        event.setMessage(msg);
        event.setSource(source);
        try {
            event.setGeneratedTime(new NCDateTime(new Date()));
        } catch (Exception ex) {
            LogEvent("NCache", ex.getMessage(), EventType.ERROR, (short) -1, -1);
        }

        _events.add(event);
//        logList.add(msg);
    }

    public static synchronized EventViewerEvent[] getEventList() {
        EventViewerEvent[] eventList = new EventViewerEvent[_events.size()];
        return _events.toArray(eventList);
    }

    public static synchronized void clearEvents() {
        _events.clear();
    }

    public void OnLogEntryWritten(EventViewerEvent event) {
        synchronized (_nevent) {
            _events.add(event);
        }
    }

    @Override
    public Object hanleEvent(Object... obj) throws SocketException, Exception {
        return _events.add((EventViewerEvent) obj[0]);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy