Skip to content

Commit

Permalink
Fix README.md and add FormattingSink.
Browse files Browse the repository at this point in the history
  • Loading branch information
raldone01 committed Jan 16, 2024
1 parent c961e99 commit 068e1ef
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# gdlogging Godot addon

This is a composable logging addon for version 4 of the [Godot game engine](https://godotengine.org/).

## Usage

```gdscript
Expand Down Expand Up @@ -44,6 +46,7 @@ timer.stop()
* `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

Expand All @@ -57,7 +60,7 @@ timer.stop()

```gdscript
class MyLogRecordFormatter extends Log.LogRecordFormatter:
func format(log_record: Dictionary, raw_message: String) -> String:
func format(log_record: Dictionary) -> String:
var time_unix: float = log_record["time_unix"]
var level: Log.LogLevel = log_record["level"]
var unformatted_message: String = log_record["unformatted_message"]
Expand Down
26 changes: 25 additions & 1 deletion logger.gd
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ class BufferedSink extends LogSink:
_write_bulks_buffered()
_sink.flush_buffer()

## Set to 0 to disable interval flushing.
func set_buffer_flush_interval_msec(p_buffer_flush_interval_msec: int) -> void:
_buffer_flush_interval_usec = p_buffer_flush_interval_msec * 1000

Expand All @@ -197,7 +198,7 @@ class BufferedSink extends LogSink:
return
_buffer_log_records.append_array(p_log_records)
_buffer_formatted_messages.append_array(p_formatted_messages)
var max_wait_exceeded := Time.get_ticks_usec() - _last_buffer_write_out_time_usec > _buffer_flush_interval_usec
var max_wait_exceeded := _buffer_flush_interval_usec != 0 and Time.get_ticks_usec() - _last_buffer_write_out_time_usec > _buffer_flush_interval_usec
if (_buffer_log_records.size() >= _buffer_size) \
or max_wait_exceeded:
_write_bulks_buffered()
Expand Down Expand Up @@ -418,6 +419,29 @@ class MemoryWindowSink extends LogSink:
"log_records": _log_records,
}

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

func _init(p_sink: LogSink, p_log_record_formatter: LogRecordFormatter) -> void:
_sink = p_sink
_log_record_formatter = p_log_record_formatter

func write_bulks(p_log_records: Array[Dictionary], p_formatted_messages: PackedStringArray) -> void:
var formatted_messages := PackedStringArray()
for i in range(p_log_records.size()):
var log_record := p_log_records[i]
var formatted_message := _log_record_formatter.format(log_record)
formatted_messages.append(formatted_message)
_sink.write_bulks(p_log_records, formatted_messages)

func flush_buffer() -> void:
_sink.flush_buffer()

func close() -> void:
flush_buffer()
_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:
var pad_length := p_length - p_string.length()
Expand Down

0 comments on commit 068e1ef

Please sign in to comment.