1 |
|
2 |
|
3 |
|
4 |
|
5 | import { Utils } from './Utils'
|
6 | import * as BB from 'botbuilder'
|
7 | import * as CLM from '@conversationlearner/models'
|
8 |
|
9 | export enum DebugType {
|
10 | Client = 1 << 0,
|
11 | ClientBody = 1 << 1,
|
12 | MessageQueue = 1 << 2,
|
13 | Memory = 1 << 3,
|
14 | MemVerbose = 1 << 4
|
15 | }
|
16 |
|
17 | enum LogType {
|
18 | Log,
|
19 | Error
|
20 | }
|
21 |
|
22 | interface LogMessage {
|
23 | message: string
|
24 | logType: LogType
|
25 | }
|
26 |
|
27 | export class CLDebug {
|
28 | private static adapter: BB.BotAdapter
|
29 | private static conversationReference: Partial<BB.ConversationReference>
|
30 | private static cachedMessages: LogMessage[] = []
|
31 | public static logToUI: boolean = false
|
32 | public static verbose: boolean = true
|
33 | public static debugType: DebugType = 0
|
34 |
|
35 | public static InitLogger(adapter: BB.BotAdapter, conversationReference: Partial<BB.ConversationReference>) {
|
36 | CLDebug.adapter = adapter
|
37 | CLDebug.conversationReference = conversationReference;
|
38 | }
|
39 |
|
40 | private static HasDebugType(debugType: DebugType): boolean {
|
41 | return (debugType & this.debugType) === debugType
|
42 | }
|
43 | private static async SendCache() {
|
44 | if (CLDebug.adapter && CLDebug.cachedMessages.length > 0) {
|
45 |
|
46 |
|
47 | await CLDebug.adapter.continueConversation(CLDebug.conversationReference, async (context) => {
|
48 | let cachedMessages = [...this.cachedMessages]
|
49 | this.cachedMessages = []
|
50 |
|
51 | for (let logMessage of cachedMessages) {
|
52 | if (logMessage.logType === LogType.Error) {
|
53 |
|
54 | const clData: CLM.CLChannelData = { replayError: new CLM.ReplayErrorException(), senderType: CLM.SenderType.Bot, roundIndex: null, scoreIndex: null }
|
55 | await context.sendActivity({ text: logMessage.message, channelData: { clData: clData } })
|
56 | }
|
57 | else {
|
58 | await context.sendActivity(logMessage.message)
|
59 | }
|
60 | }
|
61 | });
|
62 | }
|
63 | }
|
64 |
|
65 | public static Log(text: string, filter?: DebugType) {
|
66 | if (!filter || this.HasDebugType(filter)) {
|
67 | console.log(text)
|
68 |
|
69 | if (CLDebug.logToUI) {
|
70 | CLDebug.cachedMessages.push({ message: text, logType: LogType.Log })
|
71 | }
|
72 | CLDebug.SendCache()
|
73 | }
|
74 | }
|
75 |
|
76 | public static LogRequest(method: string, path: string, payload: any) {
|
77 | if (this.HasDebugType(DebugType.Client)) {
|
78 |
|
79 |
|
80 | if (path.indexOf('trainingstatus') > 0) {
|
81 | return
|
82 | }
|
83 |
|
84 | let message = `${method} ${path}`
|
85 |
|
86 | if (this.HasDebugType(DebugType.ClientBody)) {
|
87 | const formattedBody = payload.body ? JSON.stringify(payload.body, null, ' ') : ''
|
88 | if (formattedBody.length > 0) {
|
89 | message = `${message}\n\n${formattedBody}`
|
90 | }
|
91 | }
|
92 | console.log(message)
|
93 |
|
94 | if (CLDebug.logToUI) {
|
95 | CLDebug.cachedMessages.push({ message: message, logType: LogType.Log })
|
96 | }
|
97 | CLDebug.SendCache()
|
98 | }
|
99 | }
|
100 |
|
101 | public static Error(error: any, context: string = "", sendAsChat: boolean = true): string {
|
102 | let text = `ERROR: ${error ? Utils.ErrorString(error, context) : 'No details'}`
|
103 |
|
104 | console.log(text)
|
105 |
|
106 | if (sendAsChat) {
|
107 | CLDebug.cachedMessages.push({ message: text, logType: LogType.Error })
|
108 | CLDebug.SendCache()
|
109 | }
|
110 |
|
111 | return text
|
112 | }
|
113 |
|
114 | public static Verbose(text: string) {
|
115 | if (CLDebug.verbose) {
|
116 | CLDebug.Log(`${text}`)
|
117 | }
|
118 | }
|
119 |
|
120 | public static LogObject(obj: any) {
|
121 | CLDebug.Log(JSON.stringify(obj))
|
122 | }
|
123 | }
|