about summary refs log tree commit diff stats
path: root/src/ui/notifier.c
blob: c2be3be7d5dd319f52e4f10fc316f4d9ae7e90b7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
 * notifier.c
 *
 * Copyright (C) 2012 - 2016 James Booth <boothj5@gmail.com>
 *
 * This file is part of Profanity.
 *
 * Profanity is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Profanity is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Profanity.  If not, see <https://www.gnu.org/licenses/>.
 *
 * In addition, as a special exception, the copyright holders give permission to
 * link the code of portions of this program with the OpenSSL library under
 * certain conditions as described in each individual source file, and
 * distribute linked combinations including the two.
 *
 * You must obey the GNU General Public License in all respects for all of the
 * code used other than OpenSSL. If you modify file(s) with this exception, you
 * may extend this exception to your version of the file(s), but you are not
 * obligated to do so. If you do not wish to do so, delete this exception
 * statement from your version. If you delete this exception statement from all
 * source files in the program, then also delete it here.
 *
 */
#include "config.h"

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <glib.h>

#ifdef HAVE_LIBNOTIFY
#include <libnotify/notify.h>
#endif

#ifdef PLATFORM_CYGWIN
#include <windows.h>
#endif

#include "log.h"
#include "config/preferences.h"
#include "ui/ui.h"
#include "ui/window_list.h"
#include "xmpp/xmpp.h"
#include "xmpp/muc.h"

static GTimer *remind_timer;

void
notifier_initialise(void)
{
    remind_timer = g_timer_new();
}

void
notifier_uninit(void)
{
#ifdef HAVE_LIBNOTIFY
    if (notify_is_initted()) {
        notify_uninit();
    }
#endif
    g_timer_destroy(remind_timer);
}

void
notify_typing(const char *const name)
{
    char message[strlen(name) + 1 + 11];
    sprintf(message, "%s: typing...", name);

    notify(message, 10000, "Incoming message");
}

void
notify_invite(const char *const from, const char *const room, const char *const reason)
{
    GString *message = g_string_new("Room invite\nfrom: ");
    g_string_append(message, from);
    g_string_append(message, "\nto: ");
    g_string_append(message, room);
    if (reason) {
        g_string_append_printf(message, "\n\"%s\"", reason);
    }

    notify(message->str, 10000, "Incoming message");

    g_string_free(message, TRUE);
}

void
notify_message(const char *const name, int num, const char *const text)
{
    int ui_index = num;
    if (ui_index == 10) {
        ui_index = 0;
    }

    GString *message = g_string_new("");
    g_string_append_printf(message, "%s (win %d)", name, ui_index);
    if (text && prefs_get_boolean(PREF_NOTIFY_CHAT_TEXT)) {
        g_string_append_printf(message, "\n%s", text);
    }

    notify(message->str, 10000, "incoming message");
    g_string_free(message, TRUE);
}

void
notify_room_message(const char *const nick, const char *const room, int num, const char *const text)
{
    int ui_index = num;
    if (ui_index == 10) {
        ui_index = 0;
    }

    GString *message = g_string_new("");
    g_string_append_printf(message, "%s in %s (win %d)", nick, room, ui_index);
    if (text && prefs_get_boolean(PREF_NOTIFY_ROOM_TEXT)) {
        g_string_append_printf(message, "\n%s", text);
    }

    notify(message->str, 10000, "incoming message");

    g_string_free(message, TRUE);
}

void
notify_subscription(const char *const from)
{
    GString *message = g_string_new("Subscription request: \n");
    g_string_append(message, from);
    notify(message->str, 10000, "Incoming message");
    g_string_free(message, TRUE);
}

