* LocalFileEventLog.cs: When event log store does not exist, then we
[mono.git] / mcs / class / System / System.Diagnostics / EventLogImpl.cs
index 2605298cb20f9ff4ee37cfaee5b1c110de66193d..ea895a87574490dd7a93b43e3a23bdd015b021cc 100644 (file)
@@ -3,10 +3,12 @@
 //
 // Authors:
 //   Andreas Nahr (ClassDevelopment@A-SoftTech.com)
+//   Atsushi Enomoto  <atsushi@ximian.com>
+//   Gert Driesen (drieseng@users.sourceforge.net)
 //
 // (C) 2003 Andreas Nahr
+// (C) 2006 Novell, Inc.
 //
-
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
 //
 
 using System;
-using System.Diagnostics;
 using System.ComponentModel;
 using System.ComponentModel.Design;
+using System.Diagnostics;
+using System.Globalization;
+
+using Microsoft.Win32;
 
 namespace System.Diagnostics
 {
+       internal abstract class EventLogImpl
+       {
+               readonly EventLog _coreEventLog;
 
-// FIXME set a symbol for every implementation and include the implementation
-#if (EVENTLOG_WIN32)
-
-       // TODO implement the EventLog for Win32 platforms
+               protected EventLogImpl (EventLog coreEventLog)
+               {
+                       _coreEventLog = coreEventLog;
+               }
 
-#elif (EVENTLOG_GENERIC)
+               public event EntryWrittenEventHandler EntryWritten;
 
-       // TODO implement a generic (XML - based?) Eventlog for non - Win32 platforms
+               protected EventLog CoreEventLog {
+                       get { return _coreEventLog; }
+               }
 
-#else
-       // Empty implementation that does not need any specific platform
-       // but should be enough to get applications to run that WRITE to eventlog
-       internal class EventLogImpl
-       {
-               public EventLogImpl (EventLog coreEventLog)
-               {
+               public int EntryCount {
+                       get {
+                               if (_coreEventLog.Log == null || _coreEventLog.Log.Length == 0) {
+                                       throw new ArgumentException ("Log property is not set.");
+                               }
+
+                               if (!EventLog.Exists (_coreEventLog.Log, _coreEventLog.MachineName)) {
+                                       throw new InvalidOperationException (string.Format (
+                                               CultureInfo.InvariantCulture, "The event log '{0}' on "
+                                               + " computer '{1}' does not exist.", _coreEventLog.Log,
+                                               _coreEventLog.MachineName));
+                               }
+
+                               return GetEntryCount ();
+                       }
                }
 
-               public static event EntryWrittenEventHandler EntryWritten;
+               public EventLogEntry this[int index] {
+                       get {
+                               if (_coreEventLog.Log == null || _coreEventLog.Log.Length == 0) {
+                                       throw new ArgumentException ("Log property is not set.");
+                               }
+
+                               if (!EventLog.Exists (_coreEventLog.Log, _coreEventLog.MachineName)) {
+                                       throw new InvalidOperationException (string.Format (
+                                               CultureInfo.InvariantCulture, "The event log '{0}' on "
+                                               + " computer '{1}' does not exist.", _coreEventLog.Log,
+                                               _coreEventLog.MachineName));
+                               }
+
+                               if (index < 0 || index >= EntryCount)
+                                       throw new ArgumentException ("Index out of range");
 
-               public EventLogEntryCollection Entries {
-                       get {return new EventLogEntryCollection ();}
+                               return GetEntry (index);
+                       }
                }
 
                public string LogDisplayName {
-                       get {return "";}
+                       get {
+#if NET_2_0
+                               // to-do perform valid character checks
+                               if (_coreEventLog.Log != null && _coreEventLog.Log.Length == 0) {
+                                       throw new InvalidOperationException ("Event log names must"
+                                               + " consist of printable characters and cannot contain"
+                                               + " \\, *, ?, or spaces.");
+                               }
+#endif
+                               if (_coreEventLog.Log != null) {
+                                       if (!EventLog.Exists (_coreEventLog.Log, _coreEventLog.MachineName)) {
+                                               throw new InvalidOperationException (string.Format (
+                                                       CultureInfo.InvariantCulture, "Cannot find Log {0}"
+                                                       + " on computer {1}.", _coreEventLog.Log,
+                                                       _coreEventLog.MachineName));
+                                       }
+                               }
+
+                               return GetLogDisplayName ();
+                       }
                }
 
-               public void BeginInit () {}
+               public EventLogEntry [] GetEntries ()
+               {
+                       string logName = CoreEventLog.Log;
+                       if (logName == null || logName.Length == 0)
+                               throw new ArgumentException ("Log property value has not been specified.");
+
+                       if (!EventLog.Exists (logName))
+                               throw new InvalidOperationException (string.Format (
+                                       CultureInfo.InvariantCulture, "The event log '{0}' on "
+                                       + " computer '{1}' does not exist.", logName,
+                                       _coreEventLog.MachineName));
+
+                       int entryCount = GetEntryCount ();
+                       EventLogEntry [] entries = new EventLogEntry [entryCount];
+                       for (int i = 0; i < entryCount; i++) {
+                               entries [i] = GetEntry (i);
+                       }
+                       return entries;
+               }
 
-               public void Clear () {}
+               public abstract void BeginInit ();
 
-               public void Close () {}
+               public abstract void Clear ();
 
-               public static void CreateEventSource (string source, string logName, string machineName) {}
+               public abstract void Close ();
 
-               public static void Delete (string logName, string machineName) {}
+               public abstract void CreateEventSource (EventSourceCreationData sourceData);
 
-               public static void DeleteEventSource (string source, string machineName) {}
+               public abstract void Delete (string logName, string machineName);
 
-               public void Dispose (bool disposing) {}
+               public abstract void DeleteEventSource (string source, string machineName);
 
-               public void EndInit () {}
+               public abstract void Dispose (bool disposing);
 
-               public static bool Exists (string logName, string machineName)
-               {
-                       return false;
-               }
+               public abstract void EndInit ();
 
-               public static EventLog[] GetEventLogs (string machineName)
-               {
-                       return new EventLog[0];
-               }
+               public abstract bool Exists (string logName, string machineName);
 
-               public static string LogNameFromSourceName (string source, string machineName)
-               {
-                       return String.Empty;
-               }
+               protected abstract int GetEntryCount ();
 
-               public static bool SourceExists (string source, string machineName)
-               {
-                       return false;
-               }
+               protected abstract EventLogEntry GetEntry (int index);
 
-               public void WriteEntry (string message, EventLogEntryType type, int eventID, short category, byte[] rawData)
-               {
-                       WriteEntry ("", message, type, eventID, category, rawData);
-               }
+               public abstract EventLog [] GetEventLogs (string machineName);
 
-               public static void WriteEntry (string source, string message, EventLogEntryType type, int eventID, short category, byte[] rawData)
-               {
-                       EventLogEntry Entry;
-                       Entry = new EventLogEntry ("", category, 0, eventID, message, source, 
-                               "", "", type, DateTime.Now, DateTime.Now, rawData, null);
-                       if (EntryWritten != null)
-                               EntryWritten (null, new EntryWrittenEventArgs (Entry));
-               }
-       }
+               protected abstract string GetLogDisplayName ();
 
-#endif
+               public abstract string LogNameFromSourceName (string source, string machineName);
+
+               public abstract bool SourceExists (string source, string machineName);
 
+               public abstract void WriteEntry (string [] replacementStrings, EventLogEntryType type, uint instanceID, short category, byte[] rawData);
+
+               protected abstract string FormatMessage (string source, uint messageID, string [] replacementStrings);
+       }
 }