9ec57f1320a8a6f6b05881d5cdaf13a36e973911
[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 dump_dependency( dep, ref, v, e )
34         local str = nil
35
36         for k, v in luci.util.spairs( dep,
37                 function(a,b)
38                         a = ( type(dep[a]) ~= "boolean" and "_" or "" ) .. a
39                         b = ( type(dep[b]) ~= "boolean" and "_" or "" ) .. b
40                         return a < b
41                 end
42         ) do
43                 str = ( str and str .. " and " or "Dependency (" ) .. k ..
44                         ( type(v) ~= "boolean" and "=" .. v or "" )
45         end
46
47         str = string.format(
48                 '%s) failed:\n\t%s',
49                 str, e or string.format(
50                         'Option "%s" %s',
51                         table.concat( ref, "." ), (
52                                 type(v) == "boolean"
53                                         and "has no value" or 'is not equal "' .. v .. '"'
54                         )
55                 )
56         )
57
58         return str
59 end