alert system
[novacoin.git] / util.cpp
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
4
5 #include "headers.h"
6
7
8 map<string, string> mapArgs;
9 map<string, vector<string> > mapMultiArgs;
10 bool fDebug = false;
11 bool fPrintToConsole = false;
12 bool fPrintToDebugger = false;
13 char pszSetDataDir[MAX_PATH] = "";
14 bool fShutdown = false;
15 bool fDaemon = false;
16 bool fCommandLine = false;
17 string strMiscWarning;
18
19
20
21
22
23 // Workaround for "multiple definition of `_tls_used'"
24 // http://svn.boost.org/trac/boost/ticket/4258
25 extern "C" void tss_cleanup_implemented() { }
26
27
28
29
30
31 // Init openssl library multithreading support
32 static boost::interprocess::interprocess_mutex** ppmutexOpenSSL;
33 void locking_callback(int mode, int i, const char* file, int line)
34 {
35     if (mode & CRYPTO_LOCK)
36         ppmutexOpenSSL[i]->lock();
37     else
38         ppmutexOpenSSL[i]->unlock();
39 }
40
41 // Init
42 class CInit
43 {
44 public:
45     CInit()
46     {
47         // Init openssl library multithreading support
48         ppmutexOpenSSL = (boost::interprocess::interprocess_mutex**)OPENSSL_malloc(CRYPTO_num_locks() * sizeof(boost::interprocess::interprocess_mutex*));
49         for (int i = 0; i < CRYPTO_num_locks(); i++)
50             ppmutexOpenSSL[i] = new boost::interprocess::interprocess_mutex();
51         CRYPTO_set_locking_callback(locking_callback);
52
53 #ifdef __WXMSW__
54         // Seed random number generator with screen scrape and other hardware sources
55         RAND_screen();
56 #endif
57
58         // Seed random number generator with performance counter
59         RandAddSeed();
60     }
61     ~CInit()
62     {
63         // Shutdown openssl library multithreading support
64         CRYPTO_set_locking_callback(NULL);
65         for (int i = 0; i < CRYPTO_num_locks(); i++)
66             delete ppmutexOpenSSL[i];
67         OPENSSL_free(ppmutexOpenSSL);
68     }
69 }
70 instance_of_cinit;
71
72
73
74
75
76
77
78
79 void RandAddSeed()
80 {
81     // Seed with CPU performance counter
82     int64 nCounter = PerformanceCounter();
83     RAND_add(&nCounter, sizeof(nCounter), 1.5);
84     memset(&nCounter, 0, sizeof(nCounter));
85 }
86
87 void RandAddSeedPerfmon()
88 {
89     RandAddSeed();
90
91     // This can take up to 2 seconds, so only do it every 10 minutes
92     static int64 nLastPerfmon;
93     if (GetTime() < nLastPerfmon + 10 * 60)
94         return;
95     nLastPerfmon = GetTime();
96
97 #ifdef __WXMSW__
98     // Don't need this on Linux, OpenSSL automatically uses /dev/urandom
99     // Seed with the entire set of perfmon data
100     unsigned char pdata[250000];
101     memset(pdata, 0, sizeof(pdata));
102     unsigned long nSize = sizeof(pdata);
103     long ret = RegQueryValueExA(HKEY_PERFORMANCE_DATA, "Global", NULL, NULL, pdata, &nSize);
104     RegCloseKey(HKEY_PERFORMANCE_DATA);
105     if (ret == ERROR_SUCCESS)
106     {
107         RAND_add(pdata, nSize, nSize/100.0);
108         memset(pdata, 0, nSize);
109         printf("%s RandAddSeed() %d bytes\n", DateTimeStrFormat("%x %H:%M", GetTime()).c_str(), nSize);
110     }
111 #endif
112 }
113
114 uint64 GetRand(uint64 nMax)
115 {
116     if (nMax == 0)
117         return 0;
118
119     // The range of the random source must be a multiple of the modulus
120     // to give every possible output value an equal possibility
121     uint64 nRange = (UINT64_MAX / nMax) * nMax;
122     uint64 nRand = 0;
123     do
124         RAND_bytes((unsigned char*)&nRand, sizeof(nRand));
125     while (nRand >= nRange);
126     return (nRand % nMax);
127 }
128
129
130
131
132
133
134
135
136
137
138
139 inline int OutputDebugStringF(const char* pszFormat, ...)
140 {
141     int ret = 0;
142     if (fPrintToConsole)
143     {
144         // print to console
145         va_list arg_ptr;
146         va_start(arg_ptr, pszFormat);
147         ret = vprintf(pszFormat, arg_ptr);
148         va_end(arg_ptr);
149     }
150     else
151     {
152         // print to debug.log
153         char pszFile[MAX_PATH+100];
154         GetDataDir(pszFile);
155         strlcat(pszFile, "/debug.log", sizeof(pszFile));
156         FILE* fileout = fopen(pszFile, "a");
157         if (fileout)
158         {
159             //// Debug print useful for profiling
160             //fprintf(fileout, " %"PRI64d" ", GetTimeMillis());
161             va_list arg_ptr;
162             va_start(arg_ptr, pszFormat);
163             ret = vfprintf(fileout, pszFormat, arg_ptr);
164             va_end(arg_ptr);
165             fclose(fileout);
166         }
167     }
168
169 #ifdef __WXMSW__
170     if (fPrintToDebugger)
171     {
172         // accumulate a line at a time
173         static CCriticalSection cs_OutputDebugStringF;
174         CRITICAL_BLOCK(cs_OutputDebugStringF)
175         {
176             static char pszBuffer[50000];
177             static char* pend;
178             if (pend == NULL)
179                 pend = pszBuffer;
180             va_list arg_ptr;
181             va_start(arg_ptr, pszFormat);
182             int limit = END(pszBuffer) - pend - 2;
183             int ret = _vsnprintf(pend, limit, pszFormat, arg_ptr);
184             va_end(arg_ptr);
185             if (ret < 0 || ret >= limit)
186             {
187                 pend = END(pszBuffer) - 2;
188                 *pend++ = '\n';
189             }
190             else
191                 pend += ret;
192             *pend = '\0';
193             char* p1 = pszBuffer;
194             char* p2;
195             while (p2 = strchr(p1, '\n'))
196             {
197                 p2++;
198                 char c = *p2;
199                 *p2 = '\0';
200                 OutputDebugStringA(p1);
201                 *p2 = c;
202                 p1 = p2;
203             }
204             if (p1 != pszBuffer)
205                 memmove(pszBuffer, p1, pend - p1 + 1);
206             pend -= (p1 - pszBuffer);
207         }
208     }
209 #endif
210     return ret;
211 }
212
213
214 // Safer snprintf
215 //  - prints up to limit-1 characters
216 //  - output string is always null terminated even if limit reached
217 //  - return value is the number of characters actually printed
218 int my_snprintf(char* buffer, size_t limit, const char* format, ...)
219 {
220     if (limit == 0)
221         return 0;
222     va_list arg_ptr;
223     va_start(arg_ptr, format);
224     int ret = _vsnprintf(buffer, limit, format, arg_ptr);
225     va_end(arg_ptr);
226     if (ret < 0 || ret >= limit)
227     {
228         ret = limit - 1;
229         buffer[limit-1] = 0;
230     }
231     return ret;
232 }
233
234
235 string strprintf(const char* format, ...)
236 {
237     char buffer[50000];
238     char* p = buffer;
239     int limit = sizeof(buffer);
240     int ret;
241     loop
242     {
243         va_list arg_ptr;
244         va_start(arg_ptr, format);
245         ret = _vsnprintf(p, limit, format, arg_ptr);
246         va_end(arg_ptr);
247         if (ret >= 0 && ret < limit)
248             break;
249         if (p != buffer)
250             delete p;
251         limit *= 2;
252         p = new char[limit];
253         if (p == NULL)
254             throw std::bad_alloc();
255     }
256     string str(p, p+ret);
257     if (p != buffer)
258         delete p;
259     return str;
260 }
261
262
263 bool error(const char* format, ...)
264 {
265     char buffer[50000];
266     int limit = sizeof(buffer);
267     va_list arg_ptr;
268     va_start(arg_ptr, format);
269     int ret = _vsnprintf(buffer, limit, format, arg_ptr);
270     va_end(arg_ptr);
271     if (ret < 0 || ret >= limit)
272     {
273         ret = limit - 1;
274         buffer[limit-1] = 0;
275     }
276     printf("ERROR: %s\n", buffer);
277     return false;
278 }
279
280
281 void ParseString(const string& str, char c, vector<string>& v)
282 {
283     if (str.empty())
284         return;
285     string::size_type i1 = 0;
286     string::size_type i2;
287     loop
288     {
289         i2 = str.find(c, i1);
290         if (i2 == str.npos)
291         {
292             v.push_back(str.substr(i1));
293             return;
294         }
295         v.push_back(str.substr(i1, i2-i1));
296         i1 = i2+1;
297     }
298 }
299
300
301 string FormatMoney(int64 n, bool fPlus)
302 {
303     n /= CENT;
304     string str = strprintf("%"PRI64d".%02"PRI64d, (n > 0 ? n : -n)/100, (n > 0 ? n : -n)%100);
305     for (int i = 6; i < str.size(); i += 4)
306         if (isdigit(str[str.size() - i - 1]))
307             str.insert(str.size() - i, 1, ',');
308     if (n < 0)
309         str.insert((unsigned int)0, 1, '-');
310     else if (fPlus && n > 0)
311         str.insert((unsigned int)0, 1, '+');
312     return str;
313 }
314
315
316 bool ParseMoney(const string& str, int64& nRet)
317 {
318     return ParseMoney(str.c_str(), nRet);
319 }
320
321 bool ParseMoney(const char* pszIn, int64& nRet)
322 {
323     string strWhole;
324     int64 nCents = 0;
325     const char* p = pszIn;
326     while (isspace(*p))
327         p++;
328     for (; *p; p++)
329     {
330         if (*p == ',' && p > pszIn && isdigit(p[-1]) && isdigit(p[1]) && isdigit(p[2]) && isdigit(p[3]) && !isdigit(p[4]))
331             continue;
332         if (*p == '.')
333         {
334             p++;
335             if (isdigit(*p))
336             {
337                 nCents = 10 * (*p++ - '0');
338                 if (isdigit(*p))
339                     nCents += (*p++ - '0');
340             }
341             break;
342         }
343         if (isspace(*p))
344             break;
345         if (!isdigit(*p))
346             return false;
347         strWhole.insert(strWhole.end(), *p);
348     }
349     for (; *p; p++)
350         if (!isspace(*p))
351             return false;
352     if (strWhole.size() > 14)
353         return false;
354     if (nCents < 0 || nCents > 99)
355         return false;
356     int64 nWhole = atoi64(strWhole);
357     int64 nPreValue = nWhole * 100 + nCents;
358     int64 nValue = nPreValue * CENT;
359     if (nValue / CENT != nPreValue)
360         return false;
361     if (nValue / COIN != nWhole)
362         return false;
363     nRet = nValue;
364     return true;
365 }
366
367
368 vector<unsigned char> ParseHex(const char* psz)
369 {
370     static char phexdigit[256] =
371     { -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
372       -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
373       -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
374       0,1,2,3,4,5,6,7,8,9,-1,-1,-1,-1,-1,-1,
375       -1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1,
376       -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
377       -1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1
378       -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
379       -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
380       -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
381       -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
382       -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
383       -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
384       -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
385       -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
386       -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, };
387
388     // convert hex dump to vector
389     vector<unsigned char> vch;
390     loop
391     {
392         while (isspace(*psz))
393             psz++;
394         char c = phexdigit[(unsigned char)*psz++];
395         if (c == -1)
396             break;
397         unsigned char n = (c << 4);
398         c = phexdigit[(unsigned char)*psz++];
399         if (c == -1)
400             break;
401         n |= c;
402         vch.push_back(n);
403     }
404     return vch;
405 }
406
407 vector<unsigned char> ParseHex(const std::string& str)
408 {
409     return ParseHex(str.c_str());
410 }
411
412
413 void ParseParameters(int argc, char* argv[])
414 {
415     mapArgs.clear();
416     mapMultiArgs.clear();
417     for (int i = 1; i < argc; i++)
418     {
419         char psz[10000];
420         strlcpy(psz, argv[i], sizeof(psz));
421         char* pszValue = (char*)"";
422         if (strchr(psz, '='))
423         {
424             pszValue = strchr(psz, '=');
425             *pszValue++ = '\0';
426         }
427         #ifdef __WXMSW__
428         _strlwr(psz);
429         if (psz[0] == '/')
430             psz[0] = '-';
431         #endif
432         if (psz[0] != '-')
433             break;
434         mapArgs[psz] = pszValue;
435         mapMultiArgs[psz].push_back(pszValue);
436     }
437 }
438
439
440 const char* wxGetTranslation(const char* pszEnglish)
441 {
442 #ifdef GUI
443     // Wrapper of wxGetTranslation returning the same const char* type as was passed in
444     static CCriticalSection cs;
445     CRITICAL_BLOCK(cs)
446     {
447         // Look in cache
448         static map<string, char*> mapCache;
449         map<string, char*>::iterator mi = mapCache.find(pszEnglish);
450         if (mi != mapCache.end())
451             return (*mi).second;
452
453         // wxWidgets translation
454         wxString strTranslated = wxGetTranslation(wxString(pszEnglish, wxConvUTF8));
455
456         // We don't cache unknown strings because caller might be passing in a
457         // dynamic string and we would keep allocating memory for each variation.
458         if (strcmp(pszEnglish, strTranslated.utf8_str()) == 0)
459             return pszEnglish;
460
461         // Add to cache, memory doesn't need to be freed.  We only cache because
462         // we must pass back a pointer to permanently allocated memory.
463         char* pszCached = new char[strlen(strTranslated.utf8_str())+1];
464         strcpy(pszCached, strTranslated.utf8_str());
465         mapCache[pszEnglish] = pszCached;
466         return pszCached;
467     }
468     return NULL;
469 #else
470     return pszEnglish;
471 #endif
472 }
473
474
475
476
477
478
479
480
481
482
483 void FormatException(char* pszMessage, std::exception* pex, const char* pszThread)
484 {
485 #ifdef __WXMSW__
486     char pszModule[MAX_PATH];
487     pszModule[0] = '\0';
488     GetModuleFileNameA(NULL, pszModule, sizeof(pszModule));
489 #else
490     const char* pszModule = "bitcoin";
491 #endif
492     if (pex)
493         snprintf(pszMessage, 1000,
494             "EXCEPTION: %s       \n%s       \n%s in %s       \n", typeid(*pex).name(), pex->what(), pszModule, pszThread);
495     else
496         snprintf(pszMessage, 1000,
497             "UNKNOWN EXCEPTION       \n%s in %s       \n", pszModule, pszThread);
498 }
499
500 void LogException(std::exception* pex, const char* pszThread)
501 {
502     char pszMessage[1000];
503     FormatException(pszMessage, pex, pszThread);
504     printf("\n%s", pszMessage);
505 }
506
507 void PrintException(std::exception* pex, const char* pszThread)
508 {
509     char pszMessage[1000];
510     FormatException(pszMessage, pex, pszThread);
511     printf("\n\n************************\n%s\n", pszMessage);
512     fprintf(stderr, "\n\n************************\n%s\n", pszMessage);
513 #ifdef GUI
514     if (wxTheApp && !fDaemon)
515         MyMessageBox(pszMessage, "Error", wxOK | wxICON_ERROR);
516 #endif
517     throw;
518     //DebugBreak();
519 }
520
521
522
523
524
525
526
527
528 #ifdef __WXMSW__
529 typedef WINSHELLAPI BOOL (WINAPI *PSHGETSPECIALFOLDERPATHA)(HWND hwndOwner, LPSTR lpszPath, int nFolder, BOOL fCreate);
530
531 string MyGetSpecialFolderPath(int nFolder, bool fCreate)
532 {
533     char pszPath[MAX_PATH+100] = "";
534
535     // SHGetSpecialFolderPath isn't always available on old Windows versions
536     HMODULE hShell32 = LoadLibraryA("shell32.dll");
537     if (hShell32)
538     {
539         PSHGETSPECIALFOLDERPATHA pSHGetSpecialFolderPath =
540             (PSHGETSPECIALFOLDERPATHA)GetProcAddress(hShell32, "SHGetSpecialFolderPathA");
541         if (pSHGetSpecialFolderPath)
542             (*pSHGetSpecialFolderPath)(NULL, pszPath, nFolder, fCreate);
543         FreeModule(hShell32);
544     }
545
546     // Backup option
547     if (pszPath[0] == '\0')
548     {
549         if (nFolder == CSIDL_STARTUP)
550         {
551             strcpy(pszPath, getenv("USERPROFILE"));
552             strcat(pszPath, "\\Start Menu\\Programs\\Startup");
553         }
554         else if (nFolder == CSIDL_APPDATA)
555         {
556             strcpy(pszPath, getenv("APPDATA"));
557         }
558     }
559
560     return pszPath;
561 }
562 #endif
563
564 string GetDefaultDataDir()
565 {
566     // Windows: C:\Documents and Settings\username\Application Data\Bitcoin
567     // Mac: ~/Library/Application Support/Bitcoin
568     // Unix: ~/.bitcoin
569 #ifdef __WXMSW__
570     // Windows
571     return MyGetSpecialFolderPath(CSIDL_APPDATA, true) + "\\Bitcoin";
572 #else
573     char* pszHome = getenv("HOME");
574     if (pszHome == NULL || strlen(pszHome) == 0)
575         pszHome = (char*)"/";
576     string strHome = pszHome;
577     if (strHome[strHome.size()-1] != '/')
578         strHome += '/';
579 #ifdef __WXMAC_OSX__
580     // Mac
581     strHome += "Library/Application Support/";
582     filesystem::create_directory(strHome.c_str());
583     return strHome + "Bitcoin";
584 #else
585     // Unix
586     return strHome + ".bitcoin";
587 #endif
588 #endif
589 }
590
591 void GetDataDir(char* pszDir)
592 {
593     // pszDir must be at least MAX_PATH length.
594     if (pszSetDataDir[0] != 0)
595     {
596         strlcpy(pszDir, pszSetDataDir, MAX_PATH);
597         static bool fMkdirDone;
598         if (!fMkdirDone)
599         {
600             fMkdirDone = true;
601             filesystem::create_directory(pszDir);
602         }
603     }
604     else
605     {
606         // This can be called during exceptions by printf, so we cache the
607         // value so we don't have to do memory allocations after that.
608         static char pszCachedDir[MAX_PATH];
609         if (pszCachedDir[0] == 0)
610         {
611             strlcpy(pszCachedDir, GetDefaultDataDir().c_str(), sizeof(pszCachedDir));
612             filesystem::create_directory(pszCachedDir);
613         }
614         strlcpy(pszDir, pszCachedDir, MAX_PATH);
615     }
616 }
617
618 string GetDataDir()
619 {
620     char pszDir[MAX_PATH];
621     GetDataDir(pszDir);
622     return pszDir;
623 }
624
625 string GetConfigFile()
626 {
627     namespace fs = boost::filesystem;
628     fs::path pathConfig(mapArgs.count("-conf") ? mapArgs["-conf"] : string("bitcoin.conf"));
629     if (!pathConfig.is_complete())
630         pathConfig = fs::path(GetDataDir()) / pathConfig;
631     return pathConfig.string();
632 }
633
634 void ReadConfigFile(map<string, string>& mapSettingsRet,
635                     map<string, vector<string> >& mapMultiSettingsRet)
636 {
637     namespace fs = boost::filesystem;
638     namespace pod = boost::program_options::detail;
639
640     fs::ifstream streamConfig(GetConfigFile());
641     if (!streamConfig.good())
642         return;
643
644     set<string> setOptions;
645     setOptions.insert("*");
646     
647     for (pod::config_file_iterator it(streamConfig, setOptions), end; it != end; ++it)
648     {
649         // Don't overwrite existing settings so command line settings override bitcoin.conf
650         string strKey = string("-") + it->string_key;
651         if (mapSettingsRet.count(strKey) == 0)
652             mapSettingsRet[strKey] = it->value[0];
653         mapMultiSettingsRet[strKey].push_back(it->value[0]);
654     }
655 }
656
657 int GetFilesize(FILE* file)
658 {
659     int nSavePos = ftell(file);
660     int nFilesize = -1;
661     if (fseek(file, 0, SEEK_END) == 0)
662         nFilesize = ftell(file);
663     fseek(file, nSavePos, SEEK_SET);
664     return nFilesize;
665 }
666
667 void ShrinkDebugFile()
668 {
669     // Scroll debug.log if it's getting too big
670     string strFile = GetDataDir() + "/debug.log";
671     FILE* file = fopen(strFile.c_str(), "r");
672     if (file && GetFilesize(file) > 10 * 1000000)
673     {
674         // Restart the file with some of the end
675         char pch[200000];
676         fseek(file, -sizeof(pch), SEEK_END);
677         int nBytes = fread(pch, 1, sizeof(pch), file);
678         fclose(file);
679         if (file = fopen(strFile.c_str(), "w"))
680         {
681             fwrite(pch, 1, nBytes, file);
682             fclose(file);
683         }
684     }
685 }
686
687
688
689
690
691
692
693
694 //
695 // "Never go to sea with two chronometers; take one or three."
696 // Our three chronometers are:
697 //  - System clock
698 //  - Median of other server's clocks
699 //  - NTP servers
700 //
701 // note: NTP isn't implemented yet, so until then we just use the median
702 //  of other nodes clocks to correct ours.
703 //
704 int64 GetTime()
705 {
706     return time(NULL);
707 }
708
709 static int64 nTimeOffset = 0;
710
711 int64 GetAdjustedTime()
712 {
713     return GetTime() + nTimeOffset;
714 }
715
716 void AddTimeData(unsigned int ip, int64 nTime)
717 {
718     int64 nOffsetSample = nTime - GetTime();
719
720     // Ignore duplicates
721     static set<unsigned int> setKnown;
722     if (!setKnown.insert(ip).second)
723         return;
724
725     // Add data
726     static vector<int64> vTimeOffsets;
727     if (vTimeOffsets.empty())
728         vTimeOffsets.push_back(0);
729     vTimeOffsets.push_back(nOffsetSample);
730     printf("Added time data, samples %d, offset %+"PRI64d" (%+"PRI64d" minutes)\n", vTimeOffsets.size(), vTimeOffsets.back(), vTimeOffsets.back()/60);
731     if (vTimeOffsets.size() >= 5 && vTimeOffsets.size() % 2 == 1)
732     {
733         sort(vTimeOffsets.begin(), vTimeOffsets.end());
734         int64 nMedian = vTimeOffsets[vTimeOffsets.size()/2];
735         // Only let other nodes change our time by so much
736         if (abs64(nMedian) < 70 * 60)
737         {
738             nTimeOffset = nMedian;
739         }
740         else
741         {
742             nTimeOffset = 0;
743             // If nobody else has the same time as us, give a warning
744             bool fMatch = false;
745             foreach(int64 nOffset, vTimeOffsets)
746                 if (nOffset != 0 && abs64(nOffset) < 10 * 60)
747                     fMatch = true;
748             static bool fDone;
749             if (!fMatch && !fDone)
750             {
751                 fDone = true;
752                 string strMessage = _("Warning: Check your system date and time, you may not be able to generate or receive the most recent blocks!");
753                 strMiscWarning = strMessage;
754                 printf("*** %s\n", strMessage.c_str());
755                 boost::thread(bind(ThreadSafeMessageBox, strMessage+" ", string("Bitcoin"), wxOK | wxICON_EXCLAMATION, (wxWindow*)NULL, -1, -1));
756             }
757         }
758         foreach(int64 n, vTimeOffsets)
759             printf("%+"PRI64d"  ", n);
760         printf("|  nTimeOffset = %+"PRI64d"  (%+"PRI64d" minutes)\n", nTimeOffset, nTimeOffset/60);
761     }
762 }