using our own logging, allows us to do some eventual modifications

This commit is contained in:
jomo 2014-11-23 22:56:47 +01:00
parent ea3f322c73
commit dd8a3b91e2
5 changed files with 65 additions and 45 deletions

View File

@ -1,3 +1,4 @@
var logging = require('./logging');
var config = require("./config");
var redis = null;
var fs = require("fs");
@ -5,7 +6,7 @@ var fs = require("fs");
// sets up redis connection
// flushes redis when running on heroku (files aren't kept between pushes)
function connect_redis() {
console.log("connecting to redis...");
logging.log("connecting to redis...");
if (process.env.REDISCLOUD_URL) {
var redisURL = require("url").parse(process.env.REDISCLOUD_URL);
redis = require("redis").createClient(redisURL.port, redisURL.hostname);
@ -14,17 +15,17 @@ function connect_redis() {
redis = require("redis").createClient();
}
redis.on("ready", function() {
console.log("Redis connection established.");
logging.log("Redis connection established.");
if(process.env.HEROKU) {
console.log("Running on heroku, flushing redis");
logging.log("Running on heroku, flushing redis");
redis.flushall();
}
});
redis.on("error", function (err) {
console.error(err);
logging.error(err);
});
redis.on("end", function () {
console.warn("Redis connection lost!");
logging.warn("Redis connection lost!");
});
}
@ -38,11 +39,11 @@ function update_file_date(hash) {
var date = new Date();
fs.utimes(path, date, date, function(err){
if (err) {
console.error(err);
logging.error(err);
}
});
} else {
console.error("Tried to update " + path + " date, but it doesn't exist");
logging.error("Tried to update " + path + " date, but it doesn't exist");
}
});
}
@ -56,7 +57,7 @@ exp.get_redis = function() {
// sets the timestamp for +uuid+ and its face file's date to now
exp.update_timestamp = function(uuid, hash) {
console.log(uuid + " cache: updating timestamp");
logging.log(uuid + " cache: updating timestamp");
var time = new Date().getTime();
redis.hmset(uuid, "t", time);
update_file_date(hash);
@ -64,7 +65,7 @@ exp.update_timestamp = function(uuid, hash) {
// create the key +uuid+, store +hash+ and time
exp.save_hash = function(uuid, hash) {
console.log(uuid + " cache: saving hash");
logging.log(uuid + " cache: saving hash");
var time = new Date().getTime();
redis.hmset(uuid, "h", hash, "t", time);
};

View File

@ -6,7 +6,8 @@ var config = {
browser_cache_time: 3600, // seconds until browser will request image again
http_timeout: 3000, // ms until connection to mojang is dropped
faces_dir: 'skins/faces/', // directory where faces are kept. should have trailing '/'
helms_dir: 'skins/helms/' // directory where helms are kept. should have trailing '/'
helms_dir: 'skins/helms/', // directory where helms are kept. should have trailing '/'
debug_enabled: true // enables logging.debug
};
module.exports = config;

View File

@ -1,4 +1,5 @@
var networking = require('./networking');
var logging = require('./logging');
var config = require('./config');
var cache = require('./cache');
var skins = require('./skins');
@ -20,17 +21,17 @@ function store_images(uuid, details, callback) {
callback(err, null);
} else {
if (skin_url) {
console.log(uuid + " " + skin_url);
logging.log(uuid + " " + skin_url);
// set file paths
var hash = get_hash(skin_url);
if (details && details.hash == hash) {
// hash hasn't changed
console.log(uuid + " hash has not changed");
logging.log(uuid + " hash has not changed");
cache.update_timestamp(uuid, hash);
callback(null, hash);
} else {
// hash has changed
console.log(uuid + " new hash: " + hash);
logging.log(uuid + " new hash: " + hash);
var facepath = __dirname + '/../' + config.faces_dir + hash + ".png";
var helmpath = __dirname + '/../' + config.helms_dir + hash + ".png";
// download skin, extract face/helm
@ -67,18 +68,18 @@ function get_image_hash(uuid, callback) {
} else {
if (details && details.time + config.local_cache_time * 1000 >= new Date().getTime()) {
// uuid known + recently updated
console.log(uuid + " uuid known & recently updated");
logging.log(uuid + " uuid known & recently updated");
callback(null, (details.hash ? 1 : 0), details.hash);
} else {
console.log(uuid + " uuid not known or too old");
console.log("details:");
console.log(details);
console.log("/details");
logging.log(uuid + " uuid not known or too old");
logging.log("details:");
logging.log(details);
logging.log("/details");
store_images(uuid, details, function(err, hash) {
if (err) {
callback(err, -1, details && details.hash);
} else {
console.log(uuid + " hash: " + hash);
logging.log(uuid + " hash: " + hash);
var oldhash = details && details.hash;
var status = hash !== oldhash ? 2 : 3;
callback(null, status, hash);
@ -102,7 +103,7 @@ exp.uuid_valid = function(uuid) {
// image is the user's face+helm when helm is true, or the face otherwise
// for status, see get_image_hash
exp.get_avatar = function(uuid, helm, size, callback) {
console.log("\nrequest: " + uuid);
logging.log("\nrequest: " + uuid);
get_image_hash(uuid, function(err, status, hash) {
if (hash) {
var filepath = __dirname + '/../' + (helm ? config.helms_dir : config.faces_dir) + hash + ".png";

16
modules/logging.js Normal file
View File

@ -0,0 +1,16 @@
var config = require("./config");
var exp = {};
function debug() {
if (config.debug_enabled) {
console.log(Array.prototype.slice.call(arguments).join(" "));
}
}
exp.log = console.log;
exp.warn = console.warn;
exp.error = console.error;
exp.debug = debug;
module.exports = exp;

View File

@ -1,3 +1,4 @@
var logging = require('./logging');
var request = require('request');
var config = require('./config');
var skins = require('./skins');
@ -32,24 +33,24 @@ var get_username_url = function(name, callback) {
}, function(error, response, body) {
if (!error && response.statusCode == 301) {
// skin_url received successfully
console.log(name + " skin url received");
logging.log(name + " skin url received");
callback(null, response.headers.location);
} else if (error) {
callback(error, null);
} else if (response.statusCode == 404) {
// skin doesn't exist
console.log(name + " has no skin");
logging.log(name + " has no skin");
callback(0, null);
} else if (response.statusCode == 429) {
// Too Many Requests
// Never got this, seems like skins aren't limited
console.warn(name + " Too many requests");
console.warn(body);
logging.warn(name + " Too many requests");
logging.warn(body);
callback(null, null);
} else {
console.error(name + " Unknown error:");
console.error(response);
console.error(body);
logging.error(name + " Unknown error:");
logging.error(response);
logging.error(body);
callback(null, null);
}
});
@ -64,23 +65,23 @@ var get_uuid_url = function(uuid, callback) {
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
// profile downloaded successfully
console.log(uuid + " profile downloaded");
logging.log(uuid + " profile downloaded");
callback(null, extract_skin_url(JSON.parse(body)));
} else if (error) {
callback(error, null);
} else if (response.statusCode == 204 || response.statusCode == 404) {
// we get 204 No Content when UUID doesn't exist (including 404 in case they change that)
console.log(uuid + " uuid does not exist");
logging.log(uuid + " uuid does not exist");
callback(0, null);
} else if (response.statusCode == 429) {
// Too Many Requests
console.warn(uuid + " Too many requests");
console.warn(body);
logging.warn(uuid + " Too many requests");
logging.warn(body);
callback(null, null);
} else {
console.error(uuid + " Unknown error:");
console.error(response);
console.error(body);
logging.error(uuid + " Unknown error:");
logging.error(response);
logging.error(body);
callback(null, null);
}
});
@ -108,7 +109,7 @@ exp.get_skin_url = function(uuid, callback) {
// callback contains error
exp.skin_file = function(url, facename, helmname, callback) {
if (fs.existsSync(facename) && fs.existsSync(facename)) {
console.log("Images already exist, not downloading.");
logging.log("Images already exist, not downloading.");
callback(null);
return;
}
@ -119,32 +120,32 @@ exp.skin_file = function(url, facename, helmname, callback) {
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
// skin downloaded successfully
console.log(url + " skin downloaded");
logging.log(url + " skin downloaded");
skins.extract_face(body, facename, function(err) {
if (err) {
callback(err);
} else {
console.log(facename + " face extracted");
logging.log(facename + " face extracted");
skins.extract_helm(facename, body, helmname, function(err) {
console.log(helmname + " helm extracted.");
logging.log(helmname + " helm extracted.");
callback(err);
});
}
});
} else {
if (error) {
console.error("Error downloading '" + url + "': " + error);
logging.error("Error downloading '" + url + "': " + error);
} else if (response.statusCode == 404) {
console.warn("texture not found (404): " + url);
logging.warn("texture not found (404): " + url);
} else if (response.statusCode == 429) {
// Too Many Requests
// Never got this, seems like textures aren't limited
console.warn("too many requests for " + url);
console.warn(body);
logging.warn("too many requests for " + url);
logging.warn(body);
} else {
console.error("unknown error for " + url);
console.error(response);
console.error(body);
logging.error("unknown error for " + url);
logging.error(response);
logging.error(body);
error = "unknown error"; // Error needs to be set, otherwise null in callback
}
callback(error);