* luci/libs: uvl: use python-style format syntax for errors
[project/luci.git] / libs / uvl / luasrc / uvl / loghelper.lua
1 --[[
2
3 UCI Validation Layer - Logging utilities
4 (c) 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
5 (c) 2008 Steven Barth <steven@midlink.org>
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11 http://www.apache.org/licenses/LICENSE-2.0
12
13 $Id$
14
15 ]]--
16
17 module( "luci.uvl.loghelper", package.seeall )
18
19 function config_error( config, message )
20 return string.format(
21 'Error in config "%s":\n%s',
22 config, message or "Unknown error"
23 )
24 end
25
26 function section_error( section, message )
27 return string.format(
28 'Error in section "%s":\n%s',
29 section:cid(), message or "Unknown error"
30 )
31 end
32
33 function validator_error( option, message )
34 return string.format(
35 'External validator in option "%s" failed:\n%s',
36 option:cid(), message or "Unknown error"
37 )
38 end
39
40 function scheme_error( scheme, message )
41 return string.format(
42 'Error while loading scheme "%s":\n%s',
43 scheme, message:gsub("^.-:.-: ","")
44 )
45 end
46
47 function dump_dependency( dep, ref, v, e )
48 local str = nil
49
50 for k, v in luci.util.spairs( dep,
51 function(a,b)
52 a = ( type(dep[a]) ~= "boolean" and "_" or "" ) .. a
53 b = ( type(dep[b]) ~= "boolean" and "_" or "" ) .. b
54 return a < b
55 end
56 ) do
57 str = ( str and str .. " and " or "Dependency (" ) .. k ..
58 ( type(v) ~= "boolean" and "=" .. v or "" )
59 end
60
61 str = string.format(
62 '%s) failed:\n\t%s',
63 str, e and e:gsub("\n","\n\t") or string.format(
64 'Option "%s" %s',
65 table.concat( ref, "." ), (
66 type(v) == "boolean"
67 and "has no value" or 'is not equal "' .. v .. '"'
68 )
69 )
70 )
71
72 return str
73 end
74
75 function id( c, s, o )
76 if type(c) == "table" then
77 c, s, o = unpack(c)
78 end
79
80 return c .. ( s and '.' .. s or '' ) .. ( o and '.' .. o or '' )
81 end