Add function to set custom host url. Deprecate email callback function. Add some comments

This commit is contained in:
kaetemi 2015-02-23 20:01:20 +01:00
parent ff010bc5d8
commit fa9da7caf8
2 changed files with 85 additions and 100 deletions

View file

@ -21,9 +21,11 @@
namespace NLMISC { namespace NLMISC {
enum TReportResult { ReportDebug, ReportIgnore, ReportQuit, ReportError };
/** Display a custom message box. /** Display a custom message box.
* *
* \param title set the title of the report. If empty, it'll display "NeL report". * \param title set the title of the report. If empty, it'll display "NeL Crash Report" or the default title set by setReportWindowTitle.
* \param header message displayed before the edit text box. If empty, it displays the default message. * \param header message displayed before the edit text box. If empty, it displays the default message.
* \param body message displayed in the edit text box. This string will be sent by email. * \param body message displayed in the edit text box. This string will be sent by email.
* \param debugButton 0 for disabling it, 1 for enable with default behaviors (generate a breakpoint), 2 for enable with no behavior * \param debugButton 0 for disabling it, 1 for enable with default behaviors (generate a breakpoint), 2 for enable with no behavior
@ -32,11 +34,12 @@ namespace NLMISC {
* *
* \return the button clicked or error * \return the button clicked or error
*/ */
enum TReportResult { ReportDebug, ReportIgnore, ReportQuit, ReportError };
TReportResult report(const std::string &title, const std::string &header, const std::string &subject, const std::string &body, bool enableCheckIgnore, uint debugButton, bool ignoreButton, sint quitButton, bool sendReportButton, bool &ignoreNextTime, const std::string &attachedFile = ""); TReportResult report(const std::string &title, const std::string &header, const std::string &subject, const std::string &body, bool enableCheckIgnore, uint debugButton, bool ignoreButton, sint quitButton, bool sendReportButton, bool &ignoreNextTime, const std::string &attachedFile = "");
/// Set the Url of the web service used to post crash reports to
void setReportPostUrl(const std::string &postUrl);
/// DEPRECATED
/** call this in the main of your appli to enable email: setReportEmailFunction (sendEmail); /** call this in the main of your appli to enable email: setReportEmailFunction (sendEmail);
*/ */
void setReportEmailFunction(void *emailFunction); void setReportEmailFunction(void *emailFunction);

View file

@ -16,6 +16,8 @@
#include "stdmisc.h" #include "stdmisc.h"
#include <stringstream>
#include "nel/misc/common.h" #include "nel/misc/common.h"
#include "nel/misc/ucstring.h" #include "nel/misc/ucstring.h"
@ -42,58 +44,60 @@ using namespace std;
namespace NLMISC namespace NLMISC
{ {
#ifdef NL_OS_WINDOWS
static HWND sendReport=NULL;
#endif
//old doesn't work on visual c++ 7.1 due to default parameter typedef bool (*TEmailFunction) (const std::string &smtpServer, const std::string &from, const std::string &to, const std::string &subject, const std::string &body, const std::string &attachedFile = "", bool onlyCheck = false);
typedef bool (*TEmailFunction) (const std::string &smtpServer, const std::string &from, const std::string &to, const std::string &subject, const std::string &body, const std::string &attachedFile, bool onlyCheck);
#define DELETE_OBJECT(a) if((a)!=NULL) { DeleteObject (a); a = NULL; }
static TEmailFunction EmailFunction = NULL;
void setReportEmailFunction(void *emailFunction) void setReportEmailFunction(void *emailFunction)
{ {
EmailFunction = (TEmailFunction)emailFunction; // DEPRECATED
// no-op
#ifdef NL_OS_WINDOWS
if (sendReport)
EnableWindow(sendReport, FALSE);
#endif
} }
static string Body; // Contents of crash report
static std::string URL = "FILL_IN_CRASH_REPORT_HOSTNAME_HERE"; static string ReportBody;
// Host url for crash report
static std::string ReportPostUrl = "";
// Title for the crash report window
static std::string ReportWindowTitle = "";
void setReportPostUrl(const std::string &postUrl)
{
ReportPostUrl = postUrl;
}
// Launch the crash report application
static void doSendReport() static void doSendReport()
{ {
std::string filename; std::string filename;
filename = "report_"; filename = /*getLogDirectory() + */ "report_"; // FIXME: Should use log directory
filename += NLMISC::toString( int( time( NULL ) ) ); filename += NLMISC::toString( int( time( NULL ) ) );
filename += ".txt"; filename += ".txt";
std::string params; std::stringstream params;
params = "-log "; params << "-log ";
params += filename; params << filename; // FIXME: Escape the filepath with quotes
params += " -host "; if (!ReportPostUrl.empty())
params += URL; {
params << " -host ";
params << ReportPostUrl;
}
if (!ReportWindowTitle.empty())
{
params << " -title ";
params << ReportWindowTitle; // FIXME: Escape the title with quotes and test
}
std::ofstream f; std::ofstream f;
f.open( filename.c_str() ); f.open( filename.c_str() );
if( !f.good() ) if( !f.good() )
return; return;
f << Body; f << ReportBody;
f.close(); f.close();
#ifdef NL_OS_WINDOWS #ifdef NL_OS_WINDOWS
NLMISC::launchProgram( "crash_report.exe", params ); NLMISC::launchProgram( "crash_report.exe", params.str() );
#else #else
NLMISC::launchProgram( "crash_report", params ); NLMISC::launchProgram( "crash_report", params.str() );
#endif #endif
// Added because NLSMIC::launcProgram needs time to launch // Added because NLSMIC::launcProgram needs time to launch
@ -101,25 +105,36 @@ static void doSendReport()
} }
#ifndef NL_OS_WINDOWS #if defined(FINAL_VERSION) || !defined(NL_OS_WINDOWS)
// GNU/Linux, do nothing
// For FINAL_VERSION, simply launch the crash report and exit the application
TReportResult report(const std::string &title, const std::string &header, const std::string &subject, const std::string &body, bool enableCheckIgnore, uint debugButton, bool ignoreButton, sint quitButton, bool sendReportButton, bool &ignoreNextTime, const string &attachedFile) TReportResult report(const std::string &title, const std::string &header, const std::string &subject, const std::string &body, bool enableCheckIgnore, uint debugButton, bool ignoreButton, sint quitButton, bool sendReportButton, bool &ignoreNextTime, const string &attachedFile)
{ {
Body = addSlashR( body ); ReportWindowTitle = title;
ReportBody = addSlashR(body);
doSendReport(); doSendReport();
# if 1 // TODO: This behaviour is used in the old report code when Quitting the application is the default crash report behaviour. Needs testing.
# ifdef NL_OS_WINDOWS
# ifndef NL_COMP_MINGW
// disable the Windows popup telling that the application aborted and disable the dr watson report.
_set_abort_behavior(0, _WRITE_ABORT_MSG | _CALL_REPORTFAULT);
# endif
# endif
// quit without calling atexit or static object dtors.
abort();
# endif
return ReportQuit; return ReportQuit;
} }
#else #else
// Windows specific version // Windows specific version for DEV builds, first shows a dialog box for debugging
static string Subject; static HWND sendReport=NULL;
static string AttachedFile; #define DELETE_OBJECT(a) if((a)!=NULL) { DeleteObject (a); a = NULL; }
static HWND checkIgnore = NULL; static HWND checkIgnore = NULL;
static HWND debug = NULL; static HWND debug = NULL;
@ -134,33 +149,21 @@ static bool CanSendMailReport= false;
static bool DebugDefaultBehavior, QuitDefaultBehavior; static bool DebugDefaultBehavior, QuitDefaultBehavior;
static void sendEmail() static void maybeSendReport()
{ {
if (CanSendMailReport && SendMessage(sendReport, BM_GETCHECK, 0, 0) != BST_CHECKED) if (CanSendMailReport && SendMessage(sendReport, BM_GETCHECK, 0, 0) != BST_CHECKED)
{ {
bool res = EmailFunction ("", "", "", Subject, Body, AttachedFile, false); doSendReport();
if (res)
{
// EnableWindow(sendReport, FALSE);
// MessageBox (dialog, "The email was successfully sent", "email", MB_OK);
#ifndef NL_NO_DEBUG_FILES #ifndef NL_NO_DEBUG_FILES
CFile::createEmptyFile(getLogDirectory() + "report_sent"); CFile::createEmptyFile(getLogDirectory() + "report_sent");
#endif #endif
} }
else else
{ {
#ifndef NL_NO_DEBUG_FILES
CFile::createEmptyFile(getLogDirectory() + "report_failed");
#endif
// MessageBox (dialog, "Failed to send the email", "email", MB_OK | MB_ICONERROR);
}
}
else
{
#ifndef NL_NO_DEBUG_FILES #ifndef NL_NO_DEBUG_FILES
CFile::createEmptyFile(getLogDirectory() + "report_refused"); CFile::createEmptyFile(getLogDirectory() + "report_refused");
#endif #endif
} - }
} }
static LRESULT CALLBACK WndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) static LRESULT CALLBACK WndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
@ -175,7 +178,7 @@ static LRESULT CALLBACK WndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM
} }
else if ((HWND) lParam == debug) else if ((HWND) lParam == debug)
{ {
doSendReport(); maybeSendReport();
NeedExit = true; NeedExit = true;
Result = ReportDebug; Result = ReportDebug;
if (DebugDefaultBehavior) if (DebugDefaultBehavior)
@ -185,13 +188,13 @@ static LRESULT CALLBACK WndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM
} }
else if ((HWND) lParam == ignore) else if ((HWND) lParam == ignore)
{ {
doSendReport(); maybeSendReport();
NeedExit = true; NeedExit = true;
Result = ReportIgnore; Result = ReportIgnore;
} }
else if ((HWND) lParam == quit) else if ((HWND) lParam == quit)
{ {
doSendReport(); maybeSendReport();
NeedExit = true; NeedExit = true;
Result = ReportQuit; Result = ReportQuit;
@ -210,30 +213,13 @@ static LRESULT CALLBACK WndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM
abort(); abort();
} }
} }
/*else if ((HWND) lParam == sendReport)
{
if (EmailFunction != NULL)
{
bool res = EmailFunction ("", "", "", Subject, Body, AttachedFile, false);
if (res)
{
EnableWindow(sendReport, FALSE);
MessageBox (dialog, "The email was successfully sent", "email", MB_OK);
CFile::createEmptyFile(getLogDirectory() + "report_sent");
}
else
{
MessageBox (dialog, "Failed to send the email", "email", MB_OK | MB_ICONERROR);
}
}
}*/
} }
else if (message == WM_CHAR) else if (message == WM_CHAR)
{ {
if (wParam == 27) if (wParam == 27)
{ {
// ESC -> ignore // ESC -> ignore
doSendReport(); maybeSendReport();
NeedExit = true; NeedExit = true;
Result = ReportIgnore; Result = ReportIgnore;
} }
@ -264,8 +250,8 @@ TReportResult report (const std::string &title, const std::string &header, const
AlreadyRegister = true; AlreadyRegister = true;
} }
ucstring formatedTitle = title.empty() ? ucstring("NeL report") : ucstring(title); ReportWindowTitle = title.empty() ? "Nel Crash Report" : title;
ucstring formatedTitle = ucstring::makeFromUtf8(ReportWindowTitle);
// create the window // create the window
dialog = CreateWindowW (L"NLReportWindow", (LPCWSTR)formatedTitle.c_str(), WS_DLGFRAME | WS_CAPTION /*| WS_THICKFRAME*/, CW_USEDEFAULT, CW_USEDEFAULT, 456, 400, NULL, NULL, GetModuleHandle(NULL), NULL); dialog = CreateWindowW (L"NLReportWindow", (LPCWSTR)formatedTitle.c_str(), WS_DLGFRAME | WS_CAPTION /*| WS_THICKFRAME*/, CW_USEDEFAULT, CW_USEDEFAULT, 456, 400, NULL, NULL, GetModuleHandle(NULL), NULL);
@ -273,9 +259,6 @@ TReportResult report (const std::string &title, const std::string &header, const
// create the font // create the font
HFONT font = CreateFont (-12, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "Arial"); HFONT font = CreateFont (-12, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "Arial");
Subject = subject;
AttachedFile = attachedFile;
// create the edit control // create the edit control
HWND edit = CreateWindowW (L"EDIT", NULL, WS_BORDER | WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL | ES_READONLY | ES_LEFT | ES_MULTILINE, 7, 70, 429, 212, dialog, (HMENU) NULL, (HINSTANCE) GetWindowLongPtr(dialog, GWLP_HINSTANCE), NULL); HWND edit = CreateWindowW (L"EDIT", NULL, WS_BORDER | WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL | ES_READONLY | ES_LEFT | ES_MULTILINE, 7, 70, 429, 212, dialog, (HMENU) NULL, (HINSTANCE) GetWindowLongPtr(dialog, GWLP_HINSTANCE), NULL);
SendMessage (edit, WM_SETFONT, (WPARAM) font, TRUE); SendMessage (edit, WM_SETFONT, (WPARAM) font, TRUE);
@ -283,10 +266,10 @@ TReportResult report (const std::string &title, const std::string &header, const
// set the edit text limit to lot of :) // set the edit text limit to lot of :)
SendMessage (edit, EM_LIMITTEXT, ~0U, 0); SendMessage (edit, EM_LIMITTEXT, ~0U, 0);
Body = addSlashR (body); ReportBody = addSlashR(body);
// set the message in the edit text // set the message in the edit text
SendMessage (edit, WM_SETTEXT, (WPARAM)0, (LPARAM)Body.c_str()); SendMessage (edit, WM_SETTEXT, (WPARAM)0, (LPARAM)ReportBody.c_str());
if (enableCheckIgnore) if (enableCheckIgnore)
{ {
@ -336,8 +319,7 @@ TReportResult report (const std::string &title, const std::string &header, const
} }
// ace don't do that because it s slow to try to send a mail // ace don't do that because it s slow to try to send a mail
//CanSendMailReport = sendReportButton && EmailFunction != NULL && EmailFunction("", "", "", "", "", true); CanSendMailReport = sendReportButton && !ReportPostUrl.empty();
CanSendMailReport = sendReportButton && EmailFunction != NULL;
if (CanSendMailReport) if (CanSendMailReport)
formatedHeader += " Send report will only email the contents of the box below. Please, send it to help us (it could take few minutes to send the email, be patient)."; formatedHeader += " Send report will only email the contents of the box below. Please, send it to help us (it could take few minutes to send the email, be patient).";