Skip to content

Commit 38c07de

Browse files
committed
1.2 Fixed usage of instantiated squid logger and printing conditions. Added saved file location log. Improved documentation and added full API
1 parent 878e956 commit 38c07de

File tree

4 files changed

+260
-69
lines changed

4 files changed

+260
-69
lines changed

README.md

Lines changed: 199 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,49 @@ Copyright 2024-2025 Paweł Jarosz
1212

1313
## Defold dependency:
1414

15-
You can add Squid as a dependency to Defold. Open your `game.project` file and add the following link as an entry in the `Dependencies` under the `Project` section. Current version is 1.1:
15+
You can add Squid as a dependency to Defold. Open your `game.project` file and add the following link as an entry in the `Dependencies` under the `Project` section. Current version is 1.2:
1616

17-
`https://github.com/paweljarosz/squid/archive/refs/tags/1.1.zip`
17+
`https://github.com/paweljarosz/squid/archive/refs/tags/1.2.zip`
1818

19-
## Quick Reference:
20-
21-
Squid offers simple static API, check examples:
19+
---
20+
# Quick Reference:
2221

22+
If squid is initialized with init() it will handle errors and crashes automatically too:
2323
```lua
24-
-- If squid is initialized with init() it will handle errors and crashes automatically too
2524
squid.init()
25+
```
2626

27-
-- Define new tag strings and enable/disable them:
27+
Define new tag strings and enable/disable them:
28+
```lua
2829
squid.set_allowed("main", true)
30+
```
2931

30-
-- Use static logging functions with or without optional non-string data and/or tag string:
32+
Use static logging functions with or without optional non-string data and/or tag string:
33+
```lua
3134
squid.trace("My Trace Message") -- no data and no tag, uses default tag ("none")
3235
squid.debug("My Debug Message ", { my_test_data = 1 }) -- with optional non-string data (default tag used)
3336
squid.info( "My Info Message ", "main") -- with string data only (used as tag too)
3437
squid.warn( "My Warning Message", "Hello World", "main") -- with string data and tag ("main" tag is used as tag here)
3538
squid.error("My Error Message ", vmath.vector3(1), "main") -- with non-string data and tag
39+
squid.log("My Other VIP Message", 6) -- Or generic logging function with own logging level
40+
```
3641

37-
-- Or generic logging function with own logging level:
38-
squid.log("My Other Message", squid.DEBUG)
42+
It is ok to print directly numbers or other data too if they can be concatenated to string:
43+
```lua
44+
squid.warn(1.0)
45+
squid.warn(vmath.vector3(1))
46+
squid.warn(msg.url())
47+
squid.warn(type(1))
48+
squid.warn(hash "test")
49+
```
3950

40-
-- You can explicitly save any unsaved buffered logs to a file at any time:
41-
--(logs are saved automatically anyway, in batch, every X logs if configured in game.project)
51+
You can explicitly save any unsaved buffered logs to a file at any time (logs are saved automatically anyway, in batch, every X logs if configured in game.project):
52+
```lua
4253
squid.save_logs()
54+
```
4355

44-
-- If squid.final() is called (in final() preferably) it checks for crash dumps and saves all unsaved buffered logs
56+
If squid.final() is called (in final() preferably) it checks for crash dumps and saves all unsaved buffered logs:
57+
```lua
4558
squid.final()
4659
```
4760

@@ -60,15 +73,15 @@ For example (tables are always pretty printed):
6073
```lua
6174
TRACE: [none]: [16:05:06] main/main.script:13: My Trace Message
6275
DEBUG: [none]: [16:05:06] main/main.script:14: My Debug Message
63-
Data:
64-
{
65-
my_test_data = 1,
66-
}
6776
INFO: [main]: [16:05:06] main/main.script:15: My Info Message
6877
WARNING:[main]: [16:05:06] main/main.script:16: My Warning Message
6978
ERROR: [main]: [16:05:06] main/main.script:17: My Error Message
7079
Data: vmath.vector3(1, 1, 1)
7180
DEBUG: [none]: [16:05:06] main/main.script:19: My Other Message
81+
Data:
82+
{
83+
my_test_data = 1,
84+
}
7285
```
7386

