Skip to content
This repository
Browse code

Split log out of init.lua

  • Loading branch information...
commit 142861a16d672a938189f246b73b9ad41580fcdb 1 parent 8d81f1b
Pavel Holejsovsky authored

Showing 2 changed files with 39 additions and 22 deletions. Show diff stats Hide diff stats

  1. +1 22 lgi/init.lua
  2. +38 0 lgi/log.lua
23 lgi/init.lua
@@ -45,28 +45,7 @@ end
45 45
46 46 -- Prepare logging support. 'log' is module-exported table, containing all
47 47 -- functionality related to logging wrapped around GLib g_log facility.
48   -lgi.log = {}
49   -
50   --- Creates table containing methods 'message', 'warning', 'critical', 'error',
51   --- 'debug' methods which log to specified domain.
52   -function lgi.log.domain(name)
53   - local domain = lgi.log[name] or {}
54   - for _, level in ipairs { 'message', 'warning', 'critical',
55   - 'error', 'debug' } do
56   - if not domain[level] then
57   - domain[level] =
58   - function(format, ...)
59   - local ok, msg = pcall(string.format, format, ...)
60   - if not ok then
61   - msg = ("BAD FMT: `%s', `%s'"):format(format, msg)
62   - end
63   - core.log(name, level:upper(), msg)
64   - end
65   - end
66   - end
67   - lgi.log[name] = domain
68   - return domain
69   -end
  48 +lgi.log = require 'lgi.log'
70 49
71 50 -- For the rest of bootstrap, prepare logging to lgi domain.
72 51 local log = lgi.log.domain('lgi')
38 lgi/log.lua
... ... @@ -0,0 +1,38 @@
  1 +------------------------------------------------------------------------------
  2 +--
  3 +-- LGI support for GLib-based logging.
  4 +--
  5 +-- Copyright (c) 2011 Pavel Holejsovsky
  6 +-- Licensed under the MIT license:
  7 +-- http://www.opensource.org/licenses/mit-license.php
  8 +--
  9 +------------------------------------------------------------------------------
  10 +
  11 +local pcall, ipairs = pcall, ipairs
  12 +local string = require 'string'
  13 +local core = require 'lgi.core'
  14 +
  15 +local log = {}
  16 +
  17 +-- Creates table containing methods 'message', 'warning', 'critical', 'error',
  18 +-- 'debug' methods which log to specified domain.
  19 +function log.domain(name)
  20 + local domain = log[name] or {}
  21 + for _, level in ipairs { 'message', 'warning', 'critical',
  22 + 'error', 'debug' } do
  23 + if not domain[level] then
  24 + domain[level] =
  25 + function(format, ...)
  26 + local ok, msg = pcall(string.format, format, ...)
  27 + if not ok then
  28 + msg = ("BAD FMT: `%s', `%s'"):format(format, msg)
  29 + end
  30 + core.log(name, level:upper(), msg)
  31 + end
  32 + end
  33 + end
  34 + log[name] = domain
  35 + return domain
  36 +end
  37 +
  38 +return log

0 comments on commit 142861a

Please sign in to comment.
Something went wrong with that request. Please try again.