Beispiel #1
0
// initLog initializes an event logger. It registers the source name with
// the registry if it does not already exist.
func initLog(provider, source, msgFile string) (*elog.Log, error) {
	// Install entry to registry:
	_, err := elog.Install(providerName, sourceName, msgFile, true, allLevels)
	if err != nil {
		return nil, err
	}

	// Open a new logger for writing events:
	log, err := elog.Open(sourceName)
	if err != nil {
		var errs multierror.Errors
		errs = append(errs, err)
		err := elog.RemoveSource(providerName, sourceName)
		if err != nil {
			errs = append(errs, err)
		}
		err = elog.RemoveProvider(providerName)
		if err != nil {
			errs = append(errs, err)
		}
		return nil, errs.Err()
	}

	return log, nil
}
Beispiel #2
0
// uninstallLog unregisters the event logger from the registry and closes the
// log's handle if it is open.
func uninstallLog(provider, source string, log *elog.Log) error {
	var errs multierror.Errors

	if log != nil {
		err := clearEventLog(Handle(log.Handle), "")
		if err != nil {
			errs = append(errs, err)
		}

		err = log.Close()
		if err != nil {
			errs = append(errs, err)
		}
	}

	err := elog.RemoveSource(providerName, sourceName)
	if err != nil {
		errs = append(errs, err)
	}

	err = elog.RemoveProvider(providerName)
	if err != nil {
		errs = append(errs, err)
	}

	return errs.Err()
}
Beispiel #3
0
func TestLog(t *testing.T) {
	if testing.Short() {
		t.Skip("skipping test in short mode - it modifies system logs")
	}

	const name = "mylog"
	const supports = eventlog.Error | eventlog.Warning | eventlog.Info |
		eventlog.Success | eventlog.AuditSuccess | eventlog.AuditFailure
	alreadyExists, err := eventlog.InstallAsEventCreate(eventlog.Application, name, supports)
	if err != nil {
		t.Fatalf("Install failed: %s", err)
	}
	t.Log("Already exists:", alreadyExists)
	defer func() {
		err = eventlog.RemoveSource(eventlog.Application, name)
		if err != nil {
			t.Fatalf("Remove failed: %s", err)
		}
	}()

	l, err := eventlog.Open(name)
	if err != nil {
		t.Fatalf("Open failed: %s", err)
	}
	defer l.Close()

	err = l.Success(1, "success")
	if err != nil {
		t.Fatalf("Successo failed: %s", err)
	}
	err = l.Info(2, "info")
	if err != nil {
		t.Fatalf("Info failed: %s", err)
	}
	err = l.Warning(3, "warning")
	if err != nil {
		t.Fatalf("Warning failed: %s", err)
	}
	err = l.Error(4, "error")
	if err != nil {
		t.Fatalf("Error failed: %s", err)
	}
	err = l.AuditSuccess(5, "audit success")
	if err != nil {
		t.Fatalf("AuditSuccess failed: %s", err)
	}
	err = l.AuditFailure(6, "audit failure")
	if err != nil {
		t.Fatalf("AuditFailure failed: %s", err)
	}
}