* luci/libs: uvl: add support for external validation commands, various cleanups
[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 dump_dependency( dep, ref, v, e )
41         local str = nil
42
43         for k, v in luci.util.spairs( dep,
44                 function(a,b)
45                         a = ( type(dep[a]) ~= "boolean" and "_" or "" ) .. a
46                         b = ( type(dep[b]) ~= "boolean" and "_" or "" ) .. b
47                         return a < b
48                 end
49         ) do
50                 str = ( str and str .. " and " or "Dependency (" ) .. k ..
51                         ( type(v) ~= "boolean" and "=" .. v or "" )
52         end
53
54         str = string.format(
55                 '%s) failed:\n\t%s',
56                 str, e and e:gsub("\n","\n\t") or string.format(
57                         'Option "%s" %s',
58                         table.concat( ref, "." ), (
59                                 type(v) == "boolean"
60                                         and "has no value" or 'is not equal "' .. v .. '"'
61                         )
62                 )
63         )
64
65         return str
66 end