Files
thehub/libs/server/util.h
T

175 lines
5.6 KiB
C++
Raw Permalink Normal View History

2017-11-09 19:34:51 +01:00
/*
* This file is part of the Flowee project
* Copyright (C) 2009-2010 Satoshi Nakamoto
* Copyright (C) 2009-2015 The Bitcoin Core developers
2018-01-15 15:26:12 +00:00
* Copyright (C) 2017 Tom Zander <tomz@freedommail.ch>
2017-11-09 19:34:51 +01:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2013-04-13 00:13:08 -05:00
2014-08-21 16:11:09 +02:00
/**
* Server/client environment: argument handling, config file parsing,
* logging, thread wrappers
*/
2018-01-16 10:47:52 +00:00
#ifndef FLOWEE_UTIL_H
#define FLOWEE_UTIL_H
2011-05-14 10:31:46 +02:00
2013-04-13 00:13:08 -05:00
#if defined(HAVE_CONFIG_H)
2018-01-16 10:47:52 +00:00
#include "config/flowee-config.h"
2011-05-15 16:50:28 +02:00
#endif
2013-04-13 00:13:08 -05:00
#include "allowed_args.h"
2013-04-13 00:13:08 -05:00
#include "compat.h"
2014-01-16 15:52:37 +01:00
#include "tinyformat.h"
2014-09-14 12:43:56 +02:00
#include "utiltime.h"
#include "Logger.h"
2013-04-13 00:13:08 -05:00
#include <exception>
2011-05-14 10:31:46 +02:00
#include <map>
#include <vector>
2018-01-15 15:26:12 +00:00
#include <functional>
#include <mutex>
2011-05-14 10:31:46 +02:00
#include <boost/filesystem/path.hpp>
#include <boost/signals2/signal.hpp>
2014-08-21 16:11:09 +02:00
#include <boost/thread/exceptions.hpp>
2013-03-07 14:25:21 -05:00
// For bitcoin-cli
static const char DEFAULT_RPCCONNECT[] = "127.0.0.1";
static const int DEFAULT_HTTP_CLIENT_TIMEOUT = 900;
static const bool DEFAULT_LOGIPS = false;
2011-05-14 10:31:46 +02:00
extern std::map<std::string, std::string> mapArgs;
extern std::map<std::string, std::vector<std::string> > mapMultiArgs;
/**
2019-08-24 13:20:37 +02:00
* old deprecated method to do translation of GUI output.
*/
inline std::string _(const char* psz)
{
2019-08-24 13:20:37 +02:00
return std::string(psz);
}
2011-05-14 10:31:46 +02:00
#define LogPrintf(...) Log::MessageLogger(BCH_MESSAGELOG_FILE, BCH_MESSAGELOG_LINE, BCH_MESSAGELOG_FUNC).infoCompat(nullptr, __VA_ARGS__)
2012-03-18 23:14:03 +01:00
/**
* When we switch to C++11, this can be switched to variadic templates instead
2014-01-16 15:52:37 +01:00
* of this macro-based construction (see tinyformat.h).
*/
2014-01-16 15:52:37 +01:00
#define MAKE_ERROR_AND_LOG_FUNC(n) \
2018-11-21 12:18:32 +01:00
/** Print to hub.log if -debug=category switch is given OR category is NULL. */ \
template<TINYFORMAT_ARGTYPES(n)> \
static inline void LogPrint(const char* category, const char* format, TINYFORMAT_VARARGS(n)) { \
Log::MessageLogger(nullptr, 0, nullptr).infoCompat(category, format, TINYFORMAT_PASSARGS(n)); \
} \
template<TINYFORMAT_ARGTYPES(n)> \
static inline bool error(const char* format, TINYFORMAT_VARARGS(n)) { \
2017-05-12 15:06:08 +02:00
Log::MessageLogger(nullptr, 0, nullptr).warning(format, TINYFORMAT_PASSARGS(n)); \
return false;\
2014-01-16 15:52:37 +01:00
}
TINYFORMAT_FOREACH_ARGNUM(MAKE_ERROR_AND_LOG_FUNC)
/**
* Zero-arg versions of logging and error, these are not covered by
2014-01-16 15:52:37 +01:00
* TINYFORMAT_FOREACH_ARGNUM
*/
static inline void LogPrint(const char* category, const char* format)
2014-01-16 15:52:37 +01:00
{
Log::MessageLogger(nullptr, 0, nullptr).infoCompat(category, format);
2014-01-16 15:52:37 +01:00
}
2014-01-16 15:52:37 +01:00
static inline bool error(const char* format)
{
Log::MessageLogger(nullptr, 0, nullptr).warning() << "ERROR:" << format;
2014-01-16 15:52:37 +01:00
return false;
}
2014-12-07 13:29:06 +01:00
void PrintExceptionContinue(const std::exception *pex, const char* pszThread);
2018-02-12 14:15:24 +01:00
void ParseParameters(int argc, const char*const argv[], const Settings::AllowedArgs& allowedArgs);
bool TryCreateDirectory(const boost::filesystem::path& p);
2018-01-16 20:15:22 +00:00
boost::filesystem::path GetDefaultDataDir();
const boost::filesystem::path &GetDataDir(bool fNetSpecific = true);
void ClearDatadirCache();
2018-03-07 22:13:10 +01:00
boost::filesystem::path GetConfigFile(const std::string &filename = "");
2012-04-22 14:35:22 +02:00
void ReadConfigFile(std::map<std::string, std::string>& mapSettingsRet, std::map<std::string, std::vector<std::string> >& mapMultiSettingsRet);
#ifdef WIN32
boost::filesystem::path GetSpecialFolderPath(int nFolder, bool fCreate = true);
#endif
2011-05-14 10:31:46 +02:00
inline bool IsSwitchChar(char c)
{
2011-10-07 11:02:21 -04:00
#ifdef WIN32
2011-05-14 10:31:46 +02:00
return c == '-' || c == '/';
#else
return c == '-';
#endif
}
2012-02-06 12:37:49 -05:00
/**
* Return string argument or default value
*
* @param strArg Argument to get (e.g. "-foo")
* @param default (e.g. "1")
* @return command-line argument or default value
*/
std::string GetArg(const std::string& strArg, const std::string& strDefault);
2011-05-14 10:31:46 +02:00
2012-02-06 12:37:49 -05:00
/**
* Return integer argument or default value
*
* @param strArg Argument to get (e.g. "-foo")
* @param default (e.g. 1)
* @return command-line argument (0 if invalid number) or default value
*/
2013-04-13 00:13:08 -05:00
int64_t GetArg(const std::string& strArg, int64_t nDefault);
2011-05-14 10:31:46 +02:00
2012-02-06 12:37:49 -05:00
/**
* Return boolean argument or default value
*
* @param strArg Argument to get (e.g. "-foo")
* @param default (true or false)
* @return command-line argument or default value
*/
bool GetBoolArg(const std::string& strArg, bool fDefault);
2011-05-14 10:31:46 +02:00
/**
* Set an argument if it doesn't already have a value
*
* @param strArg Argument to set (e.g. "-foo")
* @param strValue Value (e.g. "1")
* @return true if argument gets set, false if it already had a value
*/
bool SoftSetArg(const std::string& strArg, const std::string& strValue);
/**
* Set a boolean argument if it doesn't already have a value
*
* @param strArg Argument to set (e.g. "-foo")
* @param fValue Value (e.g. false)
* @return true if argument gets set, false if it already had a value
*/
bool SoftSetBoolArg(const std::string& strArg, bool fValue);
2011-05-14 10:31:46 +02:00
void SetThreadPriority(int nPriority);
void RenameThread(const char* name);
2011-05-14 10:31:46 +02:00
2019-11-13 11:46:09 +01:00
/**
* Looks for -regtest, -testnet and returns the appropriate BIP70 chain name.
* @return CBaseChainParams::MAX_NETWORK_TYPES if an invalid combination is given. CBaseChainParams::MAIN by default.
*/
std::string ChainNameFromCommandLine();
2017-05-15 14:39:52 +02:00
2019-08-24 13:20:37 +02:00
#endif