5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-03 00:41:59 +08:00

Support Print logging

This commit is contained in:
Lea Anthony 2020-10-09 15:13:45 +11:00
parent 9f62a08cd2
commit 228285f693
No known key found for this signature in database
GPG Key ID: 33DAF7BB90A58405
11 changed files with 69 additions and 29 deletions

View File

@ -2,12 +2,13 @@ package logger
import (
"os"
"github.com/wailsapp/wails/v2/pkg/logger"
)
// Logger is a utlility to log messages to a number of destinations
type Logger struct {
output logger.Logger
output logger.Logger
logLevel uint8
showLevelInLog bool
}
@ -18,7 +19,7 @@ func New(output logger.Logger) *Logger {
result := &Logger{
logLevel: INFO,
showLevelInLog: true,
output: output,
output: output,
}
return result
}
@ -42,7 +43,7 @@ func (l *Logger) SetLogLevel(level uint8) {
// Writeln writes directly to the output with no log level
// Appends a carriage return to the message
func (l *Logger) Writeln(message string) error {
return l.output.Print(message+"\n")
return l.output.Print(message + "\n")
}
// Write writes directly to the output with no log level
@ -50,9 +51,18 @@ func (l *Logger) Write(message string) error {
return l.output.Print(message)
}
// Print writes directly to the output with no log level
// Appends a carriage return to the message
func (l *Logger) Print(message string) error {
return l.Write(message)
}
// Trace level logging. Works like Sprintf.
func (l *Logger) Trace(format string, args ...interface{}) error {
return l.output.Trace(format, args...)
if l.logLevel <= TRACE {
return l.output.Trace(format, args...)
}
return nil
}
// // CustomTrace returns a custom Logging function that will insert the given name before the message
@ -65,23 +75,34 @@ func (l *Logger) Trace(format string, args ...interface{}) error {
// Debug level logging. Works like Sprintf.
func (l *Logger) Debug(format string, args ...interface{}) error {
return l.output.Debug(format, args...)
if l.logLevel <= DEBUG {
return l.output.Debug(format, args...)
}
return nil
}
// Info level logging. Works like Sprintf.
func (l *Logger) Info(format string, args ...interface{}) error {
return l.output.Info(format, args...)
if l.logLevel <= INFO {
return l.output.Info(format, args...)
}
return nil
}
// Warning level logging. Works like Sprintf.
func (l *Logger) Warning(format string, args ...interface{}) error {
return l.output.Warning(format, args...)
if l.logLevel <= WARNING {
return l.output.Warning(format, args...)
}
return nil
}
// Error level logging. Works like Sprintf.
func (l *Logger) Error(format string, args ...interface{}) error {
return l.output.Error(format, args...)
if l.logLevel <= ERROR {
return l.output.Error(format, args...)
}
return nil
}
// Fatal level logging. Works like Sprintf.

View File

@ -3,6 +3,7 @@ package message
import "fmt"
var logMessageMap = map[byte]string{
'P': "log:print",
'T': "log:trace",
'D': "log:debug",
'I': "log:info",
@ -13,7 +14,7 @@ var logMessageMap = map[byte]string{
// logMessageParser does what it says on the tin!
func logMessageParser(message string) (*parsedMessage, error) {
// Sanity check: Log messages must be at least 2 bytes
if len(message) < 2 {
return nil, fmt.Errorf("log message was an invalid length")

View File

@ -6,6 +6,7 @@ import (
// Log defines all Log related operations
type Log interface {
Print(message string)
Trace(message string)
Debug(message string)
Info(message string)
@ -25,6 +26,11 @@ func newLog(bus *servicebus.ServiceBus) Log {
}
}
// Print prints a Print level message
func (r *log) Print(message string) {
r.bus.Publish("log:print", message)
}
// Trace prints a Trace level message
func (r *log) Trace(message string) {
r.bus.Publish("log:trace", message)

View File

@ -57,6 +57,8 @@ func (l *Log) Start() error {
case logMessage := <-l.logChannel:
logType := strings.TrimPrefix(logMessage.Topic(), "log:")
switch logType {
case "print":
l.logger.Print(logMessage.Data().(string))
case "trace":
l.logger.Trace(logMessage.Data().(string))
case "debug":

View File

@ -124,9 +124,9 @@
}
},
"@wails/runtime": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/@wails/runtime/-/runtime-1.0.0.tgz",
"integrity": "sha512-YKL9k4ZRvltnN9Io4gM03u7bk7VX9YjDLu8BdkaHws+3Tt0H+NKt/XnMTo9beUZdPDYnQOgAo5CTxR5UP+n/xA==",
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/@wails/runtime/-/runtime-1.0.1.tgz",
"integrity": "sha512-zAhPm1eTZ7f7IsE2II0HoDvCtnttj7ah3gzCTpoAd9zltuoao6aU7m1RMsuqLvd/zvdsA4bKywmVjsa2O4zJpQ==",
"dev": true
},
"alphanum-sort": {
@ -965,9 +965,9 @@
}
},
"halfmoon": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/halfmoon/-/halfmoon-1.1.0.tgz",
"integrity": "sha512-KgZvQLSvSbu+6foGatWcQHtlkCbmDqn43I7F/DCvdSomDrNCz0Vv3qD4JvHjBbx2IhN9B20u2MNE9yyZPOEQfQ==",
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/halfmoon/-/halfmoon-1.1.1.tgz",
"integrity": "sha512-w/ydT3CixxxxdJP4a3sqSzUnCJFmhvcqE2vQOIsWCfevpqanKlbGQNUwKJH0nKvo13M89eetM3R0gsTOfNLGHA==",
"dev": true
},
"handlebars": {
@ -2956,9 +2956,9 @@
"dev": true
},
"rollup": {
"version": "2.28.2",
"resolved": "https://registry.npmjs.org/rollup/-/rollup-2.28.2.tgz",
"integrity": "sha512-8txbsFBFLmm9Xdt4ByTOGa9Muonmc8MfNjnGAR8U8scJlF1ZW7AgNZa7aqBXaKtlvnYP/ab++fQIq9dB9NWUbg==",
"version": "2.29.0",
"resolved": "https://registry.npmjs.org/rollup/-/rollup-2.29.0.tgz",
"integrity": "sha512-gtU0sjxMpsVlpuAf4QXienPmUAhd6Kc7owQ4f5lypoxBW18fw2UNYZ4NssLGsri6WhUZkE/Ts3EMRebN+gNLiQ==",
"dev": true,
"requires": {
"fsevents": "~2.1.2"
@ -3403,9 +3403,9 @@
}
},
"svelte-preprocess": {
"version": "4.3.2",
"resolved": "https://registry.npmjs.org/svelte-preprocess/-/svelte-preprocess-4.3.2.tgz",
"integrity": "sha512-CmIsCr62y34qGS10/SC1l1VkmX0kZR6wncbGgRJ1qJftLDMEaazC3bWqoqAlrqyQFvpO0+xb44GQm4RKi/9sLQ==",
"version": "4.5.1",
"resolved": "https://registry.npmjs.org/svelte-preprocess/-/svelte-preprocess-4.5.1.tgz",
"integrity": "sha512-fZiLMg+mJzp5y4bsvBtl6wE1WCp+s5L87BoKMONGLXk8HSZD5HuRJzxhM0yhM9LHF0jP5kYG22P2Vc/vrv4I0A==",
"dev": true,
"requires": {
"@types/pug": "^2.0.4",

View File

@ -10,12 +10,12 @@
"devDependencies": {
"@rollup/plugin-commonjs": "^11.0.0",
"@rollup/plugin-node-resolve": "^7.0.0",
"@wails/runtime": "^1.0.0",
"@wails/runtime": "^1.0.1",
"focus-visible": "^5.0.2",
"halfmoon": "^1.1.0",
"halfmoon": "^1.1.1",
"postcss": "^8.1.1",
"postcss-import": "^12.0.1",
"rollup": "^2.0.0",
"rollup": "^2.29.0",
"rollup-plugin-livereload": "^1.0.0",
"rollup-plugin-postcss": "^3.1.8",
"rollup-plugin-string": "^3.0.0",
@ -24,7 +24,7 @@
"sirv-cli": "^0.4.4",
"svelte": "^3.0.0",
"svelte-highlight": "^0.6.2",
"svelte-preprocess": "^4.3.2"
"svelte-preprocess": "^4.5.1"
},
"dependencies": {}
}

View File

@ -5,11 +5,11 @@
import jsCode from './code.jsx';
import goCode from './code.go';
var loglevel = 'Trace';
var message = '';
var isJs = false;
var options = ["Trace", "Debug", "Info", "Warning", "Error", "Fatal"];
var options = ["Print", "Trace", "Debug", "Info", "Warning", "Error", "Fatal"];
var loglevel = options[0];
$: lang = isJs ? 'Javascript' : 'Go';
@ -33,7 +33,7 @@
<ul class="list">
{#each options as option}
<li>{options}</li>
<li>{option}</li>
{/each}
</ul>
All methods will log to the console and <code>Fatal</code> will also exit the program.

View File

@ -8,6 +8,8 @@ type MyStruct struct {
func (l *MyStruct) WailsInit(runtime *wails.Runtime) error {
runtime.Log.Print(message)
runtime.Log.Trace(message)
runtime.Log.Debug(message)
runtime.Log.Info(message)
runtime.Log.Warning(message)

View File

@ -3,6 +3,7 @@ import { Log } from '@wails/runtime';
function doSomeOperation() {
// Do things
let value = doSomething();
Log.Print("A raw message");
Log.Trace("I got: " + value);
Log.Debug("A debug message");
Log.Info("An Info message");

View File

@ -16,6 +16,11 @@ func (l *Logger) WailsInit(runtime *wails.Runtime) error {
return nil
}
// Print will log the given message
func (l *Logger) Print(message string) {
l.runtime.Log.Print(message)
}
// Trace will log the given message
func (l *Logger) Trace(message string) {
l.runtime.Log.Trace(message)

View File

@ -2,6 +2,7 @@ package main
import (
wails "github.com/wailsapp/wails/v2"
"github.com/wailsapp/wails/v2/pkg/logger"
"github.com/wailsapp/wails/v2/pkg/options"
"github.com/wailsapp/wails/v2/pkg/options/mac"
)
@ -22,6 +23,7 @@ func main() {
WindowBackgroundIsTranslucent: true,
TitleBar: mac.TitleBarHiddenInset(),
},
LogLevel: logger.INFO,
})
app.Bind(&Logger{})