Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move: logger into new file #265

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ BITCOIN_CORE_H = \
mastercore_rpc.h \
mastercore_sp.h \
mastercore_errors.h \
mastercore_log.h \
mastercore_version.h \
crypter.h \
db.h \
Expand Down Expand Up @@ -140,6 +141,7 @@ libbitcoin_common_a_SOURCES = \
mastercore_tx.cpp \
mastercore_rpc.cpp \
mastercore_dex.cpp \
mastercore_log.cpp \
mastercore_sp.cpp \
hash.cpp \
key.cpp \
Expand Down
124 changes: 2 additions & 122 deletions src/mastercore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ using namespace mastercore;
#include "mastercore_errors.h"
#include "mastercore_version.h"

#include "mastercore_log.h"

// part of 'breakout' feature
static const int nBlockTop = 0;
// static const int nBlockTop = 271000;
Expand All @@ -98,29 +100,6 @@ static uint64_t exodus_balance;

static boost::filesystem::path MPPersistencePath;

const int msc_debug_parser_data = 0;
const int msc_debug_parser= 0;
const int msc_debug_verbose=0;
const int msc_debug_verbose2=0;
const int msc_debug_verbose3=0;
const int msc_debug_vin = 0;
const int msc_debug_script= 0;
const int msc_debug_dex = 1;
const int msc_debug_send = 1;
const int msc_debug_tokens= 0;
const int msc_debug_spec = 1;
const int msc_debug_exo = 0;
const int msc_debug_tally = 1;
const int msc_debug_sp = 1;
const int msc_debug_sto = 1;
const int msc_debug_txdb = 0;
const int msc_debug_tradedb = 1;
const int msc_debug_persistence = 0;
// disable all flags for metadex for the immediate tag, then turn back on 0 & 2 at least
int msc_debug_metadex1= 0;
int msc_debug_metadex2= 0;
int msc_debug_metadex3= 0; // enable this to see the orderbook before & after each TX

const static int disable_Divs = 0;
const static int disableLevelDB = 0;

Expand Down Expand Up @@ -173,73 +152,6 @@ uint32_t mastercore::GetLatestBlockTime()
return (int)(Params().GenesisBlock().nTime); // Genesis block's time of current network
}

//--- CUT HERE --- mostly copied from util.h & util.cpp
// LogPrintf() has been broken a couple of times now
// by well-meaning people adding mutexes in the most straightforward way.
// It breaks because it may be called by global destructors during shutdown.
// Since the order of destruction of static/global objects is undefined,
// defining a mutex as a global object doesn't work (the mutex gets
// destroyed, and then some later destructor calls OutputDebugStringF,
// maybe indirectly, and you get a core dump at shutdown trying to lock
// the mutex).
static boost::once_flag mp_debugPrintInitFlag = BOOST_ONCE_INIT;
// We use boost::call_once() to make sure these are initialized in
// in a thread-safe manner the first time it is called:
static FILE* fileout = NULL;
static boost::mutex* mutexDebugLog = NULL;

static void mp_DebugPrintInit()
{
assert(fileout == NULL);
assert(mutexDebugLog == NULL);

boost::filesystem::path pathDebug = GetDataDir() / LOG_FILENAME ;
fileout = fopen(pathDebug.string().c_str(), "a");
if (fileout) setbuf(fileout, NULL); // unbuffered

mutexDebugLog = new boost::mutex();
}

int mp_LogPrintStr(const std::string &str)
{
int ret = 0; // Returns total number of characters written
if (fPrintToConsole)
{
// print to console
ret = fwrite(str.data(), 1, str.size(), stdout);
}
else if (fPrintToDebugLog)
{
static bool fStartedNewLine = true;
boost::call_once(&mp_DebugPrintInit, mp_debugPrintInitFlag);

if (fileout == NULL)
return ret;

boost::mutex::scoped_lock scoped_lock(*mutexDebugLog);

// reopen the log file, if requested
if (fReopenDebugLog) {
fReopenDebugLog = false;
boost::filesystem::path pathDebug = GetDataDir() / LOG_FILENAME ;
if (freopen(pathDebug.string().c_str(),"a",fileout) != NULL)
setbuf(fileout, NULL); // unbuffered
}

// Debug print useful for profiling
if (fLogTimestamps && fStartedNewLine)
ret += fprintf(fileout, "%s ", DateTimeStrFormat("%Y-%m-%d %H:%M:%S", GetTime()).c_str());
if (!str.empty() && str[str.size()-1] == '\n')
fStartedNewLine = true;
else
fStartedNewLine = false;

ret = fwrite(str.data(), 1, str.size(), fileout);
}

return ret;
}

