Skip to content

Commit

Permalink
不再使用全局的logger对象
Browse files Browse the repository at this point in the history
  • Loading branch information
takayama-lily committed Oct 26, 2020
1 parent acbe0af commit b41629f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 21 deletions.
40 changes: 23 additions & 17 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,16 @@ const default_config = {
ws_reverse_url: [],
ws_reverse_reconnect_interval: 3000,
}
const config = {}, logger = process.OICQ.logger;
const config = {};
let bot, account, dir, server, wss, online = false, websockets = new Set(), wsrCreated = false;

function startup(arg) {
account = arg;
dir = path.join(process.mainModule.path, "data", account.toString());
dir = path.join(process.mainModule.path, "data", String(account));
if (!fs.existsSync(dir))
fs.mkdirSync(dir, {recursive: true, mode: 0o755});
Object.assign(config, default_config, global_config.general, global_config[account]);
logger.info("已加载配置文件:");
console.log(config);
console.log("已加载配置文件:", config);
if (config.debug && !config.log_level)
config.log_level = "debug";
config.device_path = dir;
Expand Down Expand Up @@ -72,7 +71,7 @@ function startup(arg) {
}

function inputPassword() {
logger.info("请输入密码:");
console.log("请输入密码:");
process.stdin.once("data", (input)=>{
input = input.toString().trim();
const password = crypto.createHash("md5").update(input).digest();
Expand Down Expand Up @@ -159,7 +158,7 @@ function dipatch(event) {
timeout: config.post_timeout,
headers: {
'Content-Type': 'application/json',
"X-Self-ID": account.toString(),
"X-Self-ID": String(account),
"User-Agent": "OneBot"
}
}
Expand Down Expand Up @@ -225,10 +224,10 @@ function createServer() {
});
}
server.listen(config.port, config.host, ()=>{
logger.info(`开启http服务器成功,监听${server.address().address}:${server.address().port}`);
console.log(`开启http服务器成功,监听${server.address().address}:${server.address().port}`);
}).on("error", (e)=>{
logger.error(e.message);
logger.error("开启http服务器失败。");
console.log(e.message);
console.log("开启http服务器失败,进程退出。");
process.exit(0);
})
}
Expand Down Expand Up @@ -258,7 +257,7 @@ function onWSOpen(ws) {
function createReverseWS() {
wsrCreated = true;
const headers = {
"X-Self-ID": account.toString(),
"X-Self-ID": String(account),
"X-Client-Role": "Universal",
"User-Agent": "OneBot"
};
Expand All @@ -274,19 +273,19 @@ function createWSClient(url, headers) {
const ws = new WebSocket(url, {headers});
ws.on("error", ()=>{});
ws.on("open", ()=>{
logger.info(`反向ws连接(${url})连接成功。`);
bot.logger.info(`反向ws连接(${url})连接成功。`);
websockets.add(ws);
onWSOpen(ws);
});
ws.on("close", ()=>{
logger.error(`反向ws连接(${url})被关闭,将在${config.ws_reverse_reconnect_interval}毫秒后尝试连接。`);
bot.logger.error(`反向ws连接(${url})被关闭,将在${config.ws_reverse_reconnect_interval}毫秒后尝试连接。`);
websockets.delete(ws);
setTimeout(()=>{
createWSClient(url, headers);
}, config.ws_reverse_reconnect_interval);
});
} catch (e) {
logger.error(e.message);
bot.logger.error(e.message);
}
}

Expand All @@ -296,7 +295,7 @@ function onHttpRes(event, res) {
res.on("end", ()=>{
if (!online) return;
data = Buffer.concat(data).toString();
logger.debug(`收到HTTP响应:${res.statusCode} ` + data);
debug(`收到HTTP响应:${res.statusCode} ` + data);
try {
data = JSON.parse(data);
api.quickOperate(event, data);
Expand All @@ -313,7 +312,7 @@ async function onHttpReq(req, res) {
const qop = url.parse(req.url);
const action = qop.pathname.replace("/", "");
if (req.method === "GET") {
logger.debug(`收到GET请求: ` + req.url);
debug(`收到GET请求: ` + req.url);
const params = querystring.parse(qop.query);
try {
const ret = await api.apply({action, params});
Expand All @@ -327,7 +326,7 @@ async function onHttpReq(req, res) {
req.on("end", async()=>{
try {
data = Buffer.concat(data).toString();
logger.debug(`收到POST请求: ` + data);
debug(`收到POST请求: ` + data);
let params;
if (req.headers["content-type"] === "application/x-www-form-urlencoded")
params = querystring.parse(data);
Expand All @@ -347,7 +346,7 @@ async function onHttpReq(req, res) {
}
}
async function onWSMessage(ws, data) {
logger.debug(`收到WS消息: ` + data);
debug(`收到WS消息: ` + data);
if (!online) {
return ws.send(JSON.stringify({
retcode: 104, status: "failed"
Expand Down Expand Up @@ -380,4 +379,11 @@ async function onWSMessage(ws, data) {
}
}

function debug(msg) {
if (bot && bot.logger)
bot.logger.debug(msg);
else
console.log(msg);
}

module.exports = startup;
7 changes: 3 additions & 4 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ try {
}

require("oicq");
const logger = process.OICQ.logger;
const data_dir = path.join(process.mainModule.path, "data");
try {
if (!fs.existsSync(data_dir))
Expand All @@ -24,8 +23,8 @@ try {
fs.writeFileSync(testfile, "");
fs.unlinkSync(testfile);
} catch(e) {
logger.error("数据文件夹不可写,进程退出。");
logger.error(e.message);
console.log("数据文件夹不可写,进程退出。");
console.log(e.message);
process.exit(0);
}

Expand All @@ -36,7 +35,7 @@ function inputAccount() {
if (account > 10000 && account < 0xffffffff) {
return require("./lib/core")(account);
}
logger.info("请输入账号:");
console.log("请输入账号:");
process.stdin.once("data", (input)=>{
account = parseInt(input.toString().trim());
inputAccount();
Expand Down

0 comments on commit b41629f

Please sign in to comment.