chore: Move private packages to internal (#1664)

This commit is contained in:
Jan-Otto Kröpke
2024-10-03 20:23:56 +02:00
committed by GitHub
parent bcfe6df24d
commit 5d95610c84
181 changed files with 1680 additions and 1867 deletions

View File

@@ -0,0 +1,53 @@
//go:build windows
// Package eventlog provides a Logger that writes to Windows Event Log.
package eventlog
import (
"bytes"
"fmt"
"io"
"golang.org/x/sys/windows"
)
const (
// NeLogOemCode is a generic error log entry for OEMs to use to
// elog errors from OEM value added services.
// See: https://github.com/microsoft/win32metadata/blob/2f3c5282ce1024a712aeccd90d3aa50bf7a49e27/generation/WinSDK/RecompiledIdlHeaders/um/LMErrlog.h#L824-L845
neLogOemCode = uint32(3299)
)
// Interface guard.
var _ io.Writer = (*Writer)(nil)
type Writer struct {
handle windows.Handle
}
// NewEventLogWriter returns a new Writer which writes to Windows EventLog.
func NewEventLogWriter(handle windows.Handle) *Writer {
return &Writer{handle: handle}
}
func (w *Writer) Write(p []byte) (int, error) {
var eType uint16
switch {
case bytes.Contains(p, []byte(" level=error")) || bytes.Contains(p, []byte(`"level":"error"`)):
eType = windows.EVENTLOG_ERROR_TYPE
case bytes.Contains(p, []byte(" level=warn")) || bytes.Contains(p, []byte(`"level":"warn"`)):
eType = windows.EVENTLOG_WARNING_TYPE
default:
eType = windows.EVENTLOG_INFORMATION_TYPE
}
msg, err := windows.UTF16PtrFromString(string(p))
if err != nil {
return 0, fmt.Errorf("error convert string to UTF-16: %w", err)
}
ss := []*uint16{msg, nil, nil, nil, nil, nil, nil, nil, nil}
return len(p), windows.ReportEvent(w.handle, eType, 0, neLogOemCode, 0, 9, 0, &ss[0], nil)
}

37
internal/log/flag/flag.go Normal file
View File

@@ -0,0 +1,37 @@
// Copyright 2017 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package flag
import (
"github.com/alecthomas/kingpin/v2"
"github.com/prometheus-community/windows_exporter/internal/log"
"github.com/prometheus/common/promslog"
"github.com/prometheus/common/promslog/flag"
)
// FileFlagName is the canonical flag name to configure the log file.
const FileFlagName = "log.file"
// FileFlagHelp is the help description for the log.file flag.
const FileFlagHelp = "Output file of log messages. One of [stdout, stderr, eventlog, <path to log file>]"
// AddFlags adds the flags used by this package to the Kingpin application.
// To use the default Kingpin application, call AddFlags(kingpin.CommandLine).
func AddFlags(a *kingpin.Application, config *log.Config) {
config.Config = new(promslog.Config)
flag.AddFlags(a, config.Config)
config.File = &log.AllowedFile{}
a.Flag(FileFlagName, FileFlagHelp).Default("stderr").SetValue(config.File)
}

69
internal/log/logger.go Normal file
View File

@@ -0,0 +1,69 @@
package log
import (
"errors"
"fmt"
"io"
"log/slog"
"os"
"github.com/prometheus-community/windows_exporter/internal/log/eventlog"
"github.com/prometheus/common/promslog"
"golang.org/x/sys/windows"
)
// AllowedFile is a settable identifier for the output file that the logger can have.
type AllowedFile struct {
s string
w io.Writer
}
func (f *AllowedFile) String() string {
return f.s
}
// Set updates the value of the allowed format.
func (f *AllowedFile) Set(s string) error {
f.s = s
switch s {
case "stdout":
f.w = os.Stdout
case "stderr":
f.w = os.Stderr
case "eventlog":
handle, err := windows.RegisterEventSource(nil, windows.StringToUTF16Ptr("windows_exporter"))
if err != nil {
return fmt.Errorf("failed to open event log: %w", err)
}
f.w = eventlog.NewEventLogWriter(handle)
default:
file, err := os.OpenFile(s, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o200)
if err != nil {
return fmt.Errorf("failed to open log file: %w", err)
}
f.w = file
}
return nil
}
// Config is a struct containing configurable settings for the logger.
type Config struct {
*promslog.Config
File *AllowedFile
}
func New(config *Config) (*slog.Logger, error) {
if config.File == nil {
return nil, errors.New("log file undefined")
}
config.Config.Writer = config.File.w
config.Config.Style = promslog.GoKitStyle
return promslog.New(config.Config), nil
}