Skip to content

Commit

Permalink
Implement removeHandler in std/logging module (fixes nim-lang#23757) (
Browse files Browse the repository at this point in the history
nim-lang#24143)

Since the module allows for a handler to be added multiple times, for
the sake of consistency, `removeHandler` only removes the first found
instance of the handler in the `handlers` seq. So for n calls of
`addHandler` using the same handler, n calls of `removeHandler` are
required.

fixes nim-lang#23757

---------

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
  • Loading branch information
tocariimaa and Araq authored Sep 20, 2024
1 parent 37dba85 commit d51d887
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/pure/logging.nim
Original file line number Diff line number Diff line change
Expand Up @@ -839,13 +839,24 @@ proc addHandler*(handler: Logger) =
## each of those threads.
##
## See also:
## * `removeHandler proc`_
## * `getHandlers proc<#getHandlers>`_
runnableExamples:
var logger = newConsoleLogger()
addHandler(logger)
doAssert logger in getHandlers()
handlers.add(handler)

proc removeHandler*(handler: Logger) =
## Removes a logger from the list of registered handlers.
##
## Note that for n times a logger is registered, n calls to this proc
## are required to remove that logger.
for i, hnd in handlers:
if hnd == handler:
handlers.delete(i)
return

proc getHandlers*(): seq[Logger] =
## Returns a list of all the registered handlers.
##
Expand Down

0 comments on commit d51d887

Please sign in to comment.