// indicate whether persistence is enabled at this point, or not
// used to write/read files, for breakout mode, debugging, etc.
static bool readPersistence()
Expand All @@ -261,38 +173,6 @@ static bool writePersistence(int block_now)
return true;
}

// copied from ShrinkDebugFile, util.cpp
static void shrinkDebugFile()
{
// Scroll log if it's getting too big
const int buffer_size = 8000000; // 8MBytes
boost::filesystem::path pathLog = GetDataDir() / LOG_FILENAME;
FILE* file = fopen(pathLog.string().c_str(), "r");

if (file && boost::filesystem::file_size(pathLog) > 50000000) // 50MBytes
{
// Restart the file with some of the end
char *pch = new char[buffer_size];
if (NULL != pch)
{
fseek(file, -buffer_size, SEEK_END);
int nBytes = fread(pch, 1, buffer_size, file);
fclose(file); file = NULL;

file = fopen(pathLog.string().c_str(), "w");
if (file)
{
fwrite(pch, 1, nBytes, file);
fclose(file); file = NULL;
}
delete [] pch;
}
}
else
{
if (NULL != file) fclose(file);
}
}

string mastercore::strMPProperty(unsigned int i)
{
Expand Down
53 changes: 1 addition & 52 deletions src/mastercore.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,10 @@
#ifndef _MASTERCOIN
#define _MASTERCOIN 1

#include "netbase.h"
#include "protocol.h"

#include "tinyformat.h"
#include "mastercore_log.h"

#define DISABLE_METADEX

#define LOG_FILENAME "mastercore.log"
#define INFO_FILENAME "mastercore_crowdsales.log"
#define OWNERS_FILENAME "mastercore_owners.log"

int const MAX_STATE_HISTORY = 50;

#define TEST_ECO_PROPERTY_1 (0x80000003UL)
Expand Down Expand Up @@ -134,50 +127,6 @@ enum FILETYPES {
using boost::multiprecision::cpp_dec_float_100;
typedef cpp_dec_float_100 XDOUBLE;

int mp_LogPrintStr(const std::string &str);

/* When we switch to C++11, this can be switched to variadic templates instead
* of this macro-based construction (see tinyformat.h).
*/
#define MP_MAKE_ERROR_AND_LOG_FUNC(n) \
/* Print to debug.log if -debug=category switch is given OR category is NULL. */ \
template<TINYFORMAT_ARGTYPES(n)> \
static inline int mp_category_log(const char* category, const char* format, TINYFORMAT_VARARGS(n)) \
{ \
if(!LogAcceptCategory(category)) return 0; \
return mp_LogPrintStr(tfm::format(format, TINYFORMAT_PASSARGS(n))); \
} \
template<TINYFORMAT_ARGTYPES(n)> \
static inline int mp_log(TINYFORMAT_VARARGS(n)) \
{ \
return mp_LogPrintStr(tfm::format("%s", TINYFORMAT_PASSARGS(n))); \
} \
template<TINYFORMAT_ARGTYPES(n)> \
static inline int mp_log(const char* format, TINYFORMAT_VARARGS(n)) \
{ \
return mp_LogPrintStr(tfm::format(format, TINYFORMAT_PASSARGS(n))); \
} \
template<TINYFORMAT_ARGTYPES(n)> \
static inline int file_log(const char* format, TINYFORMAT_VARARGS(n)) \
{ \
return mp_LogPrintStr(tfm::format(format, TINYFORMAT_PASSARGS(n))); \
} \
template<TINYFORMAT_ARGTYPES(n)> \
static inline int file_log(TINYFORMAT_VARARGS(n)) \
{ \
return mp_LogPrintStr(tfm::format("%s", TINYFORMAT_PASSARGS(n))); \
} \
/* Log error and return false */ \
template<TINYFORMAT_ARGTYPES(n)> \
static inline bool mp_error(const char* format, TINYFORMAT_VARARGS(n)) \
{ \
mp_LogPrintStr("ERROR: " + tfm::format(format, TINYFORMAT_PASSARGS(n)) + "\n"); \
return false; \
}

TINYFORMAT_FOREACH_ARGNUM(MP_MAKE_ERROR_AND_LOG_FUNC)
//--- CUT HERE ---

// forward declarations
std::string FormatPriceMP(double n);
std::string FormatDivisibleMP(int64_t n, bool fSign = false);
Expand Down
2 changes: 0 additions & 2 deletions src/mastercore_dex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ using namespace mastercore;
#include "mastercore_dex.h"
#include "mastercore_tx.h"

extern int msc_debug_metadex1, msc_debug_metadex2, msc_debug_metadex3;

md_PropertiesMap mastercore::metadex;

md_PricesMap* mastercore::get_Prices(unsigned int prop)
Expand Down
143 changes: 143 additions & 0 deletions src/mastercore_log.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
#include "mastercore_log.h"

#include <assert.h>
#include <stdio.h>
#include <string>

#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/once.hpp>

/**
* util.h/.cpp:
*
* std::string DateTimeStrFormat(const char* pszFormat, int64_t nTime);
* const boost::filesystem::path &GetDataDir(bool fNetSpecific);
* int64_t GetTime();
*
* bool fPrintToConsole = false;
* bool fLogTimestamps = false;
*/
#include "util.h"

const char* LOG_FILENAME = "mastercore.log";
const char* INFO_FILENAME = "mastercore_crowdsales.log";
const char* OWNERS_FILENAME = "mastercore_owners.log";

const int msc_debug_parser_data = 0;
const int msc_debug_parser = 0;
const int msc_debug_verbose = 0;
const int msc_debug_verbose2 = 0;
const int msc_debug_verbose3 = 0;
const int msc_debug_vin = 0;
const int msc_debug_script = 0;
const int msc_debug_dex = 1;
const int msc_debug_send = 1;
const int msc_debug_tokens = 0;
const int msc_debug_spec = 1;
const int msc_debug_exo = 0;
const int msc_debug_tally = 1;
const int msc_debug_sp = 1;
const int msc_debug_sto = 1;
const int msc_debug_txdb = 0;
const int msc_debug_tradedb = 1;
const int msc_debug_persistence = 0;

// disable all flags for metadex for the immediate tag, then turn back on 0 & 2 at least
const int msc_debug_metadex1 = 0;
const int msc_debug_metadex2 = 0;
// enable this to see the orderbook before and after each transaction
const int msc_debug_metadex3 = 0;

// LogPrintf() has been broken a couple of times now
// by well-meaning people adding mutexes in the most straightforward way.
// It breaks because it may be called by global destructors during shutdown.
// Since the order of destruction of static/global objects is undefined,
// defining a mutex as a global object doesn't work (the mutex gets
// destroyed, and then some later destructor calls OutputDebugStringF,
// maybe indirectly, and you get a core dump at shutdown trying to lock
// the mutex).
static boost::once_flag mp_debugPrintInitFlag = BOOST_ONCE_INIT;

// We use boost::call_once() to make sure these are initialized in
// in a thread-safe manner the first time it is called:
static FILE* fileout = NULL;
static boost::mutex* mutexDebugLog = NULL;

static void mp_DebugPrintInit() {
assert(fileout == NULL);
assert(mutexDebugLog == NULL);

boost::filesystem::path pathDebug = GetDataDir() / LOG_FILENAME;
fileout = fopen(pathDebug.c_str(), "a");
if (fileout) setbuf(fileout, NULL); // unbuffered

mutexDebugLog = new boost::mutex();
}

int mp_LogPrintStr(const std::string &str) {
int ret = 0; // Returns total number of characters written
if (fPrintToConsole) {
// print to console
ret = fwrite(str.data(), 1, str.size(), stdout);
} else if (fPrintToDebugLog) {
static bool fStartedNewLine = true;
boost::call_once(&mp_DebugPrintInit, mp_debugPrintInitFlag);

if (fileout == NULL)
return ret;

boost::mutex::scoped_lock scoped_lock(*mutexDebugLog);

// reopen the log file, if requested
if (fReopenDebugLog) {
fReopenDebugLog = false;
boost::filesystem::path pathDebug = GetDataDir() / LOG_FILENAME;
if (freopen(pathDebug.c_str(), "a", fileout) != NULL)
setbuf(fileout, NULL); // unbuffered
}

// Debug print useful for profiling
if (fLogTimestamps && fStartedNewLine)
ret += fprintf(fileout, "%s ", DateTimeStrFormat("%Y-%m-%d %H:%M:%S", GetTime()).c_str());
if (!str.empty() && str[str.size() - 1] == '\n')
fStartedNewLine = true;
else
fStartedNewLine = false;

ret = fwrite(str.data(), 1, str.size(), fileout);
}

return ret;
}

void shrinkDebugFile() {
// Scroll log if it's getting too big
const int buffer_size = 8000000; // 8 MByte
boost::filesystem::path pathLog = GetDataDir() / LOG_FILENAME;
FILE* file = fopen(pathLog.c_str(), "r");

if (file && boost::filesystem::file_size(pathLog) > 50000000) // 50 MByte
{
// Restart the file with some of the end
char *pch = new char[buffer_size];
if (NULL != pch) {
fseek(file, -buffer_size, SEEK_END);
int nBytes = fread(pch, 1, buffer_size, file);
fclose(file);
file = NULL;

file = fopen(pathLog.c_str(), "w");
if (file) {
fwrite(pch, 1, nBytes, file);
fclose(file);
file = NULL;
}
delete [] pch;
}
} else {
if (NULL != file) fclose(file);
}
}

Loading