Skip to content

Commit

Permalink
Merge pull request xbmc#6416 from Karlson2k/mhd_log_01
Browse files Browse the repository at this point in the history
Write messages from MHD to log file
  • Loading branch information
Karlson2k committed Mar 6, 2015
2 parents e233320 + 16d14a2 commit cbeb183
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions xbmc/network/WebServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#ifdef HAS_WEB_SERVER
#include <memory>
#include <algorithm>
#include <stdexcept>

#include "URL.h"
#include "Util.h"
Expand Down Expand Up @@ -1062,10 +1063,43 @@ void CWebServer::ContentReaderFreeCallback(void *cls)
#endif
}

// local helper
static void panicHandlerForMHD(void* unused, const char* file, unsigned int line, const char *reason)
{
CLog::Log(LOGSEVERE, "CWebServer: MHD serious error: reason \"%s\" in file \"%s\" at line %ui", reason ? reason : "",
file ? file : "", line);
throw std::runtime_error("MHD serious error"); // FIXME: better solution?
}

// local helper
static void logFromMHD(void* unused, const char* fmt, va_list ap)
{
if (fmt == NULL || fmt[0] == 0)
CLog::Log(LOGERROR, "CWebServer: MHD reported error with empty string");
else
{
std::string errDsc = StringUtils::FormatV(fmt, ap);
if (errDsc.empty())
CLog::Log(LOGERROR, "CWebServer: MHD reported error with unprintable string \"%s\"", fmt);
else
{
if (errDsc.at(errDsc.length() - 1) == '\n')
errDsc.erase(errDsc.length() - 1);

// Most common error is "aborted connection", so log it at LOGDEBUG level
CLog::Log(LOGDEBUG, "CWebServer [MHD]: %s", errDsc.c_str());
}
}
}

struct MHD_Daemon* CWebServer::StartMHD(unsigned int flags, int port)
{
unsigned int timeout = 60 * 60 * 24;

#if MHD_VERSION >= 0x00040500
MHD_set_panic_func(&panicHandlerForMHD, NULL);
#endif

return MHD_start_daemon(flags |
#if (MHD_VERSION >= 0x00040002) && (MHD_VERSION < 0x00090B01)
// use main thread for each connection, can only handle one request at a
Expand All @@ -1077,6 +1111,9 @@ struct MHD_Daemon* CWebServer::StartMHD(unsigned int flags, int port)
// otherwise on libmicrohttpd 0.4.4-1 it spins a busy loop
MHD_USE_THREAD_PER_CONNECTION
#endif
#if (MHD_VERSION >= 0x00040001)
| MHD_USE_DEBUG /* Print MHD error messages to log */
#endif
,
port,
NULL,
Expand All @@ -1090,6 +1127,9 @@ struct MHD_Daemon* CWebServer::StartMHD(unsigned int flags, int port)
MHD_OPTION_CONNECTION_LIMIT, 512,
MHD_OPTION_CONNECTION_TIMEOUT, timeout,
MHD_OPTION_URI_LOG_CALLBACK, &CWebServer::UriRequestLogger, this,
#if (MHD_VERSION >= 0x00040001)
MHD_OPTION_EXTERNAL_LOGGER, &logFromMHD, NULL,
#endif // MHD_VERSION >= 0x00040001
MHD_OPTION_END);
}

Expand Down

0 comments on commit cbeb183

Please sign in to comment.