Skip to content

Instantly share code, notes, and snippets.

@b0mbie
Last active April 17, 2022 19:04
Show Gist options
  • Select an option

  • Save b0mbie/6d08ed279e752bc7baf85813d0e8326a to your computer and use it in GitHub Desktop.

Select an option

Save b0mbie/6d08ed279e752bc7baf85813d0e8326a to your computer and use it in GitHub Desktop.
Library for creating printfln and debugfln functions
local printDebug = {
debug = false
}
--- Similar to printfln of C, but uses Lua's print (which usually appends a newline).
function printDebug.printfln(fmt, ...) print(string.format(fmt, ...)) end
--- Create a printfln and debugfln function for a named context.
--- printfln message prefix: "[<ctx>] "
--- debugfln message prefix: "[<ctx>-debug] "
--- ctx: Context name to create for.
--- Returns: printfln, debugfln
function printDebug.createPrintDebug(ctx)
ctx = '[' .. ctx
local ctx_d = ctx .. "-debug] "
ctx = ctx .. "] "
local function cprintfln(fmt, ...)
printDebug.printfln(ctx .. fmt, ...)
end
local function cdebugfln(fmt, ...)
if not printDebug.debug then return end
printDebug.printfln(ctx_d .. fmt, ...)
end
return cprintfln, cdebugfln
end
return printDebug
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment