PRCYCoin  2.0.0.7rc1
P2P Digital Currency
logging.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2018 The Bitcoin developers
3 // Copyright (c) 2015-2020 The PIVX developers
4 // Distributed under the MIT software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #include "chainparamsbase.h"
8 #include "logging.h"
9 #include "utiltime.h"
10 
11 
12 const char * const DEFAULT_DEBUGLOGFILE = "debug.log";
13 namespace fs = fs;
14 
29 
30 bool fLogIPs = DEFAULT_LOGIPS;
31 
32 
33 static int FileWriteStr(const std::string &str, FILE *fp)
34 {
35  return fwrite(str.data(), 1, str.size(), fp);
36 }
37 
39 {
40  std::lock_guard<std::mutex> scoped_lock(m_file_mutex);
41 
42  assert(m_fileout == nullptr);
43  assert(!m_file_path.empty());
44 
46  if (!m_fileout) return false;
47 
48  setbuf(m_fileout, nullptr); // unbuffered
49  // dump buffered messages from before we opened the log
50  while (!m_msgs_before_open.empty()) {
51  FileWriteStr(m_msgs_before_open.front(), m_fileout);
52  m_msgs_before_open.pop_front();
53  }
54 
55  return true;
56 }
57 
59 {
60  m_categories |= flag;
61 }
62 
63 bool BCLog::Logger::EnableCategory(const std::string& str)
64 {
65  BCLog::LogFlags flag;
66  if (!GetLogCategory(flag, str)) return false;
67  EnableCategory(flag);
68  return true;
69 }
70 
72 {
73  m_categories &= ~flag;
74 }
75 
76 bool BCLog::Logger::DisableCategory(const std::string& str)
77 {
78  BCLog::LogFlags flag;
79  if (!GetLogCategory(flag, str)) return false;
80  DisableCategory(flag);
81  return true;
82 }
83 
85 {
86  return (m_categories.load(std::memory_order_relaxed) & category) != 0;
87 }
88 
90 {
91  return m_categories == BCLog::NONE;
92 }
93 
95 {
97  std::string category;
98 };
99 
101  {BCLog::NONE, "0"},
102  {BCLog::NET, "net"},
103  {BCLog::TOR, "tor"},
104  {BCLog::MEMPOOL, "mempool"},
105  {BCLog::HTTP, "http"},
106  {BCLog::BENCH, "bench"},
107  {BCLog::ZMQ, "zmq"},
108  {BCLog::DB, "db"},
109  {BCLog::RPC, "rpc"},
110  {BCLog::ESTIMATEFEE, "estimatefee"},
111  {BCLog::ADDRMAN, "addrman"},
112  {BCLog::SELECTCOINS, "selectcoins"},
113  {BCLog::REINDEX, "reindex"},
114  {BCLog::CMPCTBLOCK, "cmpctblock"},
115  {BCLog::RAND, "rand"},
116  {BCLog::PRUNE, "prune"},
117  {BCLog::PROXY, "proxy"},
118  {BCLog::MEMPOOLREJ, "mempoolrej"},
119  {BCLog::LIBEVENT, "libevent"},
120  {BCLog::COINDB, "coindb"},
121  {BCLog::QT, "qt"},
122  {BCLog::LEVELDB, "leveldb"},
123  {BCLog::STAKING, "staking"},
124  {BCLog::MASTERNODE, "masternode"},
125  {BCLog::MNBUDGET, "mnbudget"},
126  {BCLog::MNPING, "mnping"},
127  {BCLog::POA, "poa"},
128  {BCLog::SUPPLY, "supply"},
129  {BCLog::DELETETX, "deletetx"},
130  {BCLog::ALL, "1"},
131  {BCLog::ALL, "all"},
132 };
133 
134 bool GetLogCategory(BCLog::LogFlags& flag, const std::string& str)
135 {
136  if (str == "") {
137  flag = BCLog::ALL;
138  return true;
139  }
140  for (const CLogCategoryDesc& category_desc : LogCategories) {
141  if (category_desc.category == str) {
142  flag = category_desc.flag;
143  return true;
144  }
145  }
146  return false;
147 }
148 
149 std::string ListLogCategories()
150 {
151  std::string ret;
152  int outcount = 0;
153  for (const CLogCategoryDesc& category_desc : LogCategories) {
154  // Omit the special cases.
155  if (category_desc.flag != BCLog::NONE && category_desc.flag != BCLog::ALL) {
156  if (outcount != 0) ret += ", ";
157  ret += category_desc.category;
158  outcount++;
159  }
160  }
161  return ret;
162 }
163 
164 std::vector<CLogCategoryActive> ListActiveLogCategories()
165 {
166  std::vector<CLogCategoryActive> ret;
167  for (const CLogCategoryDesc& category_desc : LogCategories) {
168  // Omit the special cases.
169  if (category_desc.flag != BCLog::NONE && category_desc.flag != BCLog::ALL) {
170  CLogCategoryActive catActive;
171  catActive.category = category_desc.category;
172  catActive.active = LogAcceptCategory(category_desc.flag);
173  ret.push_back(catActive);
174  }
175  }
176  return ret;
177 }
178 
179 std::string BCLog::Logger::LogTimestampStr(const std::string &str)
180 {
181  std::string strStamped;
182 
183  if (!m_log_timestamps)
184  return str;
185 
186  if (m_started_new_line)
187  strStamped = DateTimeStrFormat("%Y-%m-%d %H:%M:%S", GetTime()) + ' ' + str;
188  else
189  strStamped = str;
190 
191  if (!str.empty() && str[str.size()-1] == '\n')
192  m_started_new_line = true;
193  else
194  m_started_new_line = false;
195 
196  return strStamped;
197 }
198 
199 int BCLog::Logger::LogPrintStr(const std::string &str)
200 {
201  int ret = 0; // Returns total number of characters written
202  if (m_print_to_console) {
203  // print to console
204  ret = fwrite(str.data(), 1, str.size(), stdout);
205  fflush(stdout);
206  } else if (m_print_to_file) {
207  std::lock_guard<std::mutex> scoped_lock(m_file_mutex);
208 
209  std::string strTimestamped = LogTimestampStr(str);
210 
211  // buffer if we haven't opened the log yet
212  if (m_fileout == NULL) {
213  ret = strTimestamped.length();
214  m_msgs_before_open.push_back(strTimestamped);
215 
216  } else {
217  // reopen the log file, if requested
218  if (m_reopen_file) {
219  m_reopen_file = false;
220  if (fsbridge::freopen(m_file_path,"a",m_fileout) != NULL)
221  setbuf(m_fileout, NULL); // unbuffered
222  }
223 
224  ret = FileWriteStr(strTimestamped, m_fileout);
225  }
226  }
227 
228  return ret;
229 }
230 
232 {
233  // Amount of debug.log to save at end when shrinking (must fit in memory)
234  constexpr size_t RECENT_DEBUG_HISTORY_SIZE = 10 * 1000000;
235 
236  assert(!m_file_path.empty());
237 
238  // Scroll debug.log if it's getting too big
239  FILE* file = fsbridge::fopen(m_file_path, "r");
240  if (file && fs::file_size(m_file_path) > RECENT_DEBUG_HISTORY_SIZE) {
241  // Restart the file with some of the end
242  std::vector<char> vch(200000, 0);
243  fseek(file, -((long)vch.size()), SEEK_END);
244  int nBytes = fread(vch.data(), 1, vch.size(), file);
245  fclose(file);
246 
247  file = fsbridge::fopen(m_file_path, "w");
248  if (file) {
249  fwrite(vch.data(), 1, nBytes, file);
250  fclose(file);
251  }
252  } else if (file != NULL)
253  fclose(file);
254 }
BCLog::LogFlags
LogFlags
Definition: logging.h:38
fLogIPs
bool fLogIPs
Definition: logging.cpp:30
utiltime.h
CLogCategoryDesc
Definition: logging.cpp:94
BCLog::ZMQ
@ ZMQ
Definition: logging.h:45
GetTime
int64_t GetTime()
For unit testing.
Definition: utiltime.cpp:19
ListActiveLogCategories
std::vector< CLogCategoryActive > ListActiveLogCategories()
Returns a vector of the active log categories.
Definition: logging.cpp:164
BCLog::HTTP
@ HTTP
Definition: logging.h:43
BCLog::Logger::WillLogCategory
bool WillLogCategory(LogFlags category) const
Definition: logging.cpp:84
fsbridge::fopen
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:13
BCLog::Logger::OpenDebugLog
bool OpenDebugLog()
Definition: logging.cpp:38
GetLogCategory
bool GetLogCategory(BCLog::LogFlags &flag, const std::string &str)
Return true if str parses as a log category and set the flag.
Definition: logging.cpp:134
chainparamsbase.h
g_logger
BCLog::Logger *const g_logger
NOTE: the logger instances is leaked on exit.
Definition: logging.cpp:28
BCLog::BENCH
@ BENCH
Definition: logging.h:44
BCLog::Logger::m_file_mutex
std::mutex m_file_mutex
Definition: logging.h:75
BCLog::Logger::m_fileout
FILE * m_fileout
Definition: logging.h:74
DateTimeStrFormat
std::string DateTimeStrFormat(const char *pszFormat, int64_t nTime)
Definition: utiltime.cpp:50
BCLog::ESTIMATEFEE
@ ESTIMATEFEE
Definition: logging.h:48
BCLog::PROXY
@ PROXY
Definition: logging.h:55
BCLog::PRUNE
@ PRUNE
Definition: logging.h:54
BCLog::MEMPOOLREJ
@ MEMPOOLREJ
Definition: logging.h:56
BCLog::RPC
@ RPC
Definition: logging.h:47
CLogCategoryActive
Definition: logging.h:31
CLogCategoryDesc::category
std::string category
Definition: logging.cpp:97
BCLog::POA
@ POA
Definition: logging.h:65
BCLog::MNPING
@ MNPING
Definition: logging.h:64
BCLog::SUPPLY
@ SUPPLY
Definition: logging.h:66
BCLog::LEVELDB
@ LEVELDB
Definition: logging.h:60
BCLog::Logger::ShrinkDebugFile
void ShrinkDebugFile()
Definition: logging.cpp:231
BCLog::LIBEVENT
@ LIBEVENT
Definition: logging.h:57
BCLog::Logger::LogTimestampStr
std::string LogTimestampStr(const std::string &str)
Definition: logging.cpp:179
BCLog::Logger::m_msgs_before_open
std::list< std::string > m_msgs_before_open
Definition: logging.h:76
BCLog::Logger::EnableCategory
void EnableCategory(LogFlags flag)
Definition: logging.cpp:58
BCLog::TOR
@ TOR
Definition: logging.h:41
BCLog::REINDEX
@ REINDEX
Definition: logging.h:51
BCLog::QT
@ QT
Definition: logging.h:59
BCLog::ALL
@ ALL
Definition: logging.h:68
BCLog::DB
@ DB
Definition: logging.h:46
BCLog::Logger::m_file_path
fs::path m_file_path
Definition: logging.h:97
ListLogCategories
std::string ListLogCategories()
Returns a string with the supported log categories.
Definition: logging.cpp:149
BCLog::RAND
@ RAND
Definition: logging.h:53
BCLog::COINDB
@ COINDB
Definition: logging.h:58
BCLog::Logger::DefaultShrinkDebugFile
bool DefaultShrinkDebugFile() const
Definition: logging.cpp:89
BCLog::MEMPOOL
@ MEMPOOL
Definition: logging.h:42
BCLog::MASTERNODE
@ MASTERNODE
Definition: logging.h:62
BCLog::Logger
Definition: logging.h:71
BCLog::STAKING
@ STAKING
Definition: logging.h:61
BCLog::Logger::LogPrintStr
int LogPrintStr(const std::string &str)
Send a string to the log output.
Definition: logging.cpp:199
logging.h
CLogCategoryActive::active
bool active
Definition: logging.h:34
DEFAULT_DEBUGLOGFILE
const char *const DEFAULT_DEBUGLOGFILE
Server/client environment: argument handling, config file parsing, thread wrappers.
Definition: logging.cpp:12
BCLog::SELECTCOINS
@ SELECTCOINS
Definition: logging.h:50
LogCategories
const CLogCategoryDesc LogCategories[]
Definition: logging.cpp:100
BCLog::ADDRMAN
@ ADDRMAN
Definition: logging.h:49
CLogCategoryActive::category
std::string category
Definition: logging.h:33
BCLog::DELETETX
@ DELETETX
Definition: logging.h:67
BCLog::MNBUDGET
@ MNBUDGET
Definition: logging.h:63
BCLog::Logger::DisableCategory
void DisableCategory(LogFlags flag)
Definition: logging.cpp:71
BCLog::NET
@ NET
Definition: logging.h:40
BCLog::CMPCTBLOCK
@ CMPCTBLOCK
Definition: logging.h:52
CLogCategoryDesc::flag
BCLog::LogFlags flag
Definition: logging.cpp:96
BCLog::NONE
@ NONE
Definition: logging.h:39
fsbridge::freopen
FILE * freopen(const fs::path &p, const char *mode, FILE *stream)
Definition: fs.cpp:18