Mercurial > prosody-hg
comparison util/async.lua @ 8788:7a9b680a79fb
util.async: Move runner id into log tag
Avoids string concatenation in every logger call
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Fri, 04 May 2018 00:10:17 +0200 |
| parents | 01264bc60c2b |
| children | ed0891383e78 |
comparison
equal
deleted
inserted
replaced
| 8787:5dbebb7627ef | 8788:7a9b680a79fb |
|---|---|
| 1 local log = require "util.logger".init("util.async"); | 1 local logger = require "util.logger"; |
| 2 local log = logger.init("util.async"); | |
| 2 local new_id = require "util.id".short; | 3 local new_id = require "util.id".short; |
| 3 | 4 |
| 4 local function checkthread() | 5 local function checkthread() |
| 5 local thread, main = coroutine.running(); | 6 local thread, main = coroutine.running(); |
| 6 if not thread or main then | 7 if not thread or main then |
| 133 runner:log("error", "Encountered error: %s", err); | 134 runner:log("error", "Encountered error: %s", err); |
| 134 error(err); | 135 error(err); |
| 135 end | 136 end |
| 136 local function default_func(f) f(); end | 137 local function default_func(f) f(); end |
| 137 local function runner(func, watchers, data) | 138 local function runner(func, watchers, data) |
| 139 local id = new_id(); | |
| 140 local _log = logger.init("runner" .. id); | |
| 138 return setmetatable({ func = func or default_func, thread = false, state = "ready", notified_state = "ready", | 141 return setmetatable({ func = func or default_func, thread = false, state = "ready", notified_state = "ready", |
| 139 queue = {}, watchers = watchers or { error = default_error_watcher }, data = data, id = new_id() } | 142 queue = {}, watchers = watchers or { error = default_error_watcher }, data = data, id = id, _log = _log; } |
| 140 , runner_mt); | 143 , runner_mt); |
| 141 end | 144 end |
| 142 | 145 |
| 143 -- Add a task item for the runner to process | 146 -- Add a task item for the runner to process |
| 144 function runner_mt:run(input) | 147 function runner_mt:run(input) |
| 217 self:log("debug", "queued new work item, %d items queued", #self.queue); | 220 self:log("debug", "queued new work item, %d items queued", #self.queue); |
| 218 return self; | 221 return self; |
| 219 end | 222 end |
| 220 | 223 |
| 221 function runner_mt:log(level, fmt, ...) | 224 function runner_mt:log(level, fmt, ...) |
| 222 return log(level, "[runner %s] "..fmt, self.id, ...); | 225 return self._log(level, fmt, ...); |
| 223 end | 226 end |
| 224 | 227 |
| 225 function runner_mt:onready(f) | 228 function runner_mt:onready(f) |
| 226 self.watchers.ready = f; | 229 self.watchers.ready = f; |
| 227 return self; | 230 return self; |
