diff --git a/v3/examples/plugins/main.go b/v3/examples/plugins/main.go index 7b62d0ba..6e2e80e3 100644 --- a/v3/examples/plugins/main.go +++ b/v3/examples/plugins/main.go @@ -5,7 +5,8 @@ import ( "github.com/wailsapp/wails/v3/pkg/application" "github.com/wailsapp/wails/v3/plugins/browser" "github.com/wailsapp/wails/v3/plugins/kvstore" - "log" + "github.com/wailsapp/wails/v3/plugins/log" + "os" "plugin_demo/plugins/hashes" //"plugin_demo/plugins/hashes" ) @@ -24,6 +25,7 @@ func main() { Plugins: map[string]application.Plugin{ "hashes": hashes.NewPlugin(), "browser": browser.NewPlugin(), + "log": log.NewPlugin(), "kvstore": kvstore.NewPlugin(&kvstore.Config{ Filename: "store.json", AutoSave: true, @@ -40,6 +42,7 @@ func main() { err := app.Run() if err != nil { - log.Fatal(err.Error()) + println(err.Error()) + os.Exit(1) } } diff --git a/v3/plugins/log/README.md b/v3/plugins/log/README.md new file mode 100644 index 00000000..ae02ab72 --- /dev/null +++ b/v3/plugins/log/README.md @@ -0,0 +1,38 @@ +# log Plugin + +This example plugin provides a way to generate hashes of strings. + +## Installation + +Add the plugin to the `Plugins` option in the Applications options: + +```go + Plugins: map[string]application.Plugin{ + "log": log.NewPlugin(), + }, +``` + +## Usage + +You can then call the methods from the frontend: + +```js + wails.Plugin("log","All","hello world").then((result) => console.log(result)) +``` + +This method returns a struct with the following fields: + +```typescript + interface Hashes { + MD5: string; + SHA1: string; + SHA256: string; + } +``` + +A TypeScript definition file is provided for this interface. + +## Support + +If you find a bug in this plugin, please raise a ticket [here](https://github.com/plugin/repository). +Please do not contact the Wails team for support. \ No newline at end of file diff --git a/v3/plugins/log/plugin.go b/v3/plugins/log/plugin.go new file mode 100644 index 00000000..794456d0 --- /dev/null +++ b/v3/plugins/log/plugin.go @@ -0,0 +1,138 @@ +package log + +import ( + "fmt" + "github.com/wailsapp/wails/v3/pkg/application" + "io" + "os" +) + +// ---------------- Plugin Setup ---------------- +// This is the main plugin struct. It can be named anything you like. +// It must implement the application.Plugin interface. +// Both the Init() and Shutdown() methods are called synchronously when the app starts and stops. + +type LogLevel int + +const ( + Trace LogLevel = iota + 1 + Debug + Info + Warning + Error + Fatal +) + +type Config struct { + // Where the logs are written to. Defaults to os.Stderr + // If you want to write to a file, use os.OpenFile() + // e.g. os.OpenFile("mylog.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666) + // Closes the writer when the app shuts down + Writer io.WriteCloser + + // The initial log level. Defaults to Debug + Level LogLevel + + // Disables the log level prefixes + DisablePrefix bool + + // Handles errors that occur when writing to the log + ErrorHandler func(err error) +} + +type Plugin struct { + config *Config + app *application.App + level LogLevel +} + +func NewPluginWithConfig(config *Config) *Plugin { + if config.Level == 0 { + config.Level = Debug + } + if config.Writer == nil { + config.Writer = os.Stderr + } + return &Plugin{ + config: config, + } +} + +func NewPlugin() *Plugin { + return NewPluginWithConfig(&Config{}) +} + +// Shutdown is called when the app is shutting down +// You can use this to clean up any resources you have allocated +func (p *Plugin) Shutdown() { + p.config.Writer.Close() +} + +// Name returns the name of the plugin. +// You should use the go module format e.g. github.com/myuser/myplugin +func (p *Plugin) Name() string { + return "github.com/wailsapp/wails/v3/plugins/log" +} + +func (p *Plugin) Init(app *application.App) error { + p.app = app + return nil +} + +// CallableByJS returns a list of methods that can be called from the frontend +func (p *Plugin) CallableByJS() []string { + return []string{ + "Trace", + "Debug", + "Info", + "Warning", + "Error", + "Fatal", + } +} + +// ---------------- Plugin Methods ---------------- +// Plugin methods are just normal Go methods. You can add as many as you like. +// The only requirement is that they are exported (start with a capital letter). +// You can also return any type that is JSON serializable. +// See https://golang.org/pkg/encoding/json/#Marshal for more information. + +func (p *Plugin) write(prefix string, level LogLevel, message string, args ...any) { + if level >= p.config.Level { + if !p.config.DisablePrefix { + message = prefix + " " + message + } + _, err := fmt.Fprintln(p.config.Writer, fmt.Sprintf(message, args...)) + if err != nil && p.config.ErrorHandler != nil { + p.config.ErrorHandler(err) + } + } +} + +func (p *Plugin) Trace(message string, args ...any) { + p.write("[Trace]", Trace, message, args...) +} + +func (p *Plugin) Debug(message string, args ...any) { + p.write("[Debug]", Debug, message, args...) +} + +func (p *Plugin) Info(message string, args ...any) { + p.write("[Info]", Info, message, args...) +} + +func (p *Plugin) Warning(message string, args ...any) { + p.write("[Warning]", Warning, message, args...) +} + +func (p *Plugin) Error(message string, args ...any) { + p.write("[Error]", Error, message, args...) +} + +func (p *Plugin) Fatal(message string, args ...any) { + p.write("[FATAL]", Fatal, message, args...) +} + +func (p *Plugin) SetLevel(newLevel LogLevel) { + p.level = newLevel +} diff --git a/v3/plugins/log/plugin.js b/v3/plugins/log/plugin.js new file mode 100644 index 00000000..a5e95254 --- /dev/null +++ b/v3/plugins/log/plugin.js @@ -0,0 +1,64 @@ +// plugin.js +// This file should contain helper functions for the that can be used by the frontend. +// Below are examples of how to use JSDoc to define the Hashes struct and the exported functions. + +/** + * Log at the Trace level. + * @param input {string} - The message in printf format. + * @param args {...any} - The arguments for the log message. + * @returns {Promise} + */ +export function Trace(input, ...args) { + return wails.Plugin("log", "Trace", input, ...args); +} + +/** + * Log at the Debug level. + * @param input {string} - The message in printf format. + * @param args {...any} - The arguments for the log message. + * @returns {Promise} + */ + +export function Debug(input, ...args) { + return wails.Plugin("log", "Debug", input, ...args); +} + +/** + * Log at the Info level. + * @param input {string} - The message in printf format. + * @param args {...any} - The arguments for the log message. + * @returns {Promise} + */ +export function Info(input, ...args) { + return wails.Plugin("log", "Info", input, ...args); +} + +/** + * Log at the Warning level. + * @param input {string} - The message in printf format. + * @param args {...any} - The arguments for the log message. + * @returns {Promise} + */ +export function Warning(input, ...args) { + return wails.Plugin("log", "Warning", input, ...args); +} + +/** + * Log at the Error level. + * @param input {string} - The message in printf format. + * @param args {...any} - The arguments for the log message. + * @returns {Promise} + */ +export function Error(input, ...args) { + return wails.Plugin("log", "Error", input, ...args); +} + +/** + * Log at the Fatal level. + * @param input {string} - The message in printf format. + * @param args {...any} - The arguments for the log message. + * @returns {Promise} + */ +export function Fatal(input, ...args) { + return wails.Plugin("log", "Fatal", input, ...args); +} \ No newline at end of file diff --git a/v3/plugins/log/plugin.toml b/v3/plugins/log/plugin.toml new file mode 100644 index 00000000..0315468d --- /dev/null +++ b/v3/plugins/log/plugin.toml @@ -0,0 +1,11 @@ +# This is the plugin definition file for the "log" plugin. + +Name = "log" +Description = "A basic logger" +Author = "Lea Anthony" +Version = "v1.0.0" +Website = "https://wails.io" +Repository = "https://github.com/wailsapp/wails/v3/plugins/log" +License = "MIT" + +