zby-live-sdk
Version:
This is a live SDK for weclassroom.
88 lines (82 loc) • 2.97 kB
JavaScript
import {
extensionInfo, config
} from '../config/config';
import dict from '../util/dict';
const currentTimeString = () => {
const timeString = '[' +
('' + new Date().getHours()).padStart(2, '0') + ':' +
('' + new Date().getMinutes()).padStart(2, '0') + ':' +
('' + new Date().getSeconds()).padStart(2, '0') + ':' +
('' + new Date().getMilliseconds()).padStart(3, '0') +
']';
return timeString;
};
const callMethod = (name, args, extensionId = 'default_ext') => {
// EM 是寄宿于端的,浏览器中并不存在,为防止报错需要先进行能力检测
if (EM) {
return new Promise((resolve, reject) => {
EM.CallMethod(
extensionId,
name,
JSON.stringify(args),
(code, content) => {
//控制台weblog高优先级
if (typeof window.zby_live_sdk_weblog !== 'undefined') {
if (window.zby_live_sdk_weblog === true) {
if (name === 'WriteLog' && code == 0) {
console.log(
currentTimeString() +
`${args.msg}`);
} else {
console.log(`${name} :
Time: ${currentTimeString()}
Param: ${args.msg}
Code: ${code}
Message: ${content}`);
}
}
} else {
//sdk默认输出weblog日志,除非明确禁用;初始化之前默认输出日志,初始化后根据参数规则
// if (!window.zby_sdk_init_params || typeof window.zby_sdk_init_params.weblog === 'undefined' || (typeof window.zby_sdk_init_params.weblog !== 'undefined' && window.zby_sdk_init_params.weblog === true)) {
if ( config.weblog === true) {
if (name === 'WriteLog' && code == 0) {
console.log(
currentTimeString() +
`${args.msg}`);
} else {
console.log(`${name} :
Time: ${currentTimeString()}
Param: ${args.msg}
Code: ${code}
Message: ${content}`);
}
}
}
resolve({
code,
content
});
});
});
}
};
export const writeLog = (msgtype = 'info', msgbody, productName) => {
msgbody = typeof msgbody === 'string' ? msgbody : JSON.stringify(msgbody);
const args = {
//日志级别,0:debug,1:info,2:warnning,3:error,4:critical
'type': dict.logMap[msgtype],
'msg': `--${productName?'['+productName+']--':''} ${msgbody}`
};
callMethod('WriteLog', args);
};
if (window.EM) {
EM.Load('default_ext',
extensionInfo.version.default_ext,
true,
function (ec, content) {
writeLog('info', 'Load EC:' + ec + '\nContent:' + content);
});
}
export default {
writeLog
};