Skip to content

Commit

Permalink
Minor refactor.
Browse files Browse the repository at this point in the history
  • Loading branch information
raldone01 committed Feb 29, 2024
1 parent 241e4d6 commit cddf3dd
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 31 deletions.
49 changes: 36 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ var console_sink := Log.ConsoleSink.new()
Log.add_sink(console_sink)
var dir_sink := Log.DirSink.new("mylog", "res://logs", 4042)
var buffered_sink := Log.BufferedSink.new(dir_sink, 500)
var buffered_pipe := Log.BufferedPipe.new(dir_sink, 500)
# Don't log TRACE messages to the log file
var file_filtered_sink := Log.FilteringSink.new(buffered_sink, Log.DEBUG)
Log.add_sink(file_filtered_sink)
var file_filtered_pipe := Log.FilteringPipe.new(buffered_pipe, Log.DEBUG)
Log.add_sink(file_filtered_pipe)
Log.debug("Hello World")
# [24/Jan/14 13:28:03] [ Global] [DBG] Hello World
Expand All @@ -41,24 +41,47 @@ timer.stop()
# [24/Jan/14 13:28:06] [ MyClass] [INF] MyTimer exceeded threshold of 1000 msec: took 1.111750 seconds.
```

## Log Levels

* `Log.TRACE`: Prints the call site in debug builds. The stack depth can be configured per call.
* `Log.DEBUG`: Debug messages
* `Log.INFO`: Informational messages
* `Log.WARN`: Warnings
* `Log.ERROR`: Errors

## Pipes

* `FilteringPipe`: Filters messages by level and forwards them to another sink.
* `BroadcastPipe`: Broadcasts messages to multiple sinks.
* `BufferedPipe`: Buffers messages and forwards them to another sink.
* `FormattingPipe`: Formats messages and forwards them to another sink.

## Sinks

* `FilteringSink`: Filters messages by level and forwards them to another sink.
* `BroadcastSink`: Broadcasts messages to multiple sinks.
* `BufferedSink`: Buffers messages and forwards them to another sink.
* `ConsoleSink`: Outputs messages to the console.
* `DirSink`: Outputs messages to log files and rotates them. Uses a thread for file io.
* `Logger`: Can receive messages from other Loggers and Sinks. Users will call the log functions which format the message.
* `MemoryWindowSink`: Keeps `n` log messages in memory. Can be used to display the last `n` messages in a GUI.
* `FormattingSink`: Formats messages and forwards them to another sink.

## Log Levels
## Custom Sinks/Pipes

* `Log.TRACE`: Prints the call site in debug builds. The stack depth can be configured per call.
* `Log.DEBUG`: Debug messages
* `Log.INFO`: Informational messages
* `Log.WARN`: Warnings
* `Log.ERROR`: Errors
Classes ending in `Pipe` are sinks that forward messages to another sink.
Classes ending in `Sink` write messages to a destination.

To create a custom sink extend the `Log.LogSink` and implement the methods.

```gdscript
class MyCustomSink extends Log.LogSink:
## Write many log records to the sink
func write_bulks(p_log_records: Array[Dictionary], p_formatted_messages: PackedStringArray) -> void:
pass
## Flushes the buffer of the sink if it has one.
func flush_buffer() -> void:
pass
## Cleans up resources used by the sink.
func close() -> void:
pass
```

## Custom Formatters

Expand Down
36 changes: 18 additions & 18 deletions funcs/logger.gd
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ static func format_month_short(p_month: int) -> String:
return month_names[p_month - 1]

## Formats the session id number to a string truncated to 4 digits.
static func format_session_id(p_session_id: int) -> String:
static func format_session_id(p_session_id: int) -> String:
return "%04d" % p_session_id

## Formats a unix timestamp to a string.
Expand Down Expand Up @@ -105,7 +105,7 @@ class LogSink extends RefCounted:
func close() -> void:
pass

class FilteringSink extends LogSink:
class FilteringPipe extends LogSink:
var _sink: LogSink
var _level: LogLevel

Expand All @@ -132,7 +132,7 @@ class FilteringSink extends LogSink:
flush_buffer()
_sink.close()

class BroadcastSink extends LogSink:
class BroadcastPipe extends LogSink:
var _sinks: Array[LogSink] = []

func add_sink(p_sink: LogSink) -> void:
Expand Down Expand Up @@ -171,7 +171,7 @@ class BufferedSink extends LogSink:
## [param buffer_size]: The size of the buffer. If 0, the buffer will be disabled.
##
## The buffer size is the number of messages that will be buffered before being flushed to the sink.
func _init(p_sink: LogSink, p_buffer_size: int = 42) -> void:
func _init(p_sink: LogSink, p_buffer_size: int=42) -> void:
if p_buffer_size < 0:
p_buffer_size = 0
Log._logger_direct_console.warning("BufferedSink: Buffer size must be equal or greater than 0.")
Expand Down Expand Up @@ -248,7 +248,7 @@ class DirSink extends LogSink:
var _io_thread_exit := false
var _io_thread_flush_buffer := false

func _init(p_log_name: String, p_dir_path: String, p_max_file_size: int = 4042, p_max_file_count: int = 10) -> void:
func _init(p_log_name: String, p_dir_path: String, p_max_file_size: int=4042, p_max_file_count: int=10) -> void:
_log_name = p_log_name
if p_dir_path.begins_with("user://") or p_dir_path.begins_with("res://"):
p_dir_path = ProjectSettings.globalize_path(p_dir_path)
Expand Down Expand Up @@ -319,7 +319,7 @@ class DirSink extends LogSink:
return
var files_to_delete := file_count - _max_file_count
for i in range(files_to_delete):
var filename := last_dir_listing[-1]
var filename := last_dir_listing[- 1]
var path := _dir_path + "/" + filename
# OS.move_to_trash(path) # completely blocks the main thread
var dir := DirAccess.open(".")
Expand Down Expand Up @@ -400,7 +400,7 @@ class MemoryWindowSink extends LogSink:
var _formatted_messages := PackedStringArray()
var _log_records: Array[Dictionary] = []

func _init(p_max_lines: int = 100) -> void:
func _init(p_max_lines: int=100) -> void:
_max_lines = p_max_lines

func write_bulks(p_log_records: Array[Dictionary], p_formatted_messages: PackedStringArray) -> void:
Expand All @@ -419,7 +419,7 @@ class MemoryWindowSink extends LogSink:
"log_records": _log_records,
}

class FormattingSink extends LogSink:
class FormattingPipe extends LogSink:
var _sink: LogSink
var _log_record_formatter: LogRecordFormatter

Expand All @@ -443,7 +443,7 @@ class FormattingSink extends LogSink:
_sink.close()

## Left pads a string with a character to a given length.
static func pad_string(p_string: String, p_length: int, p_pad_char: String = " ") -> String:
static func pad_string(p_string: String, p_length: int, p_pad_char: String=" ") -> String:
var pad_length := p_length - p_string.length()
if pad_length <= 0:
return p_string
Expand Down Expand Up @@ -488,9 +488,9 @@ class Logger extends LogSink:

func _init(
p_tag: String,
p_level: LogLevel = LogLevel.TRACE,
p_log_record_formatter: LogRecordFormatter = Log._global_logger._log_record_formatter,
p_sink: LogSink = Log._global_logger
p_level: LogLevel=LogLevel.TRACE,
p_log_record_formatter: LogRecordFormatter=Log._global_logger._log_record_formatter,
p_sink: LogSink=Log._global_logger
) -> void:
_tag = p_tag
_log_record_formatter = p_log_record_formatter
Expand All @@ -516,7 +516,7 @@ class Logger extends LogSink:
func set_log_record_formatter(p_log_record_formatter: LogRecordFormatter) -> void:
_log_record_formatter = p_log_record_formatter

func log(p_level: LogLevel, p_message: String, p_log_record: Dictionary = {}) -> void:
func log(p_level: LogLevel, p_message: String, p_log_record: Dictionary={}) -> void:
if p_level < _level:
return
p_log_record["level"] = p_level
Expand All @@ -526,7 +526,7 @@ class Logger extends LogSink:
var formatted_message := _log_record_formatter.format(p_log_record)
_sink.write_bulks([p_log_record], [formatted_message])

func trace(p_message: String, p_stack_depth: int = 1, p_stack_hint: int = 1) -> void:
func trace(p_message: String, p_stack_depth: int=1, p_stack_hint: int=1) -> void:
var log_record := {}
if OS.is_debug_build():
var stack: Array[Dictionary] = get_stack()
Expand Down Expand Up @@ -559,7 +559,7 @@ class LogTimer:
var _message: String
var _level: LogLevel = LogLevel.INFO

func _init(p_message: String, p_threshold_msec: int = 0, p_logger: Logger = Log._global_logger) -> void:
func _init(p_message: String, p_threshold_msec: int=0, p_logger: Logger=Log._global_logger) -> void:
_logger = p_logger
_message = p_message
_threshold_msec = p_threshold_msec
Expand All @@ -586,12 +586,12 @@ class LogTimer:
if _threshold_msec < elapsed_time_usec / 1000:
_logger.log(_level, "%s exceeded threshold of %d msec: took %f seconds." % [_message, _threshold_msec, elapsed_time_sec])

var _global_broadcast_sink: BroadcastSink
var _global_broadcast_sink: BroadcastPipe
var _global_logger: Logger
var _logger_direct_console: Logger

func _init() -> void:
_global_broadcast_sink = BroadcastSink.new()
_global_broadcast_sink = BroadcastPipe.new()
var log_formatter := LogRecordFormatter.new()
_global_logger = Logger.new("Global", LogLevel.TRACE, log_formatter, _global_broadcast_sink)
_logger_direct_console = Logger.new("gdlogging", LogLevel.TRACE, log_formatter, ConsoleSink.new())
Expand All @@ -602,7 +602,7 @@ func _exit_tree() -> void:
flush_buffer()
_global_logger.close()

func trace(p_message: String, p_stack_depth: int = 1, p_stack_hint: int = 2) -> void:
func trace(p_message: String, p_stack_depth: int=1, p_stack_hint: int=2) -> void:
_global_logger.trace(p_message, p_stack_depth, p_stack_hint)

func debug(p_message: String) -> void:
Expand Down

0 comments on commit cddf3dd

Please sign in to comment.