fix: 日志系统
This commit is contained in:
parent
6b6385fc43
commit
6470eb1280
@ -34,4 +34,10 @@ export const EntryTag = '[Entry]';
|
||||
export const WorkerTag = '[Worker]';
|
||||
|
||||
//中心udp
|
||||
export const CenterUdpTag = '[CenterUdpBusiness]'
|
||||
export const CenterUdpTag = '[CenterUdpBusiness]'
|
||||
|
||||
//TerminalInfo
|
||||
export const TerminalInfoTag = '[TerminalInfo]';
|
||||
|
||||
// CommonFile
|
||||
export const CommonFileTag = '[CommonFile]';
|
||||
@ -17,4 +17,78 @@ export interface OtherMessageType {
|
||||
export interface WorkerBackMessage {
|
||||
type: string;
|
||||
data: string | CenterCallBackMsgType
|
||||
}
|
||||
|
||||
export interface LogWorkerMessage {
|
||||
type: WorkerMessageType;
|
||||
data?: string;
|
||||
}
|
||||
|
||||
//枚举
|
||||
export enum WorkerMessageType {
|
||||
Log = 'log',
|
||||
Error = 'error',
|
||||
Warn = 'warn',
|
||||
Info = 'info',
|
||||
// 初始化 初始化文件夹到/mnt/hmdfs/100/account/device_view/local/files/duolun/logs/2024_10_12
|
||||
Init = 'init',
|
||||
// 初始化过程数据,初始化到人员以及人员里面各个目录
|
||||
ProcessDataInit = 'processDataInit',
|
||||
// 打开过程数据文件
|
||||
OpenProcessDataFile = 'openProcessDataFile',
|
||||
|
||||
}
|
||||
|
||||
export interface LogPathType {
|
||||
// log文件
|
||||
log?: string
|
||||
// error文件
|
||||
error?: string
|
||||
// 四合一原始数据
|
||||
four_one_log_byte_data?: string;
|
||||
|
||||
// 四合一转换数据
|
||||
four_one_log_data?: string;
|
||||
|
||||
// 评判数据
|
||||
judge_exam_data?: string;
|
||||
|
||||
// 评判日志
|
||||
judge_log_data?: string
|
||||
// 评判事件回调日志
|
||||
judge_progress_callback_data?: string;
|
||||
|
||||
// plc数据
|
||||
plc_data?: string
|
||||
// 无锡所数据
|
||||
wuxi_exam_data?: string
|
||||
// 无锡所过程轨迹数据
|
||||
wuxi_progress_data?: string;
|
||||
}
|
||||
|
||||
export interface LogFileFd {
|
||||
// log文件
|
||||
log?: number
|
||||
// error文件
|
||||
error?: number
|
||||
// 四合一原始数据
|
||||
four_one_log_byte_data?: number;
|
||||
|
||||
// 四合一转换数据
|
||||
four_one_log_data?: number;
|
||||
|
||||
// 评判数据
|
||||
judge_exam_data?: number;
|
||||
|
||||
// 评判日志
|
||||
judge_log_data?: number
|
||||
// 评判事件回调日志
|
||||
judge_progress_callback_data?: number;
|
||||
|
||||
// plc数据
|
||||
plc_data?: number
|
||||
// 无锡所数据
|
||||
wuxi_exam_data?: number
|
||||
// 无锡所过程轨迹数据
|
||||
wuxi_progress_data?: number;
|
||||
}
|
||||
@ -2,7 +2,7 @@ import HeaderComponent from './compontents/Header';
|
||||
import { CommonListType, EnvironmentConfigurationType } from '../model';
|
||||
import common from '@ohos.app.ability.common';
|
||||
import FileUtils from '../utils/FileUtils';
|
||||
import { GlobalConfig } from '../config';
|
||||
import { GlobalConfig, TerminalInfoTag } from '../config';
|
||||
import ethernet from '@ohos.net.ethernet';
|
||||
import { BusinessError } from '@ohos.base';
|
||||
import Prompt from '@system.prompt';
|
||||
@ -17,7 +17,7 @@ struct TerminalInfoPage {
|
||||
async aboutToAppear() {
|
||||
this.fileUtil = new FileUtils(this.context)
|
||||
const data = await this.fileUtil.readFile(GlobalConfig.commonFileWriteAddress + '/config/ipConfig.txt');
|
||||
console.log("data", data)
|
||||
console.log(TerminalInfoTag, "data", data)
|
||||
if (data) {
|
||||
this.config = JSON.parse(data)
|
||||
AppStorage.setOrCreate<EnvironmentConfigurationType>("EnvironmentConfiguration", this.config)
|
||||
@ -194,7 +194,7 @@ struct TerminalInfoPage {
|
||||
|
||||
Row() {
|
||||
Image($r('app.media.bc')).height(80).objectFit(ImageFit.Contain).onClick(() => {
|
||||
console.log("保存配置", JSON.stringify(this.config))
|
||||
console.log(TerminalInfoTag, "保存配置", JSON.stringify(this.config))
|
||||
AppStorage.setOrCreate<EnvironmentConfigurationType>("EnvironmentConfiguration", this.config)
|
||||
this.fileUtil.addFile(GlobalConfig.commonFileWriteAddress + '/config/ipConfig.txt', JSON.stringify(this.config))
|
||||
ethernet.setIfaceConfig("eth0", {
|
||||
|
||||
@ -1,4 +1,7 @@
|
||||
import dayTs from './Date';
|
||||
import fs from '@ohos.file.fs';
|
||||
import { BusinessError } from '@ohos.base';
|
||||
import { CommonFileTag } from '../config';
|
||||
|
||||
enum timeType {
|
||||
fulltime = 1
|
||||
@ -155,4 +158,47 @@ export function DeepClone<T extends Object>(target: T): T {
|
||||
}
|
||||
// 如果是原始值,则直接返回
|
||||
return target;
|
||||
}
|
||||
|
||||
/*
|
||||
* 检查文件或目录是否存在
|
||||
* @param path 文件或目录的路径
|
||||
* @return 返回一个 Promise,解析为 true 如果存在,否则为 false
|
||||
*/
|
||||
export function IsExit(path: string): Promise<boolean> {
|
||||
return new Promise((resolve, reject) => {
|
||||
try {
|
||||
let res = fs.accessSync(path);
|
||||
if (res) {
|
||||
console.info(CommonFileTag, "file exists");
|
||||
resolve(true);
|
||||
} else {
|
||||
console.info(CommonFileTag, "file not exists");
|
||||
resolve(false);
|
||||
}
|
||||
} catch (error) {
|
||||
let err: BusinessError = error as BusinessError;
|
||||
console.error(CommonFileTag, "accessSync failed with error message: " + err.message + ", error code: " + err.code);
|
||||
reject(false);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建文件夹
|
||||
* @param path 文件夹路径
|
||||
* @return 返回一个 Promise,解析为 true 如果创建成功,否则为 false
|
||||
*/
|
||||
export function CreateDir(path: string): Promise<boolean> {
|
||||
return new Promise((resolve, reject) => {
|
||||
try {
|
||||
fs.mkdirSync(path);
|
||||
console.info(CommonFileTag, "Directory created successfully");
|
||||
resolve(true);
|
||||
} catch (error) {
|
||||
let err: BusinessError = error as BusinessError;
|
||||
console.error(CommonFileTag, "mkdirSync failed with error message: " + err.message + ", error code: " + err.code);
|
||||
reject(false);
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -145,4 +145,5 @@ export default class FileUtils {
|
||||
duration: 4000,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,4 +1,10 @@
|
||||
import dayTs from '../Date'
|
||||
import worker from '@ohos.worker'
|
||||
import { LogWorkerMessage, WorkerMessageType } from '../../model'
|
||||
|
||||
class logWorker {
|
||||
private workerInstance = new worker.ThreadWorker("entry/ets/workers/Log.ets")
|
||||
|
||||
// 正常日志
|
||||
log(msg: string) {
|
||||
console.log(msg)
|
||||
@ -18,6 +24,23 @@ class logWorker {
|
||||
error(msg: string) {
|
||||
console.error(msg)
|
||||
}
|
||||
|
||||
// 初始化板子,需要判断duolun是否存在,不存在则创建
|
||||
init() {
|
||||
this.workerInstance = new worker.ThreadWorker("entry/ets/workers/Log.ets");
|
||||
let data: LogWorkerMessage = {
|
||||
type: WorkerMessageType.Init
|
||||
}
|
||||
this.workerInstance.postMessage(JSON.stringify(data))
|
||||
}
|
||||
|
||||
// 过程数据初始化
|
||||
processDataInit(carNo: string, name: string) {
|
||||
let date = dayTs().format("YYYY_MM_DD_HH_mm_ss");
|
||||
// 组装文件夹名 2025_06_18_09_14_36_2506474075216_371427200311275216_王宏伟
|
||||
let folderName = `${date}_${carNo}_${name}`;
|
||||
// 内部包含文件
|
||||
}
|
||||
}
|
||||
|
||||
export const dConsole = new logWorker();
|
||||
@ -1,30 +1,68 @@
|
||||
import worker, { ThreadWorkerGlobalScope, MessageEvents, ErrorEvent } from '@ohos.worker';
|
||||
import worker, { ErrorEvent, MessageEvents, ThreadWorkerGlobalScope } from '@ohos.worker';
|
||||
import { GlobalConfig } from '../config';
|
||||
import { LogFileFd, LogPathType, LogWorkerMessage, WorkerMessageType } from '../model';
|
||||
import { CreateDir, IsExit } from '../utils/Common';
|
||||
import dayTs from '../utils/Date';
|
||||
|
||||
const workerPort: ThreadWorkerGlobalScope = worker.workerPort;
|
||||
|
||||
/**
|
||||
* Defines the event handler to be called when the worker thread receives a message sent by the host thread.
|
||||
* The event handler is executed in the worker thread.
|
||||
*
|
||||
* @param e message data
|
||||
*/
|
||||
workerPort.onmessage = (e: MessageEvents) => {
|
||||
// 日志文件目录
|
||||
let logPaths: LogPathType = {
|
||||
log: `${GlobalConfig.commonFileWriteAddress}/logs/${dayTs().format("YYYY_MM_DD")}/log/log.log`,
|
||||
error: `${GlobalConfig.commonFileWriteAddress}/logs/${dayTs().format("YYYY_MM_DD")}/log/error.log`,
|
||||
}
|
||||
let logFileFd: LogFileFd = {}
|
||||
const result: LogWorkerMessage = JSON.parse(e.data);
|
||||
if (result.type === WorkerMessageType.Init) {
|
||||
Init()
|
||||
}
|
||||
if (result.type === WorkerMessageType.OpenProcessDataFile) {
|
||||
OpenProcessDataFile(logPaths, logFileFd, result.data)
|
||||
}
|
||||
}
|
||||
|
||||
// 初始化
|
||||
async function Init() {
|
||||
// /mnt/hmdfs/100/account/device_view/local/files/duolun/logs/2024_10_12
|
||||
// 创建duolun文件夹 /mnt/hmdfs/100/account/device_view/local/files/duolun
|
||||
const basePath = GlobalConfig.commonFileWriteAddress;
|
||||
const isExit = await IsExit(basePath);
|
||||
if (!isExit) {
|
||||
// 创建文件夹
|
||||
await CreateDir(basePath)
|
||||
}
|
||||
// 创建logs文件夹/mnt/hmdfs/100/account/device_view/local/files/duolun/logs
|
||||
let logPath = `${basePath}/logs`;
|
||||
const logIsExit = await IsExit(logPath);
|
||||
if (!logIsExit) {
|
||||
// 创建logs文件夹
|
||||
await CreateDir(logPath);
|
||||
}
|
||||
// 创建日期文件夹/mnt/hmdfs/100/account/device_view/local/files/duolun/logs/2024_10_12
|
||||
let date = dayTs().format("YYYY_MM_DD");
|
||||
const folderName = `${GlobalConfig.commonFileWriteAddress}/logs/${date}`;
|
||||
const folderIsExit = await IsExit(folderName);
|
||||
if (!folderIsExit) {
|
||||
// 创建日期文件夹
|
||||
await CreateDir(folderName);
|
||||
}
|
||||
// 创建log文件夹/mnt/hmdfs/100/account/device_view/local/files/duolun/logs/2024_10_12/log
|
||||
const logFilePath = `${folderName}/log`;
|
||||
const logDirIsExit = await IsExit(logFilePath);
|
||||
if (!logDirIsExit) {
|
||||
// 创建log文件夹
|
||||
await CreateDir(logFilePath);
|
||||
}
|
||||
}
|
||||
|
||||
function OpenProcessDataFile(logPaths: LogPathType, logFileFd: LogFileFd, name: string) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines the event handler to be called when the worker receives a message that cannot be deserialized.
|
||||
* The event handler is executed in the worker thread.
|
||||
*
|
||||
* @param e message data
|
||||
*/
|
||||
workerPort.onmessageerror = (e: MessageEvents) => {
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines the event handler to be called when an exception occurs during worker execution.
|
||||
* The event handler is executed in the worker thread.
|
||||
*
|
||||
* @param e error message
|
||||
*/
|
||||
|
||||
workerPort.onerror = (e: ErrorEvent) => {
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user