khanat-opennel-code/code/nel/src/misc/report.cpp

219 lines
5.6 KiB
C++
Raw Normal View History

// 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>
#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"
#include "nel/misc/system_utils.h"
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-03-06 18:07:25 +00:00
namespace NLMISC
{
2015-03-06 18:07:25 +00:00
void setReportPostUrl(const char *postUrl)
{
2015-03-06 18:07:25 +00:00
#if NL_DEBUG_REPORT
if (INelContext::isContextInitialised())
nldebug("Set report post url to '%s'", postUrl);
#endif
#ifdef NL_OS_WINDOWS
2015-03-06 18:07:25 +00:00
SetEnvironmentVariableA(NL_REPORT_POST_URL_ENVVAR, postUrl);
#else
2015-03-06 18:07:25 +00:00
setenv(NL_REPORT_POST_URL_ENVVAR, postUrl, 1);
#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;
#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
{
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());
#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
}
}
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
{
std::string params;
2015-02-22 16:38:06 +00:00
2015-03-06 18:07:25 +00:00
if (!reportPath.empty())
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())
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())
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())
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)
params += NLMISC::toString(" -host \"%s\"", reportPostUrl);
2015-02-22 16:38:06 +00:00
2015-03-06 18:07:25 +00:00
if (synchronous)
params += " -dev";
2015-02-22 16:38:06 +00:00
2015-03-06 18:07:25 +00:00
if (sendReport)
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
{
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
{
NLMISC::launchProgram(NL_CRASH_REPORT_TOOL, params,
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
}
}
}
} // NLMISC