2012-05-29 13:31:11 +00:00
|
|
|
// NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
|
|
|
|
// Copyright (C) 2010 Winch Gate Property Limited
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as
|
|
|
|
// published by the Free Software Foundation, either version 3 of the
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program 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 Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
#include "stdmisc.h"
|
|
|
|
|
2015-03-06 18:06:11 +00:00
|
|
|
#include <stdlib.h>
|
2015-02-24 11:47:32 +00:00
|
|
|
#include <sstream>
|
2015-02-23 19:01:20 +00:00
|
|
|
|
2012-05-29 13:31:11 +00:00
|
|
|
#include "nel/misc/common.h"
|
|
|
|
#include "nel/misc/ucstring.h"
|
|
|
|
|
|
|
|
#include "nel/misc/report.h"
|
|
|
|
#include "nel/misc/path.h"
|
2015-03-06 18:07:25 +00:00
|
|
|
#include "nel/misc/file.h"
|
2015-03-06 20:07:12 +00:00
|
|
|
#include "nel/misc/system_utils.h"
|
2012-05-29 13:31:11 +00:00
|
|
|
|
2015-02-22 16:15:41 +00:00
|
|
|
#ifdef DEBUG_NEW
|
|
|
|
#define new DEBUG_NEW
|
|
|
|
#endif
|
|
|
|
|
2015-03-06 18:07:25 +00:00
|
|
|
#define NL_REPORT_POST_URL_ENVVAR "NL_REPORT_POST_URL"
|
|
|
|
#ifdef NL_OS_WINDOWS
|
|
|
|
#define NL_CRASH_REPORT_TOOL "crash_report.exe"
|
|
|
|
#else
|
|
|
|
#define NL_CRASH_REPORT_TOOL "crash_report"
|
|
|
|
#endif
|
|
|
|
#define NL_DEBUG_REPORT 0
|
|
|
|
// Set to 1 if you want command line report tool
|
|
|
|
#define NL_REPORT_CONSOLE 0
|
|
|
|
// Set to 1 if you want command line report tool even when the debugger is present
|
|
|
|
#define NL_REPORT_CONSOLE_DEBUGGER 1
|
2015-02-23 00:36:50 +00:00
|
|
|
|
2015-03-06 18:07:25 +00:00
|
|
|
namespace NLMISC
|
2015-02-23 19:01:20 +00:00
|
|
|
{
|
2015-02-23 00:36:50 +00:00
|
|
|
|
2015-03-06 18:07:25 +00:00
|
|
|
void setReportPostUrl(const char *postUrl)
|
2015-02-23 00:36:50 +00:00
|
|
|
{
|
2015-03-06 18:07:25 +00:00
|
|
|
#if NL_DEBUG_REPORT
|
|
|
|
if (INelContext::isContextInitialised())
|
|
|
|
nldebug("Set report post url to '%s'", postUrl);
|
|
|
|
#endif
|
2015-02-23 00:36:50 +00:00
|
|
|
#ifdef NL_OS_WINDOWS
|
2015-03-06 18:07:25 +00:00
|
|
|
SetEnvironmentVariableA(NL_REPORT_POST_URL_ENVVAR, postUrl);
|
2015-02-23 00:36:50 +00:00
|
|
|
#else
|
2015-03-06 18:07:25 +00:00
|
|
|
setenv(NL_REPORT_POST_URL_ENVVAR, postUrl, 1);
|
2015-02-23 00:36:50 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-03-06 18:07:25 +00:00
|
|
|
inline const char *getReportPostURL()
|
2015-02-22 16:20:24 +00:00
|
|
|
{
|
2015-03-06 18:07:25 +00:00
|
|
|
#ifdef NL_OS_WINDOWS
|
|
|
|
static char buf[512];
|
|
|
|
buf[0] = '\0';
|
|
|
|
int res = GetEnvironmentVariableA(NL_REPORT_POST_URL_ENVVAR, buf, sizeof(buf));
|
|
|
|
if (res <= 0 || res > 511) return NULL;
|
|
|
|
if (buf[0] == '\0') return NULL;
|
|
|
|
return buf;
|
2015-02-22 16:20:24 +00:00
|
|
|
#else
|
2015-03-06 18:07:25 +00:00
|
|
|
char *res = getenv(NL_REPORT_POST_URL_ENVVAR);
|
|
|
|
if (res == NULL || res[0] == '\0') return NULL;
|
|
|
|
return res;
|
2015-02-20 01:03:33 +00:00
|
|
|
#endif
|
2015-02-22 16:38:06 +00:00
|
|
|
}
|
|
|
|
|
2015-03-06 18:07:25 +00:00
|
|
|
TReportResult report(const std::string &title, const std::string &subject, const std::string &body, const std::string &attachment, bool synchronous, bool sendReport, TReportResult defaultResult)
|
2015-02-22 16:38:06 +00:00
|
|
|
{
|
2015-03-06 18:07:25 +00:00
|
|
|
std::string reportPath;
|
|
|
|
if (!body.empty())
|
2015-02-22 16:38:06 +00:00
|
|
|
{
|
2015-12-28 17:15:28 +00:00
|
|
|
std::string reportFile = getLogDirectory() + NLMISC::toString("nel_report_%u.log", (uint)time(NULL));
|
|
|
|
reportPath = CFile::findNewFile(reportFile);
|
2015-03-06 18:07:25 +00:00
|
|
|
std::ofstream f;
|
|
|
|
f.open(reportPath.c_str());
|
|
|
|
if (!f.good())
|
2015-02-22 16:38:06 +00:00
|
|
|
{
|
2015-03-06 18:07:25 +00:00
|
|
|
#if NL_DEBUG_REPORT
|
|
|
|
if (INelContext::isContextInitialised())
|
|
|
|
nldebug("Failed to write report log to '%s'", reportPath.c_str());
|
2012-05-29 13:31:11 +00:00
|
|
|
#endif
|
2015-03-06 18:07:25 +00:00
|
|
|
reportPath.clear();
|
2015-02-22 16:38:06 +00:00
|
|
|
}
|
2015-03-06 18:07:25 +00:00
|
|
|
else
|
2015-02-22 16:38:06 +00:00
|
|
|
{
|
2015-03-06 18:07:25 +00:00
|
|
|
f << body;
|
|
|
|
f.close();
|
2015-02-22 16:38:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-06 20:07:12 +00:00
|
|
|
if (((INelContext::isContextInitialised()
|
|
|
|
&& INelContext::getInstance().isWindowedApplication())
|
|
|
|
|| CSystemUtils::detectWindowedApplication())
|
2015-03-06 18:07:25 +00:00
|
|
|
&& CFile::isExists(NL_CRASH_REPORT_TOOL))
|
2015-02-22 16:38:06 +00:00
|
|
|
{
|
2015-12-28 17:15:28 +00:00
|
|
|
std::string params;
|
2015-02-22 16:38:06 +00:00
|
|
|
|
2015-03-06 18:07:25 +00:00
|
|
|
if (!reportPath.empty())
|
2015-12-28 17:15:28 +00:00
|
|
|
params += NLMISC::toString(" -log \"%s\"", reportPath.c_str());
|
2015-02-22 16:38:06 +00:00
|
|
|
|
2015-03-06 18:07:25 +00:00
|
|
|
if (!subject.empty())
|
2015-12-28 17:15:28 +00:00
|
|
|
params += NLMISC::toString(" -attachment \"%s\"", attachment.c_str());
|
2015-02-22 16:38:06 +00:00
|
|
|
|
2015-03-06 18:07:25 +00:00
|
|
|
if (!title.empty())
|
2015-12-28 17:15:28 +00:00
|
|
|
params += NLMISC::toString(" -title \"%s\"", title.c_str());
|
2015-02-22 16:38:06 +00:00
|
|
|
|
2015-03-06 18:07:25 +00:00
|
|
|
if (!subject.empty())
|
2015-12-28 17:15:28 +00:00
|
|
|
params += NLMISC::toString(" -subject \"%s\"", subject.c_str());
|
2015-02-22 16:38:06 +00:00
|
|
|
|
2015-03-06 18:07:25 +00:00
|
|
|
const char *reportPostUrl = getReportPostURL();
|
|
|
|
if (reportPostUrl)
|
2015-12-28 17:15:28 +00:00
|
|
|
params += NLMISC::toString(" -host \"%s\"", reportPostUrl);
|
2015-02-22 16:38:06 +00:00
|
|
|
|
2015-03-06 18:07:25 +00:00
|
|
|
if (synchronous)
|
2015-12-28 17:15:28 +00:00
|
|
|
params += " -dev";
|
2015-02-22 16:38:06 +00:00
|
|
|
|
2015-03-06 18:07:25 +00:00
|
|
|
if (sendReport)
|
2015-12-28 17:15:28 +00:00
|
|
|
params += " -sendreport";
|
2015-02-22 16:38:06 +00:00
|
|
|
|
2015-03-06 18:07:25 +00:00
|
|
|
if (synchronous)
|
2015-02-22 16:38:06 +00:00
|
|
|
{
|
2016-01-02 18:15:21 +00:00
|
|
|
TReportResult result = (TReportResult)NLMISC::launchProgramAndWaitForResult(NL_CRASH_REPORT_TOOL, params);
|
|
|
|
|
2015-03-06 18:07:25 +00:00
|
|
|
if (result != ReportAlwaysIgnore
|
|
|
|
&& result != ReportIgnore
|
|
|
|
&& result != ReportAbort
|
|
|
|
&& result != ReportBreak)
|
|
|
|
{
|
|
|
|
#if NL_DEBUG_REPORT
|
|
|
|
if (INelContext::isContextInitialised())
|
|
|
|
nldebug("Return default result, invalid return code %i", (int)result);
|
|
|
|
#endif
|
|
|
|
return defaultResult;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-12-28 17:15:28 +00:00
|
|
|
NLMISC::launchProgram(NL_CRASH_REPORT_TOOL, params,
|
2015-03-06 20:07:12 +00:00
|
|
|
NL_DEBUG_REPORT ? INelContext::isContextInitialised() : false); // Only log if required, avoid infinite loop
|
2015-03-06 18:07:25 +00:00
|
|
|
return defaultResult;
|
2015-02-22 16:38:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-03-06 18:07:25 +00:00
|
|
|
#if NL_DEBUG_REPORT
|
|
|
|
if (INelContext::isContextInitialised() && !CFile::isExists(NL_CRASH_REPORT_TOOL))
|
|
|
|
nldebug("Crash report tool '%s' does not exist", NL_CRASH_REPORT_TOOL);
|
|
|
|
#endif
|
|
|
|
#if defined(NL_OS_WINDOWS) && !FINAL_VERSION && !NL_REPORT_CONSOLE_DEBUGGER
|
|
|
|
if (IsDebuggerPresent())
|
|
|
|
{
|
|
|
|
return defaultResult;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
if (synchronous)
|
|
|
|
{
|
|
|
|
#if NL_REPORT_CONSOLE
|
|
|
|
// An interactive console based report
|
|
|
|
printf("\n");
|
|
|
|
if (!title.empty())
|
|
|
|
printf("%s\n", title.c_str());
|
|
|
|
else
|
|
|
|
printf("NeL report\n");
|
|
|
|
printf("\n");
|
|
|
|
if (!subject.empty())
|
|
|
|
printf("\tsubject: '%s'\n", subject.c_str());
|
|
|
|
if (!body.empty())
|
|
|
|
printf("\tbody: '%s'\n", reportPath.c_str());
|
|
|
|
if (!attachment.empty())
|
|
|
|
printf("\tattachment: '%s'\n", attachment.c_str());
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
printf("\n");
|
|
|
|
printf("Always Ignore (S), Ignore (I), Abort (A), Break (B)?\n"); // S for Surpress
|
|
|
|
printf("> ");
|
|
|
|
int c = getchar();
|
|
|
|
getchar();
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case 'S':
|
|
|
|
case 's':
|
|
|
|
return ReportAlwaysIgnore;
|
|
|
|
case 'I':
|
|
|
|
case 'i':
|
|
|
|
return ReportIgnore;
|
|
|
|
case 'A':
|
|
|
|
case 'a':
|
|
|
|
return ReportAbort;
|
|
|
|
case 'B':
|
|
|
|
case 'b':
|
|
|
|
return ReportBreak;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
return defaultResult;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
2015-02-22 16:38:06 +00:00
|
|
|
{
|
2015-03-06 18:07:25 +00:00
|
|
|
return defaultResult;
|
2015-02-22 16:38:06 +00:00
|
|
|
}
|
|
|
|
}
|
2012-05-29 13:31:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // NLMISC
|