void
notify_remind(void)
{
    gdouble elapsed = g_timer_elapsed(remind_timer, NULL);
    gint remind_period = prefs_get_notify_remind();
    if (remind_period > 0 && elapsed >= remind_period) {
        gboolean donotify = wins_do_notify_remind();
        gint unread = wins_get_total_unread();
        gint open = muc_invites_count();
        gint subs = presence_sub_request_count();

        GString *text = g_string_new("");

        if (donotify && unread > 0) {
            if (unread == 1) {
                g_string_append(text, "1 unread message");
            } else {
                g_string_append_printf(text, "%d unread messages", unread);
            }

        }
        if (open > 0) {
            if (unread > 0) {
                g_string_append(text, "\n");
            }
            if (open == 1) {
                g_string_append(text, "1 room invite");
            } else {
                g_string_append_printf(text, "%d room invites", open);
            }
        }
        if (subs > 0) {
            if ((unread > 0) || (open > 0)) {
                g_string_append(text, "\n");
            }
            if (subs == 1) {
                g_string_append(text, "1 subscription request");
            } else {
                g_string_append_printf(text, "%d subscription requests", subs);
            }
        }

        if ((donotify && unread > 0) || (open > 0) || (subs > 0)) {
            notify(text->str, 5000, "Incoming message");
        }

        g_string_free(text, TRUE);

        g_timer_start(remind_timer);
    }
}

