| 1 | // Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved. |
| 2 | // Use of this source code is governed by an MIT license |
| 3 | // that can be found in the LICENSE file. |
| 4 | @[has_globals] |
| 5 | module log |
| 6 | |
| 7 | __global default_logger &Logger |
| 8 | |
| 9 | // TODO: remove this hack, when the language has a way to access the raw pointer to an interface value directly: |
| 10 | @[typedef] |
| 11 | pub struct C.log__Logger { |
| 12 | mut: |
| 13 | _object voidptr |
| 14 | } |
| 15 | |
| 16 | // init will be called before the user's main program starts, to initialize the default logger |
| 17 | fn init() { |
| 18 | default_logger = new_thread_safe_log() |
| 19 | at_exit(deinit) or {} |
| 20 | } |
| 21 | |
| 22 | // deinit will be called on exit of the program and will free the memory allocated for the default logger |
| 23 | fn deinit() { |
| 24 | free_logger(default_logger) |
| 25 | } |
| 26 | |
| 27 | @[manualfree] |
| 28 | fn free_logger(logger &Logger) { |
| 29 | if voidptr(logger) == unsafe { nil } { |
| 30 | return |
| 31 | } |
| 32 | unsafe { |
| 33 | // C.printf(c'free_logger logger: %p\n', logger) |
| 34 | logger.free() |
| 35 | // |
| 36 | pobject := &C.log__Logger(logger)._object |
| 37 | // C.printf(c'free_logger pobject: %p\n', pobject) |
| 38 | free(pobject) |
| 39 | // |
| 40 | free(logger) |
| 41 | } |
| 42 | } |
| 43 | |