Open-Transactions  0.93.0-ge03d287
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
OTLog.hpp
Go to the documentation of this file.
1 /************************************************************
2  *
3  * OTLog.hpp
4  *
5  */
6 
7 /************************************************************
8  -----BEGIN PGP SIGNED MESSAGE-----
9  Hash: SHA1
10 
11  * OPEN TRANSACTIONS
12  *
13  * Financial Cryptography and Digital Cash
14  * Library, Protocol, API, Server, CLI, GUI
15  *
16  * -- Anonymous Numbered Accounts.
17  * -- Untraceable Digital Cash.
18  * -- Triple-Signed Receipts.
19  * -- Cheques, Vouchers, Transfers, Inboxes.
20  * -- Basket Currencies, Markets, Payment Plans.
21  * -- Signed, XML, Ricardian-style Contracts.
22  * -- Scripted smart contracts.
23  *
24  * Copyright (C) 2010-2013 by "Fellow Traveler" (A pseudonym)
25  *
26  * EMAIL:
28  *
29  * BITCOIN: 1NtTPVVjDsUfDWybS4BwvHpG2pdS9RnYyQ
30  *
31  * KEY FINGERPRINT (PGP Key in license file):
32  * 9DD5 90EB 9292 4B48 0484 7910 0308 00ED F951 BB8E
33  *
34  * OFFICIAL PROJECT WIKI(s):
35  * https://github.com/FellowTraveler/Moneychanger
36  * https://github.com/FellowTraveler/Open-Transactions/wiki
37  *
38  * WEBSITE:
39  * http://www.OpenTransactions.org/
40  *
41  * Components and licensing:
42  * -- Moneychanger..A Java client GUI.....LICENSE:.....GPLv3
43  * -- otlib.........A class library.......LICENSE:...LAGPLv3
44  * -- otapi.........A client API..........LICENSE:...LAGPLv3
45  * -- opentxs/ot....Command-line client...LICENSE:...LAGPLv3
46  * -- otserver......Server Application....LICENSE:....AGPLv3
47  * Github.com/FellowTraveler/Open-Transactions/wiki/Components
48  *
49  * All of the above OT components were designed and written by
50  * Fellow Traveler, with the exception of Moneychanger, which
51  * was contracted out to Vicky C ([email protected]).
52  * The open-source community has since actively contributed.
53  *
54  * -----------------------------------------------------
55  *
56  * LICENSE:
57  * This program is free software: you can redistribute it
58  * and/or modify it under the terms of the GNU Affero
59  * General Public License as published by the Free Software
60  * Foundation, either version 3 of the License, or (at your
61  * option) any later version.
62  *
63  * ADDITIONAL PERMISSION under the GNU Affero GPL version 3
64  * section 7: (This paragraph applies only to the LAGPLv3
65  * components listed above.) If you modify this Program, or
66  * any covered work, by linking or combining it with other
67  * code, such other code is not for that reason alone subject
68  * to any of the requirements of the GNU Affero GPL version 3.
69  * (==> This means if you are only using the OT API, then you
70  * don't have to open-source your code--only your changes to
71  * Open-Transactions itself must be open source. Similar to
72  * LGPLv3, except it applies to software-as-a-service, not
73  * just to distributing binaries.)
74  *
75  * Extra WAIVER for OpenSSL, Lucre, and all other libraries
76  * used by Open Transactions: This program is released under
77  * the AGPL with the additional exemption that compiling,
78  * linking, and/or using OpenSSL is allowed. The same is true
79  * for any other open source libraries included in this
80  * project: complete waiver from the AGPL is hereby granted to
81  * compile, link, and/or use them with Open-Transactions,
82  * according to their own terms, as long as the rest of the
83  * Open-Transactions terms remain respected, with regard to
84  * the Open-Transactions code itself.
85  *
86  * Lucre License:
87  * This code is also "dual-license", meaning that Ben Lau-
88  * rie's license must also be included and respected, since
89  * the code for Lucre is also included with Open Transactions.
90  * See Open-Transactions/src/otlib/lucre/LUCRE_LICENSE.txt
91  * The Laurie requirements are light, but if there is any
92  * problem with his license, simply remove the Lucre code.
93  * Although there are no other blind token algorithms in Open
94  * Transactions (yet. credlib is coming), the other functions
95  * will continue to operate.
96  * See Lucre on Github: https://github.com/benlaurie/lucre
97  * -----------------------------------------------------
98  * You should have received a copy of the GNU Affero General
99  * Public License along with this program. If not, see:
100  * http://www.gnu.org/licenses/
101  *
102  * If you would like to use this software outside of the free
103  * software license, please contact FellowTraveler.
104  * (Unfortunately many will run anonymously and untraceably,
105  * so who could really stop them?)
106  *
107  * DISCLAIMER:
108  * This program is distributed in the hope that it will be
109  * useful, but WITHOUT ANY WARRANTY; without even the implied
110  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
111  * PURPOSE. See the GNU Affero General Public License for
112  * more details.
113 
114  -----BEGIN PGP SIGNATURE-----
115  Version: GnuPG v1.4.9 (Darwin)
116 
117  iQIcBAEBAgAGBQJRSsfJAAoJEAMIAO35UbuOQT8P/RJbka8etf7wbxdHQNAY+2cC
118  vDf8J3X8VI+pwMqv6wgTVy17venMZJa4I4ikXD/MRyWV1XbTG0mBXk/7AZk7Rexk
119  KTvL/U1kWiez6+8XXLye+k2JNM6v7eej8xMrqEcO0ZArh/DsLoIn1y8p8qjBI7+m
120  aE7lhstDiD0z8mwRRLKFLN2IH5rAFaZZUvj5ERJaoYUKdn4c+RcQVei2YOl4T0FU
121  LWND3YLoH8naqJXkaOKEN4UfJINCwxhe5Ke9wyfLWLUO7NamRkWD2T7CJ0xocnD1
122  sjAzlVGNgaFDRflfIF4QhBx1Ddl6wwhJfw+d08bjqblSq8aXDkmFA7HeunSFKkdn
123  oIEOEgyj+veuOMRJC5pnBJ9vV+7qRdDKQWaCKotynt4sWJDGQ9kWGWm74SsNaduN
124  TPMyr9kNmGsfR69Q2Zq/FLcLX/j8ESxU+HYUB4vaARw2xEOu2xwDDv6jt0j3Vqsg
125  x7rWv4S/Eh18FDNDkVRChiNoOIilLYLL6c38uMf1pnItBuxP3uhgY6COm59kVaRh
126  nyGTYCDYD2TK+fI9o89F1297uDCwEJ62U0Q7iTDp5QuXCoxkPfv8/kX6lS6T3y9G
127  M9mqIoLbIQ1EDntFv7/t6fUTS2+46uCrdZWbQ5RjYXdrzjij02nDmJAm2BngnZvd
128  kamH0Y/n11lCvo1oQxM+
129  =uSzz
130  -----END PGP SIGNATURE-----
131  **************************************************************/
132 
133 #ifndef OPENTXS_CORE_OTLOG_HPP
134 #define OPENTXS_CORE_OTLOG_HPP
135 
136 #include "OTString.hpp"
137 #include "util/Assert.hpp"
138 
139 #include <deque>
140 #include <ostream>
141 #include <streambuf>
142 
143 #if defined(unix) || defined(__unix__) || defined(__unix) || \
144  defined(__APPLE__) || defined(linux) || defined(__linux) || \
145  defined(__linux__)
146 #define PREDEF_PLATFORM_UNIX 1
147 #endif
148 
149 #if defined(debug) || defined(_DEBUG) || defined(DEBUG)
150 #define PREDEF_MODE_DEBUG 1
151 #endif
152 
153 namespace opentxs
154 {
155 
156 typedef std::deque<OTString*> dequeOfStrings;
157 
158 class OTLogStream;
159 
160 #ifdef _WIN32
161 #ifdef OTLOG_IMPORT
162 #undef OTLOG_IMPORT
163 #define OTLOG_IMPORT __declspec(dllexport)
164 #else
165 #define OTLOG_IMPORT __declspec(dllimport)
166 #endif
167 #else
168 #ifndef OTLOG_IMPORT
169 #define OTLOG_IMPORT
170 #endif
171 #endif
172 
173 OTLOG_IMPORT extern OTLogStream otErr; // logs using OTLog::vError()
174 OTLOG_IMPORT extern OTLogStream otInfo; // logs using OTLog::vOutput(2)
175 OTLOG_IMPORT extern OTLogStream otOut; // logs using OTLog::vOutput(0)
176 OTLOG_IMPORT extern OTLogStream otWarn; // logs using OTLog::vOutput(1)
177 OTLOG_IMPORT extern OTLogStream otLog3; // logs using OTLog::vOutput(3)
178 OTLOG_IMPORT extern OTLogStream otLog4; // logs using OTLog::vOutput(4)
179 OTLOG_IMPORT extern OTLogStream otLog5; // logs using OTLog::vOutput(5)
180 
181 class OTLogStream : public std::ostream, std::streambuf
182 {
183 private:
184  int logLevel;
185  int next;
186  char* pBuffer;
187 
188 public:
189  OTLogStream(int _logLevel);
190  ~OTLogStream();
191 
192  virtual int overflow(int c);
193 };
194 
195 // cppcheck-suppress noConstructor
196 class OTLog
197 {
198 private:
199  // public: // should be private, but since of bug in msvc.
200 
201  static OTLog* pLogger;
202 
203  static const OTString m_strVersion;
204  static const OTString m_strPathSeparator;
205 
206  dequeOfStrings logDeque;
207 
208  OTString m_strThreadContext;
209  OTString m_strLogFileName;
210  OTString m_strLogFilePath;
211 
212  int32_t m_nLogLevel;
213 
214  bool m_bInitialized;
215 
216  // For things that represent internal inconsistency in the code.
217  // Normally should NEVER happen even with bad input from user.
218  // (Don't call this directly. Use the above #defined macro instead.)
219  static Assert::fpt_Assert_sz_n_sz(logAssert);
220 
221  static bool CheckLogger(OTLog* pLogger);
222 
223 public:
224  // EXPORT static OTLog& It();
225 
226  // now the logger checks the global config file itself for the log-filename.
227  EXPORT static bool Init(const OTString& strThreadContext = "",
228  const int32_t& nLogLevel = 0);
229 
230  EXPORT static bool IsInitialized();
231 
232  EXPORT static bool Cleanup();
233 
234  // OTLog Constants.
235  //
236 
237  // Compiled into OTLog:
238 
239  EXPORT static const char* Version();
240  EXPORT static const OTString& GetVersion();
241 
242  EXPORT static const char* PathSeparator();
243  EXPORT static const OTString& GetPathSeparator();
244 
245  // Set in constructor:
246 
247  EXPORT static const OTString& GetThreadContext();
248 
249  EXPORT static const char* LogFilePath();
250  EXPORT static const OTString& GetLogFilePath();
251 
252  EXPORT static int32_t LogLevel();
253  EXPORT static bool SetLogLevel(const int32_t& nLogLevel);
254 
255  // OTLog Functions:
256  //
257 
258  EXPORT static bool LogToFile(const OTString& strOutput);
259 
260  // We keep 1024 logs in memory, to make them available via the API.
261  EXPORT static int32_t GetMemlogSize();
262  EXPORT static OTString GetMemlogAtIndex(int32_t nIndex);
263  EXPORT static OTString PeekMemlogFront();
264  EXPORT static OTString PeekMemlogBack();
265  EXPORT static bool PopMemlogFront();
266  EXPORT static bool PopMemlogBack();
267  EXPORT static bool PushMemlogFront(const OTString& strLog);
268  EXPORT static bool PushMemlogBack(const OTString& strLog);
269  EXPORT static bool SleepSeconds(int64_t lSeconds);
270  EXPORT static bool SleepMilliseconds(int64_t lMilliseconds);
271 
272  // Output() logs normal output, which carries a verbosity level.
273  //
274  // If nVerbosity of a message is 0, the message will ALWAYS log. (ALL output
275  // levels are higher or equal to 0.)
276  // If nVerbosity is 1, the message will run only if __CurrentLogLevel is 1
277  // or higher.
278  // If nVerbosity if 2, the message will run only if __CurrentLogLevel is 2
279  // or higher.
280  // Etc.
281  // THEREFORE: The higher the verbosity level for a message, the more verbose
282  // the
283  // software must be configured in order to display that message.
284  //
285  // Default verbosity level for the software is 0, and output that MUST
286  // appear on
287  // the screen should be set at level 0. For output that you don't want to
288  // see as often,
289  // set it up to 1. Set it up even higher for the really verbose stuff (e.g.
290  // only if you
291  // really want to see EVERYTHING.)
292 
293  EXPORT static void Output(int32_t nVerbosity,
294  const char* szOutput); // stdout
295  EXPORT static void vOutput(int32_t nVerbosity, const char* szOutput, ...);
296 
297  // This logs an error condition, which usually means bad input from the
298  // user, or a file wouldn't open,
299  // or something like that. This contrasted with Assert() which should NEVER
300  // actually happen. The software
301  // expects bad user input from time to time. But it never expects a loaded
302  // mint to have a nullptr pointer.
303  // The bad input would log with Error(), whereas the nullptr pointer would
304  // log
305  // with Assert();
306  EXPORT static void Error(const char* szError); // stderr
307  EXPORT static void vError(const char* szError, ...); // stderr
308 
309  // This method will print out errno and its associated string.
310  // Optionally you can pass the location you are calling it from,
311  // which will be prepended to the log.
312  //
313  EXPORT static void Errno(const char* szLocation = nullptr); // stderr
314 
315  // String Helpers
316  EXPORT static bool StringFill(OTString& out_strString, const char* szString,
317  int32_t iLength,
318  const char* szAppend = nullptr);
319 
320  EXPORT static void SetupSignalHandler(); // OPTIONAL. Therefore I will call
321  // it in xmlrpcxx_client.cpp just
322  // above OT_Init.
323 };
324 
325 } // namespace opentxs
326 
327 #endif // OPENTXS_CORE_OTLOG_HPP
static EXPORT void vError(const char *szError,...)
Definition: OTLog.cpp:800
OTLOG_IMPORT OTLogStream otLog4
virtual int overflow(int c)
Definition: OTLog.cpp:278
static EXPORT void Output(int32_t nVerbosity, const char *szOutput)
Definition: OTLog.cpp:710
static EXPORT const OTString & GetVersion()
Definition: OTLog.cpp:403
static EXPORT const OTString & GetThreadContext()
Definition: OTLog.cpp:419
static EXPORT bool PopMemlogBack()
Definition: OTLog.cpp:592
static EXPORT bool LogToFile(const OTString &strOutput)
Definition: OTLog.cpp:462
static EXPORT const char * PathSeparator()
Definition: OTLog.cpp:408
static EXPORT bool SetLogLevel(const int32_t &nLogLevel)
Definition: OTLog.cpp:443
static EXPORT OTString GetMemlogAtIndex(int32_t nIndex)
Definition: OTLog.cpp:499
OTLOG_IMPORT OTLogStream otOut
OTLOG_IMPORT OTLogStream otLog3
static EXPORT int32_t GetMemlogSize()
Definition: OTLog.cpp:526
size_t( fpt_Assert_sz_n_sz)(const char *, size_t, const char *)
Definition: Assert.hpp:164
static EXPORT void Error(const char *szError)
Definition: OTLog.cpp:831
static EXPORT bool PopMemlogFront()
Definition: OTLog.cpp:575
static EXPORT bool Init(const OTString &strThreadContext="", const int32_t &nLogLevel=0)
Definition: OTLog.cpp:300
static EXPORT const char * Version()
Definition: OTLog.cpp:399
static EXPORT void SetupSignalHandler()
Definition: OTLog.cpp:1315
OTLogStream(int _logLevel)
Definition: OTLog.cpp:264
static EXPORT const char * LogFilePath()
Definition: OTLog.cpp:424
#define OTLOG_IMPORT
Definition: OTLog.hpp:169
static EXPORT OTString PeekMemlogBack()
Definition: OTLog.cpp:554
static EXPORT bool Cleanup()
Definition: OTLog.cpp:377
static EXPORT bool IsInitialized()
Definition: OTLog.cpp:371
static EXPORT bool PushMemlogBack(const OTString &strLog)
Definition: OTLog.cpp:627
OTLOG_IMPORT OTLogStream otInfo
static EXPORT bool SleepMilliseconds(int64_t lMilliseconds)
Definition: OTLog.cpp:651
static EXPORT bool SleepSeconds(int64_t lSeconds)
Definition: OTLog.cpp:640
static EXPORT int32_t LogLevel()
Definition: OTLog.cpp:434
OTLOG_IMPORT OTLogStream otWarn
OTLOG_IMPORT OTLogStream otErr
static EXPORT const OTString & GetPathSeparator()
Definition: OTLog.cpp:412
static EXPORT const OTString & GetLogFilePath()
Definition: OTLog.cpp:428
static EXPORT void Errno(const char *szLocation=nullptr)
Definition: OTLog.cpp:859
static EXPORT bool StringFill(OTString &out_strString, const char *szString, int32_t iLength, const char *szAppend=nullptr)
Definition: OTLog.cpp:900
static EXPORT void vOutput(int32_t nVerbosity, const char *szOutput,...)
Definition: OTLog.cpp:768
std::deque< OTString * > dequeOfStrings
Definition: OTLog.hpp:156
static EXPORT bool PushMemlogFront(const OTString &strLog)
Definition: OTLog.cpp:609
static EXPORT OTString PeekMemlogFront()
Definition: OTLog.cpp:534
OTLOG_IMPORT OTLogStream otLog5