eventlog.java
/*
* Copyright (c) 2001-2005 Servertec. All Rights Reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* THIS NOTICE MUST NOT BE ALTERED NOR REMOVED.
*
* CopyrightVersion 1.0
*/
import stec.sfc.Win32.EventLog;
import stec.sfc.Win32.EventLogType;
import stec.sfc.Win32.EventLogRecord;
import stec.sfc.Win32.sfc;
public class eventlog
{
public static void main(String[] args) throws Exception
{
if((stec.sfc.Win32.System.getVersion() & 0x80000000) == 0x80000000)
{
System.out.println("eventlog requires Windows NT/2000/XP.");
return;
}
if(args == null)
{
syntax("action was not specified.");
return;
}
if(args.length < 1)
{
syntax("action was not specified.");
return;
}
String action = args[0].toLowerCase();
if(action.equals("-help") || action.equals("-h") || action.equals("-?"))
{
syntax();
return;
}
else if(action.equals("-dump"))
{
EventLog event_log = new EventLog(EventLog.APPLICATION);
try
{
dump(event_log);
}
finally
{
event_log.close();
}
return;
}
if(args.length < 2)
{
syntax("source was not specified.");
return;
}
EventLog.addSource(EventLog.APPLICATION, args[1], "c:\\sfc\\bin\\" + sfc.getLibrary() + ".dll", EventLogType.ERROR | EventLogType.INFORMATION | EventLogType.WARNING, 1);
EventLog event_log = new EventLog(args[1]);
try
{
if(action.equals("-log"))
{
if(args.length < 3)
{
syntax("type was not specified.");
return;
}
else if(args.length < 4)
{
syntax("message was not specified.");
return;
}
int type;
String value = args[2].toLowerCase();
if(value.equals("info"))
{
type = EventLogType.INFORMATION;
}
else if(value.equals("warn"))
{
type = EventLogType.WARNING;
}
else if(value.equals("error"))
{
type = EventLogType.ERROR;
}
else
{
syntax("type is not valid: " + value);
return;
}
int length = args.length - 3;
String[] messages = new String[length];
for(int i = 0; i < length; i++)
{
messages[i] = args[i + 3];
}
event_log.log(type, messages);
System.out.println("message logged.");
}
else if(action.equals("-clear"))
{
if(args.length > 2)
{
event_log.backup(args[2]);
}
event_log.clear();
System.out.println("log cleared.");
}
else if(action.equals("-backup"))
{
if(args.length < 2)
{
syntax("filename was not specified.");
return;
}
event_log.backup(args[2]);
System.out.println("backup completed.");
}
else
{
System.out.println("Unknown action: " + args[0]);
return;
}
}
finally
{
event_log.close();
}
}
private static void dump(EventLog event_log) throws Exception
{
EventLogRecord event_log_record;
String message;
int count = event_log.size();
while((event_log_record = event_log.next()) != null)
{
output("Record Number", event_log_record.getRecordNumber());
output("Generated Timestamp", event_log_record.getGeneratedTimestamp());
output("Written Timestamp", event_log_record.getWrittenTimestamp());
output("Event ID", event_log_record.getEventID() & 0x0000ffff);
output("Event Type", EventLogType.toString(event_log_record.getEventType()));
String category = event_log_record.getCategory();
if(category.equals("0"))
{
category = "None";
}
output("Category", category);
output("Source Name", event_log_record.getSourceName());
output("Computer Name", event_log_record.getComputerName());
output("User Name", event_log_record.getUserName());
System.out.println("Message:");
message = event_log_record.getMessage();
if(message != null)
{
System.out.println(message);
}
System.out.println();
}
if(count == 1)
{
System.out.println("1 event log record was dumped.");
}
else
{
System.out.println(count + " event log records were dumped.");
}
}
private static final void output(String name, String value)
{
System.out.print(name);
System.out.print(": ");
if(value != null)
{
System.out.print(value);
}
System.out.println();
}
private static final void output(String name, int value)
{
output(name, String.valueOf(value));
}
private static final void output(String name, long value)
{
output(name, String.valueOf(value));
}
private static final void syntax()
{
System.out.println("usage: eventlog {}");
System.out.println();
System.out.println("action: -backup ");
System.out.println(" -clear {}");
System.out.println(" -dump");
System.out.println(" -help");
System.out.println(" -log [info|error|warn] ...");
System.out.println();
}
private static final void syntax(String message)
{
syntax();
if(message != null)
{
System.out.println(message);
}
System.out.println();
}
}