Comparison

util/datamanager.lua @ 4993:5243b74a4cbb

Hopefully inert commit to clean up logging across a number of modules, removing all cases of concatenation when building log messages
author Matthew Wild <mwild1@gmail.com>
date Mon, 23 Jul 2012 17:32:33 +0100
parent 4452:7de17ca4de14
child 5024:d25e1b9332cc
comparison
equal deleted inserted replaced
4992:e79e4d1f75de 4993:5243b74a4cbb
113 function load(username, host, datastore) 113 function load(username, host, datastore)
114 local data, ret = loadfile(getpath(username, host, datastore)); 114 local data, ret = loadfile(getpath(username, host, datastore));
115 if not data then 115 if not data then
116 local mode = lfs.attributes(getpath(username, host, datastore), "mode"); 116 local mode = lfs.attributes(getpath(username, host, datastore), "mode");
117 if not mode then 117 if not mode then
118 log("debug", "Assuming empty "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil")); 118 log("debug", "Assuming empty %s storage ('%s') for user: %s@%s", datastore, ret, username or "nil", host or "nil");
119 return nil; 119 return nil;
120 else -- file exists, but can't be read 120 else -- file exists, but can't be read
121 -- TODO more detailed error checking and logging? 121 -- TODO more detailed error checking and logging?
122 log("error", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil")); 122 log("error", "Failed to load %s storage ('%s') for user: %s@%s", datastore, ret, username or "nil", host or "nil");
123 return nil, "Error reading storage"; 123 return nil, "Error reading storage";
124 end 124 end
125 end 125 end
126 setfenv(data, {}); 126 setfenv(data, {});
127 local success, ret = pcall(data); 127 local success, ret = pcall(data);
128 if not success then 128 if not success then
129 log("error", "Unable to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil")); 129 log("error", "Unable to load %s storage ('%s') for user: %s@%s", datastore, ret, username or "nil", host or "nil");
130 return nil, "Error reading storage"; 130 return nil, "Error reading storage";
131 end 131 end
132 return ret; 132 return ret;
133 end 133 end
134 134
143 end 143 end
144 144
145 -- save the datastore 145 -- save the datastore
146 local f, msg = io_open(getpath(username, host, datastore, nil, true), "w+"); 146 local f, msg = io_open(getpath(username, host, datastore, nil, true), "w+");
147 if not f then 147 if not f then
148 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil")); 148 log("error", "Unable to write to %s storage ('%s') for user: %s@%s", datastore, msg, username or "nil", host or "nil");
149 return nil, "Error saving to storage"; 149 return nil, "Error saving to storage";
150 end 150 end
151 f:write("return "); 151 f:write("return ");
152 append(f, data); 152 append(f, data);
153 f:close(); 153 f:close();
164 if not data then return; end 164 if not data then return; end
165 if callback(username, host, datastore) == false then return true; end 165 if callback(username, host, datastore) == false then return true; end
166 -- save the datastore 166 -- save the datastore
167 local f, msg = io_open(getpath(username, host, datastore, "list", true), "a+"); 167 local f, msg = io_open(getpath(username, host, datastore, "list", true), "a+");
168 if not f then 168 if not f then
169 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil")); 169 log("error", "Unable to write to %s storage ('%s') for user: %s@%s", datastore, msg, username or "nil", host or "nil");
170 return; 170 return;
171 end 171 end
172 f:write("item("); 172 f:write("item(");
173 append(f, data); 173 append(f, data);
174 f:write(");\n"); 174 f:write(");\n");
182 end 182 end
183 if callback(username, host, datastore) == false then return true; end 183 if callback(username, host, datastore) == false then return true; end
184 -- save the datastore 184 -- save the datastore
185 local f, msg = io_open(getpath(username, host, datastore, "list", true), "w+"); 185 local f, msg = io_open(getpath(username, host, datastore, "list", true), "w+");
186 if not f then 186 if not f then
187 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil")); 187 log("error", "Unable to write to %s storage ('%s') for user: %s@%s", datastore, msg, username or "nil", host or "nil");
188 return; 188 return;
189 end 189 end
190 for _, d in ipairs(data) do 190 for _, d in ipairs(data) do
191 f:write("item("); 191 f:write("item(");
192 append(f, d); 192 append(f, d);
205 function list_load(username, host, datastore) 205 function list_load(username, host, datastore)
206 local data, ret = loadfile(getpath(username, host, datastore, "list")); 206 local data, ret = loadfile(getpath(username, host, datastore, "list"));
207 if not data then 207 if not data then
208 local mode = lfs.attributes(getpath(username, host, datastore, "list"), "mode"); 208 local mode = lfs.attributes(getpath(username, host, datastore, "list"), "mode");
209 if not mode then 209 if not mode then
210 log("debug", "Assuming empty "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil")); 210 log("debug", "Assuming empty %s storage ('%s') for user: %s@%s", datastore, ret, username or "nil", host or "nil");
211 return nil; 211 return nil;
212 else -- file exists, but can't be read 212 else -- file exists, but can't be read
213 -- TODO more detailed error checking and logging? 213 -- TODO more detailed error checking and logging?
214 log("error", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil")); 214 log("error", "Failed to load %s storage ('%s') for user: %s@%s", datastore, ret, username or "nil", host or "nil");
215 return nil, "Error reading storage"; 215 return nil, "Error reading storage";
216 end 216 end
217 end 217 end
218 local items = {}; 218 local items = {};
219 setfenv(data, {item = function(i) t_insert(items, i); end}); 219 setfenv(data, {item = function(i) t_insert(items, i); end});
220 local success, ret = pcall(data); 220 local success, ret = pcall(data);
221 if not success then 221 if not success then
222 log("error", "Unable to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil")); 222 log("error", "Unable to load %s storage ('%s') for user: %s@%s", datastore, ret, username or "nil", host or "nil");
223 return nil, "Error reading storage"; 223 return nil, "Error reading storage";
224 end 224 end
225 return items; 225 return items;
226 end 226 end
227 227