void
notify(const char *const message, int timeout, const char *const category)
{
#ifdef HAVE_LIBNOTIFY
    log_debug("Attempting notification: %s", message);
    if (notify_is_initted()) {
        log_debug("Reinitialising libnotify");
        notify_uninit();
        notify_init("Profanity");
    } else {
        log_debug("Initialising libnotify");
        notify_init("Profanity");
    }
    if (notify_is_initted()) {
        NotifyNotification *notification;
        notification = notify_notification_new("Profanity", message, NULL);
        notify_notification_set_timeout(notification, timeout);
        notify_notification_set_category(notification, category);
        notify_notification_set_urgency(notification, NOTIFY_URGENCY_NORMAL);

        GError *error = NULL;
        gboolean notify_success = notify_notification_show(notification, &error);

        if (!notify_success) {
            log_error("Error sending desktop notification:");
            log_error("  -> Message : %s", message);
            log_error("  -> Error   : %s", error->message);
        } else {
	    log_debug("Notification sent.");
	}
    } else {
        log_error("Libnotify not initialised.");
    }
#endif
#ifdef PLATFORM_CYGWIN
    NOTIFYICONDATA nid;
    nid.cbSize = sizeof(NOTIFYICONDATA);
    //nid.hWnd = hWnd;
    nid.uID = 100;
    nid.uVersion = NOTIFYICON_VERSION;
    //nid.uCallbackMessage = WM_MYMESSAGE;
    nid.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    strncpy(nid.szTip, "Tray Icon", 10);
    nid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
    Shell_NotifyIcon(NIM_ADD, &nid);

    // For a Ballon Tip
    nid.uFlags = NIF_INFO;
    strncpy(nid.szInfoTitle, "Profanity", 10); // Title
    strncpy(nid.szInfo, message, 256); // Copy Tip
    nid.uTimeout = timeout;  // 3 Seconds
    nid.dwInfoFlags = NIIF_INFO;

    Shell_NotifyIcon(NIM_MODIFY, &nid);
#endif
#ifdef HAVE_OSXNOTIFY
    GString *notify_command = g_string_new("terminal-notifier -title \"Profanity\" -message '");

    char *escaped_single = str_replace(message, "'", "'\\''");

    if (escaped_single[0] == '<') {
        g_string_append(notify_command, "\\<");
        g_string_append(notify_command, &escaped_single[1]);
    } else if (escaped_single[0] == '[') {
        g_string_append(notify_command, "\\[");
        g_string_append(notify_command, &escaped_single[1]);
    } else if (escaped_single[0] == '(') {
        g_string_append(notify_command, "\\(");
        g_string_append(notify_command, &escaped_single[1]);
    } else if (escaped_single[0] == '{') {
        g_string_append(notify_command, "\\{");
        g_string_append(notify_command, &escaped_single[1]);
    } else {
        g_string_append(notify_command, escaped_single);
    }

    g_string_append(notify_command, "'");
    free(escaped_single);

    char *term_name = getenv("TERM_PROGRAM");
    char *app_id = NULL;
    if (g_strcmp0(term_name, "Apple_Terminal") == 0) {
        app_id = "com.apple.Terminal";
    } else if (g_strcmp0(term_name, "iTerm.app") == 0) {
        app_id = "com.googlecode.iterm2";
    }

    if (app_id) {
        g_string_append(notify_command, " -sender ");
        g_string_append(notify_command, app_id);
    }

    int res = system(notify_command->str);
    if (res == -1) {
        log_error("Could not send desktop notificaion.");
    }

    g_string_free(notify_command, TRUE);
#endif
}
trType then -- Check if the specified object. if cExistTag[cObject] and (not cNameAllAlias[strName]) then cNameAllAlias[strName] = true end -- Add reference and name. if cAccessTag[cObject] then return end -- Get this name. cAccessTag[cObject] = true -- Dump environment table. local getfenv = debug.getfenv if getfenv then local cEnv = getfenv(cObject) if cEnv then CollectSingleObjectReferenceInMemory(strName ..".[thread:environment]", cEnv, cDumpInfoContainer) end end -- Dump metatable. local cMt = getmetatable(cObject) if cMt then CollectSingleObjectReferenceInMemory(strName ..".[thread:metatable]", cMt, cDumpInfoContainer) end elseif "userdata" == strType then -- Check if the specified object. if cExistTag[cObject] and (not cNameAllAlias[strName]) then cNameAllAlias[strName] = true end -- Add reference and name. if cAccessTag[cObject] then return end -- Get this name. cAccessTag[cObject] = true -- Dump environment table. local getfenv = debug.getfenv if getfenv then local cEnv = getfenv(cObject) if cEnv then CollectSingleObjectReferenceInMemory(strName ..".[userdata:environment]", cEnv, cDumpInfoContainer) end end -- Dump metatable. local cMt = getmetatable(cObject) if cMt then CollectSingleObjectReferenceInMemory(strName ..".[userdata:metatable]", cMt, cDumpInfoContainer) end elseif "string" == strType then -- Check if the specified object. if cExistTag[cObject] and (not cNameAllAlias[strName]) then cNameAllAlias[strName] = true end -- Add reference and name. if cAccessTag[cObject] then return end -- Get this name. cAccessTag[cObject] = true else -- For "number" and "boolean" type, they are not object type, skip. end end -- The base method to dump a mem ref info result into a file. -- strSavePath - The save path of the file to store the result, must be a directory path, If nil or "" then the result will output to console as print does. -- strExtraFileName - If you want to add extra info append to the end of the result file, give a string, nothing will do if set to nil or "". -- nMaxRescords - How many rescords of the results in limit to save in the file or output to the console, -1 will give all the result. -- strRootObjectName - The header info to show the root object name, can be nil. -- cRootObject - The header info to show the root object address, can be nil. -- cDumpInfoResultsBase - The base dumped mem info result, nil means no compare and only output cDumpInfoResults, otherwise to compare with cDumpInfoResults. -- cDumpInfoResults - The compared dumped mem info result, dump itself only if cDumpInfoResultsBase is nil, otherwise dump compared results with cDumpInfoResultsBase. local function OutputMemorySnapshot(strSavePath, strExtraFileName, nMaxRescords, strRootObjectName, cRootObject, cDumpInfoResultsBase, cDumpInfoResults) -- Check results. if not cDumpInfoResults then return end -- Get time format string. local strDateTime = FormatDateTimeNow() -- Collect memory info. local cRefInfoBase = (cDumpInfoResultsBase and cDumpInfoResultsBase.m_cObjectReferenceCount) or nil local cNameInfoBase = (cDumpInfoResultsBase and cDumpInfoResultsBase.m_cObjectAddressToName) or nil local cRefInfo = cDumpInfoResults.m_cObjectReferenceCount local cNameInfo = cDumpInfoResults.m_cObjectAddressToName -- Create a cache result to sort by ref count. local cRes = {} local nIdx = 0 for k in pairs(cRefInfo) do nIdx = nIdx + 1 cRes[nIdx] = k end -- Sort result. table.sort(cRes, function (l, r) return cRefInfo[l] > cRefInfo[r] end) -- Save result to file. local bOutputFile = strSavePath and (string.len(strSavePath) > 0) local cOutputHandle = nil local cOutputEntry = print if bOutputFile then -- Check save path affix. local strAffix = string.sub(strSavePath, -1) if ("/" ~= strAffix) and ("\\" ~= strAffix) then strSavePath = strSavePath .. "/" end -- Combine file name. local strFileName = strSavePath .. "LuaMemRefInfo-All" if (not strExtraFileName) or (0 == string.len(strExtraFileName)) then if cDumpInfoResultsBase then if cConfig.m_bComparedMemoryRefFileAddTime then strFileName = strFileName .. "-[" .. strDateTime .. "].txt" else strFileName = strFileName .. ".txt" end else if cConfig.m_bAllMemoryRefFileAddTime then strFileName = strFileName .. "-[" .. strDateTime .. "].txt" else strFileName = strFileName .. ".txt" end end else if cDumpInfoResultsBase then if cConfig.m_bComparedMemoryRefFileAddTime then strFileName = strFileName .. "-[" .. strDateTime .. "]-[" .. strExtraFileName .. "].txt" else strFileName = strFileName .. "-[" .. strExtraFileName .. "].txt" end else if cConfig.m_bAllMemoryRefFileAddTime then strFileName = strFileName .. "-[" .. strDateTime .. "]-[" .. strExtraFileName .. "].txt" else strFileName = strFileName .. "-[" .. strExtraFileName .. "].txt" end end end local cFile = assert(io.open(strFileName, "w")) cOutputHandle = cFile cOutputEntry = cFile.write end local cOutputer = function (strContent) if cOutputHandle then cOutputEntry(cOutputHandle, strContent) else cOutputEntry(strContent) end end -- Write table header. if cDumpInfoResultsBase then cOutputer("--------------------------------------------------------\n") cOutputer("-- This is compared memory information.\n") cOutputer("--------------------------------------------------------\n") cOutputer("-- Collect base memory reference at line:" .. tostring(cDumpInfoResultsBase.m_nCurrentLine) .. "@file:" .. cDumpInfoResultsBase.m_strShortSrc .. "\n") cOutputer("-- Collect compared memory reference at line:" .. tostring(cDumpInfoResults.m_nCurrentLine) .. "@file:" .. cDumpInfoResults.m_strShortSrc .. "\n") else cOutputer("--------------------------------------------------------\n") cOutputer("-- Collect memory reference at line:" .. tostring(cDumpInfoResults.m_nCurrentLine) .. "@file:" .. cDumpInfoResults.m_strShortSrc .. "\n") end cOutputer("--------------------------------------------------------\n") cOutputer("-- [Table/Function/String Address/Name]\t[Reference Path]\t[Reference Count]\n") cOutputer("--------------------------------------------------------\n") if strRootObjectName and cRootObject then if "string" == type(cRootObject) then cOutputer("-- From Root Object: \"" .. tostring(cRootObject) .. "\" (" .. strRootObjectName .. ")\n") else cOutputer("-- From Root Object: " .. GetOriginalToStringResult(cRootObject) .. " (" .. strRootObjectName .. ")\n") end end -- Save each info. for i, v in ipairs(cRes) do if (not cDumpInfoResultsBase) or (not cRefInfoBase[v]) then if (nMaxRescords > 0) then if (i <= nMaxRescords) then if "string" == type(v) then local strOrgString = tostring(v) local nPattenBegin, nPattenEnd = string.find(strOrgString, "string: \".*\"") if ((not cDumpInfoResultsBase) and ((nil == nPattenBegin) or (nil == nPattenEnd))) then local strRepString = string.gsub(strOrgString, "([\n\r])", "\\n") cOutputer("string: \"" .. strRepString .. "\"\t" .. cNameInfo[v] .. "\t" .. tostring(cRefInfo[v]) .. "\n") else cOutputer(tostring(v) .. "\t" .. cNameInfo[v] .. "\t" .. tostring(cRefInfo[v]) .. "\n") end else cOutputer(GetOriginalToStringResult(v) .. "\t" .. cNameInfo[v] .. "\t" .. tostring(cRefInfo[v]) .. "\n") end end else if "string" == type(v) then local strOrgString = tostring(v) local nPattenBegin, nPattenEnd = string.find(strOrgString, "string: \".*\"") if ((not cDumpInfoResultsBase) and ((nil == nPattenBegin) or (nil == nPattenEnd))) then local strRepString = string.gsub(strOrgString, "([\n\r])", "\\n") cOutputer("string: \"" .. strRepString .. "\"\t" .. cNameInfo[v] .. "\t" .. tostring(cRefInfo[v]) .. "\n") else cOutputer(tostring(v) .. "\t" .. cNameInfo[v] .. "\t" .. tostring(cRefInfo[v]) .. "\n") end else cOutputer(GetOriginalToStringResult(v) .. "\t" .. cNameInfo[v] .. "\t" .. tostring(cRefInfo[v]) .. "\n") end end end end if bOutputFile then io.close(cOutputHandle) cOutputHandle = nil end end -- The base method to dump a mem ref info result of a single object into a file. -- strSavePath - The save path of the file to store the result, must be a directory path, If nil or "" then the result will output to console as print does. -- strExtraFileName - If you want to add extra info append to the end of the result file, give a string, nothing will do if set to nil or "". -- nMaxRescords - How many rescords of the results in limit to save in the file or output to the console, -1 will give all the result. -- cDumpInfoResults - The dumped results. local function OutputMemorySnapshotSingleObject(strSavePath, strExtraFileName, nMaxRescords, cDumpInfoResults) -- Check results. if not cDumpInfoResults then return end -- Get time format string. local strDateTime = FormatDateTimeNow() -- Collect memory info. local cObjectAliasName = cDumpInfoResults.m_cObjectAliasName -- Save result to file. local bOutputFile = strSavePath and (string.len(strSavePath) > 0) local cOutputHandle = nil local cOutputEntry = print if bOutputFile then -- Check save path affix. local strAffix = string.sub(strSavePath, -1) if ("/" ~= strAffix) and ("\\" ~= strAffix) then strSavePath = strSavePath .. "/" end -- Combine file name. local strFileName = strSavePath .. "LuaMemRefInfo-Single" if (not strExtraFileName) or (0 == string.len(strExtraFileName)) then if cConfig.m_bSingleMemoryRefFileAddTime then strFileName = strFileName .. "-[" .. strDateTime .. "].txt" else strFileName = strFileName .. ".txt" end else if cConfig.m_bSingleMemoryRefFileAddTime then strFileName = strFileName .. "-[" .. strDateTime .. "]-[" .. strExtraFileName .. "].txt" else strFileName = strFileName .. "-[" .. strExtraFileName .. "].txt" end end local cFile = assert(io.open(strFileName, "w")) cOutputHandle = cFile cOutputEntry = cFile.write end local cOutputer = function (strContent) if cOutputHandle then cOutputEntry(cOutputHandle, strContent) else cOutputEntry(strContent) end end -- Write table header. cOutputer("--------------------------------------------------------\n") cOutputer("-- Collect single object memory reference at line:" .. tostring(cDumpInfoResults.m_nCurrentLine) .. "@file:" .. cDumpInfoResults.m_strShortSrc .. "\n") cOutputer("--------------------------------------------------------\n") -- Calculate reference count. local nCount = 0 for k in pairs(cObjectAliasName) do nCount = nCount + 1 end -- Output reference count. cOutputer("-- For Object: " .. cDumpInfoResults.m_strAddressName .. " (" .. cDumpInfoResults.m_strObjectName .. "), have " .. tostring(nCount) .. " reference in total.\n") cOutputer("--------------------------------------------------------\n") -- Save each info. for k in pairs(cObjectAliasName) do if (nMaxRescords > 0) then if (i <= nMaxRescords) then cOutputer(k .. "\n") end else cOutputer(k .. "\n") end end if bOutputFile then io.close(cOutputHandle) cOutputHandle = nil end end -- Fileter an existing result file and output it. -- strFilePath - The existing result file. -- strFilter - The filter string. -- bIncludeFilter - Include(true) or exclude(false) the filter. -- bOutputFile - Output to file(true) or console(false). local function OutputFilteredResult(strFilePath, strFilter, bIncludeFilter, bOutputFile) if (not strFilePath) or (0 == string.len(strFilePath)) then print("You need to specify a file path.") return end if (not strFilter) or (0 == string.len(strFilter)) then print("You need to specify a filter string.") return end -- Read file. local cFilteredResult = {} local cReadFile = assert(io.open(strFilePath, "rb")) for strLine in cReadFile:lines() do local nBegin, nEnd = string.find(strLine, strFilter) if nBegin and nEnd then if bIncludeFilter then nBegin, nEnd = string.find(strLine, "[\r\n]") if nBegin and nEnd and (string.len(strLine) == nEnd) then table.insert(cFilteredResult, string.sub(strLine, 1, nBegin - 1)) else table.insert(cFilteredResult, strLine) end end else if not bIncludeFilter then nBegin, nEnd = string.find(strLine, "[\r\n]") if nBegin and nEnd and (string.len(strLine) == nEnd) then table.insert(cFilteredResult, string.sub(strLine, 1, nBegin - 1)) else table.insert(cFilteredResult, strLine) end end end end -- Close and clear read file handle. io.close(cReadFile) cReadFile = nil -- Write filtered result. local cOutputHandle = nil local cOutputEntry = print if bOutputFile then -- Combine file name. local _, _, strResFileName = string.find(strFilePath, "(.*)%.txt") strResFileName = strResFileName .. "-Filter-" .. ((bIncludeFilter and "I") or "E") .. "-[" .. strFilter .. "].txt" local cFile = assert(io.open(strResFileName, "w")) cOutputHandle = cFile cOutputEntry = cFile.write end local cOutputer = function (strContent) if cOutputHandle then cOutputEntry(cOutputHandle, strContent) else cOutputEntry(strContent) end end -- Output result. for i, v in ipairs(cFilteredResult) do cOutputer(v .. "\n") end if bOutputFile then io.close(cOutputHandle) cOutputHandle = nil end end -- Dump memory reference at current time. -- strSavePath - The save path of the file to store the result, must be a directory path, If nil or "" then the result will output to console as print does. -- strExtraFileName - If you want to add extra info append to the end of the result file, give a string, nothing will do if set to nil or "". -- nMaxRescords - How many rescords of the results in limit to save in the file or output to the console, -1 will give all the result. -- strRootObjectName - The root object name that start to search, default is "_G" if leave this to nil. -- cRootObject - The root object that start to search, default is _G if leave this to nil. local function DumpMemorySnapshot(strSavePath, strExtraFileName, nMaxRescords, strRootObjectName, cRootObject) -- Get time format string. local strDateTime = FormatDateTimeNow() -- Check root object. if cRootObject then if (not strRootObjectName) or (0 == string.len(strRootObjectName)) then strRootObjectName = tostring(cRootObject) end else cRootObject = debug.getregistry() strRootObjectName = "registry" end -- Create container. local cDumpInfoContainer = CreateObjectReferenceInfoContainer() local cStackInfo = debug.getinfo(2, "Sl") if cStackInfo then cDumpInfoContainer.m_strShortSrc = cStackInfo.short_src cDumpInfoContainer.m_nCurrentLine = cStackInfo.currentline end -- Collect memory info. CollectObjectReferenceInMemory(strRootObjectName, cRootObject, cDumpInfoContainer) -- Dump the result. OutputMemorySnapshot(strSavePath, strExtraFileName, nMaxRescords, strRootObjectName, cRootObject, nil, cDumpInfoContainer) end -- Dump compared memory reference results generated by DumpMemorySnapshot. -- strSavePath - The save path of the file to store the result, must be a directory path, If nil or "" then the result will output to console as print does. -- strExtraFileName - If you want to add extra info append to the end of the result file, give a string, nothing will do if set to nil or "". -- nMaxRescords - How many rescords of the results in limit to save in the file or output to the console, -1 will give all the result. -- cResultBefore - The base dumped results. -- cResultAfter - The compared dumped results. local function DumpMemorySnapshotCompared(strSavePath, strExtraFileName, nMaxRescords, cResultBefore, cResultAfter) -- Dump the result. OutputMemorySnapshot(strSavePath, strExtraFileName, nMaxRescords, nil, nil, cResultBefore, cResultAfter) end -- Dump compared memory reference file results generated by DumpMemorySnapshot. -- strSavePath - The save path of the file to store the result, must be a directory path, If nil or "" then the result will output to console as print does. -- strExtraFileName - If you want to add extra info append to the end of the result file, give a string, nothing will do if set to nil or "". -- nMaxRescords - How many rescords of the results in limit to save in the file or output to the console, -1 will give all the result. -- strResultFilePathBefore - The base dumped results file. -- strResultFilePathAfter - The compared dumped results file. local function DumpMemorySnapshotComparedFile(strSavePath, strExtraFileName, nMaxRescords, strResultFilePathBefore, strResultFilePathAfter) -- Read results from file. local cResultBefore = CreateObjectReferenceInfoContainerFromFile(strResultFilePathBefore) local cResultAfter = CreateObjectReferenceInfoContainerFromFile(strResultFilePathAfter) -- Dump the result. OutputMemorySnapshot(strSavePath, strExtraFileName, nMaxRescords, nil, nil, cResultBefore, cResultAfter) end -- Dump memory reference of a single object at current time. -- strSavePath - The save path of the file to store the result, must be a directory path, If nil or "" then the result will output to console as print does. -- strExtraFileName - If you want to add extra info append to the end of the result file, give a string, nothing will do if set to nil or "". -- nMaxRescords - How many rescords of the results in limit to save in the file or output to the console, -1 will give all the result. -- strObjectName - The object name reference you want to dump. -- cObject - The object reference you want to dump. local function DumpMemorySnapshotSingleObject(strSavePath, strExtraFileName, nMaxRescords, strObjectName, cObject) -- Check object. if not cObject then return end if (not strObjectName) or (0 == string.len(strObjectName)) then strObjectName = GetOriginalToStringResult(cObject) end -- Get time format string. local strDateTime = FormatDateTimeNow() -- Create container. local cDumpInfoContainer = CreateSingleObjectReferenceInfoContainer(strObjectName, cObject) local cStackInfo = debug.getinfo(2, "Sl") if cStackInfo then cDumpInfoContainer.m_strShortSrc = cStackInfo.short_src cDumpInfoContainer.m_nCurrentLine = cStackInfo.currentline end -- Collect memory info. CollectSingleObjectReferenceInMemory("registry", debug.getregistry(), cDumpInfoContainer) -- Dump the result. OutputMemorySnapshotSingleObject(strSavePath, strExtraFileName, nMaxRescords, cDumpInfoContainer) end -- Return methods. local cPublications = {m_cConfig = nil, m_cMethods = {}, m_cHelpers = {}, m_cBases = {}} cPublications.m_cConfig = cConfig cPublications.m_cMethods.DumpMemorySnapshot = DumpMemorySnapshot cPublications.m_cMethods.DumpMemorySnapshotCompared = DumpMemorySnapshotCompared cPublications.m_cMethods.DumpMemorySnapshotComparedFile = DumpMemorySnapshotComparedFile cPublications.m_cMethods.DumpMemorySnapshotSingleObject = DumpMemorySnapshotSingleObject cPublications.m_cHelpers.FormatDateTimeNow = FormatDateTimeNow cPublications.m_cHelpers.GetOriginalToStringResult = GetOriginalToStringResult cPublications.m_cBases.CreateObjectReferenceInfoContainer = CreateObjectReferenceInfoContainer cPublications.m_cBases.CreateObjectReferenceInfoContainerFromFile = CreateObjectReferenceInfoContainerFromFile cPublications.m_cBases.CreateSingleObjectReferenceInfoContainer = CreateSingleObjectReferenceInfoContainer cPublications.m_cBases.CollectObjectReferenceInMemory = CollectObjectReferenceInMemory cPublications.m_cBases.CollectSingleObjectReferenceInMemory = CollectSingleObjectReferenceInMemory cPublications.m_cBases.OutputMemorySnapshot = OutputMemorySnapshot cPublications.m_cBases.OutputMemorySnapshotSingleObject = OutputMemorySnapshotSingleObject cPublications.m_cBases.OutputFilteredResult = OutputFilteredResult return cPublications