7487
Additionally, the different log level messages are **colored** in Defold console:
@@ -98,27 +111,44 @@ Squid can be conveniently used as internal logger module for various other Defol
98111
* [Defold Saver](https://github.com/Insality/defold-saver) by Insality
99112
* [Defold Event](https://github.com/Insality/defold-event) by Insality
100113

114+
Create new instance with optional tag assigned (`none` by default) initially allowed or not:
101115
```lua
102-
-- Create new instance with optional tag assigned (`none` by default) initially allowed or not:
103116
local is_allowed = true
104117
self.player_logger = squid.new("player", is_allowed)
118+
```
105119

106-
-- Use all API function with the created instance:
120+
Use all API function with the created instance:
121+
```lua
107122
self.player_logger:info("Logger with 'player' tag")
108123
```
109124

110-
## Configuration
125+
If you want to use Squid with Pigeon use:
126+
```lua
127+
pigeon.set_dependency_module_log( squid )
128+
```
129+
130+
If you want to use Squid with Saver or Event use (they works with instantiated loggers):
131+
```lua
132+
saver.set_logger( squid.new("saver", true) )
133+
event.set_logger( squid.new("event", true) )
134+
```
135+
136+
---
137+
138+
# Configuration
111139

112140
Squid configuration can be set with user's custom one with `squid.set_config(your_config)`.
113141
It has to be compatible with SquidConfig (you can use Lua Language Server to check it too).
114142
You can get current configuration using `squid.get_config()`.
115143

144+
Get current configuration:
116145
```lua
117-
-- Get current configuration:
118146
local my_config = squid.get_config()
119-
my_config.is_printing = false
147+
```
120148

121-
-- Set new configuration:
149+
Set new configuration:
150+
```lua
151+
my_config.is_printing = false
122152
squid.set_config(my_config)
123153
```
124154

@@ -145,6 +175,8 @@ max_data_depth = 5
145175

146176
It can be then defined in `game.project` editor in Defold.
147177

178+
### Configuration
179+
148180
| Entry | Default | Type | Description |
149181
|-|-|-|-|
150182
| **app_catalog** | `squid_app_catalog` | *[string]* | Name of the catalog where file with logs is saved. Parent directory is the save file directory given by Defold sys API. It is operating system specific and is typically located under the user's home directory .e.g. `%appdata%` for Windows. For Linux it is aditionally prefixed with `config/`, so usually `~/config/`. For HTML5 it is only used as a prefix. |
@@ -162,30 +194,167 @@ It can be then defined in `game.project` editor in Defold.
162194
| **max_data_length** | 1000 | *[integer]* | Maximum length of a data string |
163195
| **max_data_depth** | 5 | *[integer]* | Maximum depth of a data table |
164196

197+
Note:
198+
165199
*[0/1]* might be replaced with boolean checkboxes when [it will be possible in Defold with this issue solved](https://github.com/defold/defold/issues/9981).
166200

167-
## Thanks
201+
---
202+
203+
# API
204+
205+
| Public Member | Type | Description | Purpose |
206+
|-|-|-|-|
207+
| Squid.TRACE | integer | (1) trace logging level constant | Read-only |
208+
| Squid.DEBUG | integer | (2) debug logging level constant | Read-only |
209+
| Squid.INFO | integer | (3) info logging level constant | Read-only |
210+
| Squid.WARN | integer | (4) warn logging level constant | Read-only |
211+
| Squid.ERROR | integer | (5) error logging level constant | Read-only |
212+
| Squid.ALLOWLIST | table | Public list of allowed tags (pairs tag[string] - is_allowed[boolean]) | Can be modified directly of via .set_allowed() |
213+
214+
## Static Functions
215+
216+
Sign ? after name marks optional parameters:
217+
218+
---
219+
### Squid.init()
220+
Initialize Squid for error and crash handling and logging
221+
222+
---
223+
### Squid.set_allowed(tag, is_allowed)
224+
Set if logs should be saved to file
225+
| | name | type | description |
226+
|-|-|-|-|
227+
| param | tag | string | Tag to change
228+
| param | is_allowed | boolean | True if tag should be logged, false otherwise
229+
230+
---
231+
### Squid.trace(message, data_or_tag, tag)
232+
Log TRACE level message with optional data and tag
233+
| | name | type | description |
234+
|-|-|-|-|
235+
| param | message | string|number | Message to log
236+
| param | data_or_tag? | any | Optional non-string data to log or tag if string provided
237+
| param | tag? | string | Optional tag string, if provided, used instead of `data_or_tag` string as a matter of priority
238+
239+
---
240+
### Squid.debug(message, data_or_tag, tag)
241+
Log DEBUG level message with optional data and tag
242+
| | name | type | description |
243+
|-|-|-|-|
244+
| param | message | string|number | Message to log
245+
| param | data_or_tag? | any | Optional non-string data to log or tag if string provided
246+
| param | tag? | string | Optional tag string, if provided, used instead of `data_or_tag` string as a matter of priority
247+
248+
---
249+
### Squid.info(message, data_or_tag, tag)
250+
Log INFO level message with optional data and tag
251+
| | name | type | description |
252+
|-|-|-|-|
253+
| param | message | string|number | Message to log
254+
| param | data_or_tag? | any | Optional non-string data to log or tag if string provided
255+
| param | tag? | string | Optional tag string, if provided, used instead of `data_or_tag` string as a matter of priority
256+
257+
---
258+
### Squid.warn(message, data_or_tag, tag)
259+
Log WARN level message with optional data and tag
260+
| | name | type | description |
261+
|-|-|-|-|
262+
| param | message | string|number | Message to log
263+
| param | data_or_tag? | any | Optional non-string data to log or tag if string provided
264+
| param | tag? | string | Optional tag string, if provided, used instead of `data_or_tag` string as a matter of priority
265+
266+
---
267+
### Squid.error(message, data_or_tag, tag)
268+
Log ERROR level message with optional data and tag
269+
| | name | type | description |
270+
|-|-|-|-|
271+
| param | message | string|number | Message to log
272+
| param | data_or_tag? | any | Optional non-string data to log or tag if string provided
273+
| param | tag? | string | Optional tag string, if provided, used instead of `data_or_tag` string as a matter of priority
274+
275+
---
276+
### Squid.log(message, level, data_or_tag, tag)
277+
---Log message with provided level, message, data and tag
278+
| | name | type | description |
279+
|-|-|-|-|
280+
| param | message | string|number | Message to log
281+
| param | level? | integer | Log level (if not provided, using Squid.INFO by default)
282+
| param | data_or_tag? | any | Optional non-string data to log or tag if string provided
283+
| param | tag? | string | Optional tag string, if provided, used instead of `data_or_tag` string as a matter of priority
284+
285+
---
286+
### Squid.save_logs()
287+
Explicitly save buffer of unsaved logs to a file
288+
| | name | type | description |
289+
|-|-|-|-|
290+
| return | | boolean | True if saved logs succesfully, false otherwise
291+
292+
---
293+
### Squid.set_error_callback(callback)
294+
Explicitly save buffer of unsaved logs to a file
295+
| | name | type | description |
296+
|-|-|-|-|
297+
| param | callback | fun(source: string, message: string, traceback: string) | Error callback function
298+
299+
---
300+
### Squid.final()
301+
Finalize Squid logging - check for crashes and saved all unsaved buffered logs
302+
303+
---
304+
### Squid.get_config()
305+
Get Squid configuration
306+
| | name | type | description |
307+
|-|-|-|-|
308+
| return | | SquidConfig | User configuration table compatible with SquidConfig
309+
310+
---
311+
### Squid.set_config(config)
312+
Set and use user configuration
313+
| | name | type | description |
314+
|-|-|-|-|
315+
| param | config | SquidConfig | User configuration table compatible with SquidConfig
316+
317+
---
318+
### Squid.new(tag, is_allowed)
319+
Create a new instance of the Squid logger
320+
| | name | type | description |
321+
|-|-|-|-|
322+
| param | tag? | string | Optional tag used for all logs when invoked within this instance
323+
| param | is_allowed? | boolean | Optional flag to set if instance's tag is allowed to be logged initially
324+
325+
---
326+
327+
# Thanks
168328

169329
Squid is heavily inspired by [Log](https://github.com/subsoap/log) and [Err](https://github.com/subsoap/err) by Subsoap and [Defold Log](https://github.com/Insality/defold-log) by Insality and uses and iterates over some of their solutions. Squid tries to be compatible with both APIs for easier replacements.
170330

171331
If you like this module, you can show appreciation by supporting them. Insality is running [Github Sponsors](https://github.com/sponsors/insality), Ko-Fi, etc and [Subsoap create awesome games](https://www.subsoap.com/).
172332

173333
You can also support me here via [Github Sponsors](https://github.com/sponsors/paweljarosz), [Ko-Fi](https://ko-fi.com/witchcrafter) or [Patreon](https://www.patreon.com/witchcrafter_rpg) ^^
174334

175-
## Changelog
335+
# Changelog
176336

177-
#### 1.0
337+
### 1.0
178338
* First public version release.
179339

180-
#### 1.1
340+
### 1.1
181341
* Removed Immutable dependency.
182342
* Changed final message about no crashes to more friendly:
183-
(```INFO: [squid]: [14:47:48] squid/squid.lua:122: No crashes, no dump found.```)
343+
(e.g. ```INFO: [squid]: [14:47:48] squid/squid.lua:122: No crashes, no dump found.```)
184344
* Added functions to get and set new squid configuration.
185345
* Improved documentation.
186346

347+
### 1.2
348+
* Fixed code address nesting level when using instantiated squid logger.
349+
* Improved documentation and example script, added API documentation.
350+
* Added message when saving logs and crashes with location of the saved file:
351+
(e.g. ```INFO: [squid]: [14:47:48] squid/squid.lua:122: Succesfully saved at:
352+
Data: /home/pawel/snap/steam/common/.config/squid_app_catalog/squid_log_file_2025-01-20_22_32.txt```)
353+
* Added default level to plain squid.log() function set as squid.INFO when no level is provided by user.
354+
* Fixed Lua annotations when using instantiated squid logger.
355+
* Fixed printing condition not working issue.
187356

188-
## License
357+
# License
189358

190359
MIT
191360

@@ -195,4 +364,4 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of
195364

196365
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
197366

198-
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
367+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

main/main.script

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ function init(self)
1818
-- Or generic logging function with own logging level:
1919
squid.log("My Other Message", squid.DEBUG)
2020

21+
-- It is ok to print directly numbers or other data too if they can be concatenated to string:
22+
squid.warn(1.0)
23+
squid.warn(vmath.vector3(1))
24+
squid.warn(msg.url())
25+
squid.warn(type(1))
26+
squid.warn(hash "test")
27+
2128
-- Errors will be logged with source, message and traceback in console and saved to log file if configured:
2229
--assert(false)
2330

@@ -30,17 +37,23 @@ function init(self)
3037

3138
-- Create new instance with optional tag assigned (`none` by default) initially allowed or not
3239
self.player_logger = squid.new("player", true)
33-
-- Use all API function with the created instance
40+
-- Use all API function with the created instance:
3441
self.player_logger:info("Logger with 'player' tag")
42+
self.player_logger:log("Logger error with 'player' tag", squid.ERROR)
43+
self.player_logger:set_allowed("player", false)
44+
self.player_logger:warn("Not printing")
3545

3646
-- Get current configuration:
3747
local my_config = squid.get_config()
3848
my_config.is_printing = false
3949

40-
-- Set new configuration:
50+
-- Set new configuration with is_printing set to false, so below log will not be printed:
4151
squid.set_config(my_config)
52+
squid.error("Not printing this")
4253

43-
squid.error("TEST")
54+
-- Set back configuration:
55+
my_config.is_printing = true
56+
squid.set_config(my_config)
4457
end
4558

4659
function final()

0 commit comments

Comments
 (0)