日志控制

This commit is contained in:
chen wei bo
2025-09-21 20:18:30 +08:00
parent 23c038fd14
commit 8f90c1d7cf
23 changed files with 207 additions and 178 deletions
@@ -3,6 +3,7 @@ import { DataManager, DataId } from "../data/DataManager";
import { GirlData } from "../data/GirlData";
import proto from "db://assets/Scripts/proto/proto.pb.js";
import { ChatService } from "../network/services/ChatService";
import { logger } from "db://assets/Scripts/Main/Common/Logger";
export interface ChatMessage {
role: "user" | "model";
@@ -49,7 +50,7 @@ export class ChatHistoryManager {
const existingHistory: ChatHistory = JSON.parse(existingData);
createdAt = existingHistory.createdAt;
} catch (parseError) {
console.warn(`Failed to parse existing history for role ${roleId}, using current time as createdAt`);
logger.warn(`Failed to parse existing history for role ${roleId}, using current time as createdAt`);
}
}
@@ -61,11 +62,11 @@ export class ChatHistoryManager {
};
sys.localStorage.setItem(key, JSON.stringify(history));
console.log(
logger.log(
`Saved chat history for role ${roleId}, ${messages.length} messages`
);
} catch (error) {
console.error(`Failed to save chat history for role ${roleId}:`, error);
logger.error(`Failed to save chat history for role ${roleId}:`, error);
}
}
@@ -80,13 +81,13 @@ export class ChatHistoryManager {
const data = sys.localStorage.getItem(key);
if (data) {
const history: ChatHistory = JSON.parse(data);
console.log(
logger.log(
`Loaded chat history for role ${roleId}: ${history.messages.length} messages`
);
return history.messages;
}
} catch (error) {
console.error(`Failed to load chat history for role ${roleId}:`, error);
logger.error(`Failed to load chat history for role ${roleId}:`, error);
// 如果数据损坏,清除错误的数据
this.clearHistory(roleId);
}
@@ -100,7 +101,7 @@ export class ChatHistoryManager {
public clearHistory(roleId: number): void {
const key = `chat_history_${roleId}`;
sys.localStorage.removeItem(key);
console.log(`Cleared chat history for role ${roleId}`);
logger.log(`Cleared chat history for role ${roleId}`);
}
/**
@@ -118,7 +119,7 @@ export class ChatHistoryManager {
// 限制历史长度,保留最近100条消息
if (history.length > 100) {
history.splice(0, history.length - 100);
console.log(`Trimmed chat history for role ${roleId} to 100 messages`);
logger.log(`Trimmed chat history for role ${roleId} to 100 messages`);
}
this.saveHistory(roleId, history);
@@ -163,7 +164,7 @@ export class ChatHistoryManager {
sys.localStorage.removeItem(key);
});
console.log(
logger.log(
`Cleared all chat histories, ${keysToRemove.length} records removed`
);
}
@@ -193,7 +194,7 @@ export class ChatHistoryManager {
public async syncToRemote(roleId: number): Promise<void> {
const history = this.loadHistory(roleId);
if (history.length === 0) {
console.log(`No history to sync for role ${roleId}`);
logger.log(`No history to sync for role ${roleId}`);
return;
}
@@ -204,11 +205,11 @@ export class ChatHistoryManager {
// messages: history
// }, "POST");
console.log(
logger.log(
`Ready to sync ${history.length} messages for role ${roleId} to remote server`
);
} catch (error) {
console.error(`Failed to sync history for role ${roleId}:`, error);
logger.error(`Failed to sync history for role ${roleId}:`, error);
}
}
@@ -232,7 +233,7 @@ export class ChatHistoryManager {
return history.createdAt;
}
} catch (error) {
console.error(`Failed to get last chat time for role ${roleId}:`, error);
logger.error(`Failed to get last chat time for role ${roleId}:`, error);
}
return null;
}
@@ -250,14 +251,14 @@ export class ChatHistoryManager {
// if (response && response.messages) {
// this.saveHistory(roleId, response.messages);
// console.log(`Synced ${response.messages.length} messages from remote for role ${roleId}`);
// logger.log(`Synced ${response.messages.length} messages from remote for role ${roleId}`);
// }
console.log(
logger.log(
`Ready to sync history from remote server for role ${roleId}`
);
} catch (error) {
console.error(`Failed to sync from remote for role ${roleId}:`, error);
logger.error(`Failed to sync from remote for role ${roleId}:`, error);
}
}
@@ -288,7 +289,7 @@ export class ChatHistoryManager {
// 按时间戳排序,确保消息顺序正确
messages.sort((a, b) => (a.timestamp || 0) - (b.timestamp || 0));
console.log(
logger.log(
`Converted ${serverMessages.length} server messages to ${messages.length} local messages`
);
return messages;
@@ -313,7 +314,7 @@ export class ChatHistoryManager {
limit,
};
console.log("获取服务端聊天记录:", reqData);
logger.log("获取服务端聊天记录:", reqData);
const res = await ChatService.I.reqGetChatMsg(reqData);
if (res && res.code === proto.cs.EnmRetCode.SUCCESS) {
@@ -333,20 +334,20 @@ export class ChatHistoryManager {
resData.chatRemainCount
);
console.log(
logger.log(
`成功从服务端获取聊天记录,roleId: ${roleId}, 消息数: ${
resData.msgs?.length || 0
}`
);
return true;
} else {
console.warn(
logger.warn(
`从服务端获取聊天记录失败,roleId: ${roleId}, code: ${res?.code}`
);
return false;
}
} catch (error) {
console.error(`获取服务端聊天记录异常,roleId: ${roleId}:`, error);
logger.error(`获取服务端聊天记录异常,roleId: ${roleId}:`, error);
return false;
}
}
@@ -390,7 +391,7 @@ export class ChatHistoryManager {
const mergedMessages = Array.from(messageMap.values());
mergedMessages.sort((a, b) => (a.timestamp || 0) - (b.timestamp || 0));
console.log(
logger.log(
`合并聊天记录: 本地 ${localMessages.length} 条, 服务端 ${serverMessages.length} 条, 合并后 ${mergedMessages.length}`
);
return mergedMessages;
@@ -407,18 +408,18 @@ export class ChatHistoryManager {
// 2. 如果本地有数据,直接返回
if (localHistory && localHistory.length > 0) {
console.log(
logger.log(
`使用本地聊天记录,roleId: ${roleId}, 消息数: ${localHistory.length}`
);
return localHistory;
}
// 3. 本地没有数据,尝试从服务端获取
console.log(`本地无聊天记录,尝试从服务端获取,roleId: ${roleId}`);
logger.log(`本地无聊天记录,尝试从服务端获取,roleId: ${roleId}`);
const serverSuccess = await this.fetchServerChatHistory(roleId);
if (!serverSuccess) {
console.log(`服务端获取失败,返回空记录,roleId: ${roleId}`);
logger.log(`服务端获取失败,返回空记录,roleId: ${roleId}`);
return [];
}
@@ -447,7 +448,7 @@ export class ChatHistoryManager {
// 6. 保存到本地存储
if (convertedMessages.length > 0) {
this.saveHistory(roleId, convertedMessages);
console.log(
logger.log(
`从服务端获取并保存聊天记录,roleId: ${roleId}, 消息数: ${convertedMessages.length}`
);
}
@@ -2,6 +2,7 @@ import { ChatContentsLayout } from "../ui/components/ChatContentsLayout";
import { DiaLogData, Dialog } from "../data/DialogData";
import Utils from "db://assets/Scripts/Main/Common/Utils";
import { InnerMsgCode } from "db://assets/Scripts/Main/Config/InnerMsgCode";
import { logger } from "db://assets/Scripts/Main/Common/Logger";
/**
* 对话管理器
@@ -75,7 +76,7 @@ export class DialogManager {
// 如果是玩家消息,直接添加单个气泡
if (isPlayer) {
this.dialogData.pushDialog(isPlayer, str);
console.log("Dialog updated:", {
logger.log("Dialog updated:", {
isPlayer,
content: str.substring(0, 50) + "...",
});
@@ -134,7 +135,7 @@ export class DialogManager {
*/
public addLoadingDialog(): void {
this.dialogData.pushDialog(false, "...", true);
console.log("Loading dialog added");
logger.log("Loading dialog added");
Utils.sendInnerMsg(InnerMsgCode.Chat_DialogRefresh);
}
@@ -153,7 +154,7 @@ export class DialogManager {
*/
public clearDialogs(): void {
this.dialogData.cleanDialog();
console.log("All dialogs cleared");
logger.log("All dialogs cleared");
}
/**
@@ -168,7 +169,7 @@ export class DialogManager {
dialog.content,
);
});
console.log(`DialogManager: Set ${dialogs.length} dialogs`);
logger.log(`DialogManager: Set ${dialogs.length} dialogs`);
Utils.sendInnerMsg(InnerMsgCode.Chat_DialogRefresh);
}
@@ -178,7 +179,7 @@ export class DialogManager {
*/
public syncFromChatModel(dialogs: Dialog[]): void {
this.setDialogs(dialogs);
console.log("DialogManager: Synced dialogs from ChatModel");
logger.log("DialogManager: Synced dialogs from ChatModel");
}
}
@@ -7,6 +7,7 @@ import { InnerMsgCode } from "../../Main/Config/InnerMsgCode";
import { ThemePanel } from "../ui/panels/ThemePanel";
import { GirlListPanel } from "../ui/panels/GirlListPanel";
import li_BaseView from "../../Main/Common/li_BaseView";
import { logger } from "db://assets/Scripts/Main/Common/Logger";
/**
* 页面过渡动画类型枚举
@@ -120,7 +121,7 @@ export class NavigationManager {
*/
public registerNavigationPanel(panel: any): void {
this.navigationPanel = panel;
console.log("[NavigationManager] NavigationPanel registered");
logger.log("[NavigationManager] NavigationPanel registered");
}
/**
@@ -133,19 +134,19 @@ export class NavigationManager {
callback: Function = null
): Node {
if (!panelType) {
console.error("[NavigationManager] switchToPanel: panelType is invalid");
logger.error("[NavigationManager] switchToPanel: panelType is invalid");
return null;
}
if (this.currentActivePanelType === panelType && !force) {
console.log(
logger.log(
`[NavigationManager] Already on ${panelType}, skipping switch`
);
return this.currentActivePanel;
}
if (!this.navigationPanel) {
console.warn(
logger.warn(
"[NavigationManager] NavigationPanel not registered, fallback to direct ViewManager call"
);
//this.fallbackSwitchPanel(panelType);
@@ -377,21 +378,21 @@ export class NavigationManager {
force: boolean = false
): Node {
if (!panelType) {
console.error(
logger.error(
"[NavigationManager] switchToPanelWithTransition: panelType is invalid"
);
return null;
}
if (this.currentActivePanelType === panelType && !force) {
console.log(
logger.log(
`[NavigationManager] Already on ${panelType}, skipping switch`
);
return this.currentActivePanel;
}
if (!this.navigationPanel) {
console.warn(
logger.warn(
"[NavigationManager] NavigationPanel not registered, fallback to direct ViewManager call"
);
return null;
@@ -545,13 +546,13 @@ export class NavigationManager {
callback?: Function
): void {
if (!panelName) {
console.error("[NavigationManager] openPopupPanel: panelName is empty");
logger.error("[NavigationManager] openPopupPanel: panelName is empty");
return;
}
const popupCallback = (panel: Node) => {
if (!panel || !panel.isValid) {
console.error(
logger.error(
"[NavigationManager] openPopupPanel: created panel is invalid"
);
return;
@@ -559,7 +560,7 @@ export class NavigationManager {
// 将弹窗添加到管理集合中
this.popupPanels.add(panel);
console.log(
logger.log(
`[NavigationManager] Popup panel opened: ${panelName}, total popups: ${this.popupPanels.size}`
);
@@ -577,14 +578,14 @@ export class NavigationManager {
*/
public closePopupPanel(panel: Node): void {
if (!panel || !panel.isValid) {
console.warn("[NavigationManager] closePopupPanel: panel is invalid");
logger.warn("[NavigationManager] closePopupPanel: panel is invalid");
return;
}
// 从弹窗管理集合中移除
if (this.popupPanels.has(panel)) {
this.popupPanels.delete(panel);
console.log(
logger.log(
`[NavigationManager] Popup panel closed: ${panel.name}, total popups: ${this.popupPanels.size}`
);
}
@@ -598,7 +599,7 @@ export class NavigationManager {
panel.destroy();
}
} catch (error) {
console.error("[NavigationManager] closePopupPanel error:", error);
logger.error("[NavigationManager] closePopupPanel error:", error);
}
}
@@ -610,7 +611,7 @@ export class NavigationManager {
return;
}
console.log(
logger.log(
`[NavigationManager] Closing all popups, count: ${this.popupPanels.size}`
);
@@ -631,7 +632,7 @@ export class NavigationManager {
animationConfig?: SubPanelAnimationConfig
) {
if (!panel) {
console.warn(
logger.warn(
"[NavigationManager] closeSubPanel: panel is null or undefined"
);
return;
@@ -794,7 +795,7 @@ export class NavigationManager {
// 不在这里调用Show(),因为ThemePanel已经有加载状态保护
// 只在必要时调用onHide()确保子页面状态正确
if (typeof themePanel.onHide === "function") {
console.log("[NavigationManager] 确保主题面板子页面状态正确");
logger.log("[NavigationManager] 确保主题面板子页面状态正确");
themePanel.onHide();
}
}
@@ -918,7 +919,7 @@ export class NavigationManager {
public setSelectedGirlId(girlId: number): void {
this.selectedGirlId = girlId;
sys.localStorage.setItem(NavigationManager.LastSelectGirlID, girlId);
console.log("[NavigationManager] Selected girl ID set to:", girlId);
logger.log("[NavigationManager] Selected girl ID set to:", girlId);
}
/**
@@ -935,7 +936,7 @@ export class NavigationManager {
*/
public setSelectedCategoryId(themeId: number): void {
this.selectedCategoryId = themeId;
console.log("[NavigationManager] Selected theme ID set to:", themeId);
logger.log("[NavigationManager] Selected theme ID set to:", themeId);
}
/**