Skip to content

Commit

Permalink
docs
Browse files Browse the repository at this point in the history
  • Loading branch information
exposed committed Jan 19, 2024
1 parent e280f57 commit ff02d8d
Show file tree
Hide file tree
Showing 66 changed files with 4,783 additions and 81 deletions.
8 changes: 0 additions & 8 deletions .idea/modules.xml

This file was deleted.

9 changes: 0 additions & 9 deletions .idea/wglog.iml

This file was deleted.

27 changes: 25 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
# wglog
[![Go Reference](https://pkg.go.dev/badge/github.com/point-c/wglog@v0.0.2.svg)](https://godocs.io/github.com/point-c/wglog@v0.0.3)

This is a set of extensions for `Wireguard-go`'s logger.

## Installation

To use wglog in your Go project, install it using `go get`:

```bash
go get github.com/point-c/wglog
```

## Loggers

- `noop`: Non-nil logger that does nothing.
Expand All @@ -13,4 +20,20 @@ This is a set of extensions for `Wireguard-go`'s logger.
- `Errorf`
- `slog.LevelError`
- `multi`: Emits log messages on multiple loggers.
- `async`: Runs the logger funcs in a goroutine.
- `async`: Runs the logger funcs in a goroutine.

## Testing

The package includes tests that demonstrate its functionality. Use Go's testing tools to run the tests:

```bash
go test
```

## Godocs

To regenerate godocs:

```bash
go generate -tags docs ./...
```
12 changes: 10 additions & 2 deletions async.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@ package wglog
import "golang.zx2c4.com/wireguard/device"

// Async will call the underlying logger methods in a goroutine.
// If a field in logger is nil, it will not spawn a goroutine.
func Async(logger *device.Logger) *device.Logger {
return &device.Logger{
Verbosef: func(format string, args ...any) { go logger.Verbosef(format, args...) },
Errorf: func(format string, args ...any) { go logger.Errorf(format, args...) },
Verbosef: async(logger.Verbosef),
Errorf: async(logger.Errorf),
}
}

func async(fn FmtFn) (r func(string, ...any)) {
if fn == nil {
return noopFn
}
return func(s string, a ...any) { go fn(s, a...) }
}
57 changes: 34 additions & 23 deletions async_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,39 @@ import (
)

func TestAsync(t *testing.T) {
var v, va, e, ea bool
var wg sync.WaitGroup
wg.Add(2)
l := Async(&device.Logger{
Verbosef: func(string, ...any) {
t.Helper()
defer wg.Done()
v = true
va = runtime.Callers(3, make([]uintptr, 1)) == 0
},
Errorf: func(string, ...any) {
t.Helper()
defer wg.Done()
e = true
ea = runtime.Callers(3, make([]uintptr, 1)) == 0
},
t.Run("normal", func(t *testing.T) {
var v, va, e, ea bool
var w sync.WaitGroup
w.Add(2)
l := Async(&device.Logger{
Verbosef: func(string, ...any) {
defer w.Done()
v = true
// Check if func was called with `go`
// https://stackoverflow.com/a/56702614
va = runtime.Callers(3, make([]uintptr, 1)) == 0
},
Errorf: func(string, ...any) {
defer w.Done()
e = true
// Check if func was called with `go`
// https://stackoverflow.com/a/56702614
ea = runtime.Callers(3, make([]uintptr, 1)) == 0
},
})
l.Verbosef("")
l.Errorf("")
w.Wait()
require.True(t, v, "failed to call verbose")
require.True(t, va, "failed to call verbose in a goroutine")
require.True(t, e, "failed to call error")
require.True(t, ea, "failed to call error in a goroutine")
})
t.Run("noop", func(t *testing.T) {
l := Async(new(device.Logger))
require.NotNil(t, l.Verbosef)
require.NotNil(t, l.Errorf)
l.Verbosef("")
l.Errorf("")
})
l.Verbosef("")
l.Errorf("")
wg.Wait()
require.True(t, v, "failed to call verbose")
require.True(t, va, "failed to call verbose in a goroutine")
require.True(t, e, "failed to call error")
require.True(t, ea, "failed to call error in a goroutine")
}
8 changes: 8 additions & 0 deletions docs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//go:build docs

package wglog

//go:generate rm -rf docs
//go:generate go run "github.com/johnstarich/go/gopages" -base /$GOPACKAGE -internal -out docs -source-link "https://github.com/point-c/$GOPACKAGE/blob/main/{{.Path}}{{if .Line}}#L{{.Line}}{{end}}"

import _ "github.com/johnstarich/go/gopages"
70 changes: 70 additions & 0 deletions docs/404.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#375EAB">

<title>Page not found - GoPages</title>

<link type="text/css" rel="stylesheet" href="/wglog/lib/godoc/style.css">

<script>window.initFuncs = [];</script>
<script src="/wglog/lib/godoc/jquery.js" defer></script>




<script>var goVersion = "go1.21.6";</script>
<script src="/wglog/lib/godoc/godocs.js" defer></script>

</head>
<body>

<div id='lowframe' style="position: fixed; bottom: 0; left: 0; height: 0; width: 100%; border-top: thin solid grey; background-color: white; overflow: auto;">
...
</div><!-- #lowframe -->

<div id="topbar" class="wide"><div class="container">
<div class="top-heading" id="heading-wide"><a href="/wglog/pkg/github.com/point-c/wglog/">GoPages | Auto-generated docs</a></div>
<div class="top-heading" id="heading-narrow"><a href="/wglog/pkg/github.com/point-c/wglog/">GoPages</a></div>
<a href="#" id="menu-button"><span id="menu-button-arrow">&#9661;</span></a>

</div></div>



<div id="page" class="wide">
<div class="container">


<h1>
Page not found
<span class="text-muted"></span>
</h1>







<div id="nav"></div>


<p>
<span class="alert" style="font-size:120%">
Oops, this page doesn't exist.
</span>
</p>
<p>If something should be here, <a href="https://github.com/JohnStarich/go/issues/new" target="_blank">open an issue</a>.</p>


<div id="footer">
Build version go1.21.6.<br>
</div>

</div><!-- .container -->
</div><!-- #page -->
</body>
</html>
11 changes: 11 additions & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<script>
window.location = "pkg/github.com/point-c/wglog/"
</script>
</head>
<body>
<a href="pkg/github.com/point-c/wglog/">Click here to see this module's documentation.</a>
</body>
</html>
Binary file added docs/lib/godoc/analysis/call-eg.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/lib/godoc/analysis/call3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/lib/godoc/analysis/callers1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/lib/godoc/analysis/callers2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/lib/godoc/analysis/chan1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/lib/godoc/analysis/chan2a.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/lib/godoc/analysis/chan2b.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/lib/godoc/analysis/error1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit ff02d8d

Please sign in to comment.