Skip to content
/ i2pd Public
forked from PurpleI2P/i2pd

Commit

Permalink
configurable throw function
Browse files Browse the repository at this point in the history
  • Loading branch information
orignal committed May 5, 2020
1 parent dbe1e3f commit d7d70b7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
6 changes: 6 additions & 0 deletions Win32/DaemonWin32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#ifdef _WIN32
#include "Win32/Win32Service.h"
#ifdef WIN32_APP
#include <windows.h>
#include "Win32/Win32App.h"
#endif

Expand All @@ -23,6 +24,11 @@ namespace util
setlocale(LC_ALL, "Russian");
setlocale(LC_TIME, "C");

i2p::log::SetThrowFunction ([](const std::string& s)
{
MessageBox(0, TEXT(s.c_str ()), TEXT("i2pd"), MB_ICONERROR | MB_TASKMODAL | MB_OK );
});

if (!Daemon_Singleton::init(argc, argv))
return false;

Expand Down
6 changes: 6 additions & 0 deletions libi2pd/Log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,5 +236,11 @@ namespace log {
Log & Logger() {
return logger;
}

static ThrowFunction g_ThrowFunction;
ThrowFunction GetThrowFunction () { return g_ThrowFunction; }
void SetThrowFunction (ThrowFunction f) { g_ThrowFunction = f; }

} // log
} // i2p

19 changes: 8 additions & 11 deletions libi2pd/Log.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,13 @@
#include <chrono>
#include <memory>
#include <thread>
#include <functional>
#include "Queue.h"

#ifndef _WIN32
#include <syslog.h>
#endif

#ifdef WIN32_APP
#include <windows.h> // TODO: move away to win32app
#endif

enum LogLevel
{
eLogNone = 0,
Expand Down Expand Up @@ -155,6 +152,10 @@ namespace log {
};

Log & Logger();

typedef std::function<void (const std::string&)> ThrowFunction;
ThrowFunction GetThrowFunction ();
void SetThrowFunction (ThrowFunction f);
} // log
}

Expand Down Expand Up @@ -201,27 +202,23 @@ void LogPrint (LogLevel level, TArgs&&... args) noexcept
log.Append(msg);
}


/**
* @brief Throw fatal error message with the list of arguments
* @param args Array of message parts
*/
template<typename... TArgs>
void ThrowFatal (TArgs&&... args) noexcept
{
auto f = i2p::log::GetThrowFunction ();
if (!f) return;
// fold message to single string
std::stringstream ss("");
#if (__cplusplus >= 201703L) // C++ 17 or higher
(LogPrint (ss, std::forward<TArgs>(args)), ...);
#else
LogPrint (ss, std::forward<TArgs>(args)...);
#endif

#ifdef WIN32_APP
MessageBox(0, TEXT(ss.str ().c_str ()), TEXT("i2pd"), MB_ICONERROR | MB_TASKMODAL | MB_OK );
#else
std::cout << ss.str ();
#endif
f (ss.str ());
}

#endif // LOG_H__

0 comments on commit d7d70b7

Please sign in to comment.