-
Notifications
You must be signed in to change notification settings - Fork 9
/
logging.cpp
141 lines (120 loc) · 2.64 KB
/
logging.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "logging.h"
#include "BitcoinAddress.h"
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#ifdef _MSC_VER
#include <conio.h>
#pragma warning(disable:4996)
#endif
#define MAXNUMERIC 32 // JWR support up to 16 32 character long numeric formated strings
#define MAXFNUM 16
static char gFormat[MAXNUMERIC*MAXFNUM];
static int32_t gIndex = 0;
// This is a helper method for getting a formatted numeric output (basically having the commas which makes them easier to read)
const char * formatNumber(int32_t number) // JWR format this integer into a fancy comma delimited string
{
char * dest = &gFormat[gIndex*MAXNUMERIC];
gIndex++;
if (gIndex == MAXFNUM) gIndex = 0;
char scratch[512];
#ifdef _MSC_VER
itoa(number, scratch, 10);
#else
snprintf(scratch, 10, "%d", number);
#endif
char *source = scratch;
char *str = dest;
uint32_t len = (uint32_t)strlen(scratch);
if (scratch[0] == '-')
{
*str++ = '-';
source++;
len--;
}
for (uint32_t i = 0; i < len; i++)
{
int32_t place = (len - 1) - i;
*str++ = source[i];
if (place && (place % 3) == 0) *str++ = ',';
}
*str = 0;
return dest;
}
const char *getDateString(uint32_t _t)
{
time_t t(_t);
static char scratch[1024];
struct tm *gtm = gmtime(&t);
// strftime(scratch, 1024, "%m, %d, %Y", gtm);
sprintf(scratch, "%4d-%02d-%02d", gtm->tm_year + 1900, gtm->tm_mon + 1, gtm->tm_mday);
return scratch;
}
const char *getTimeString(uint32_t timeStamp)
{
if (timeStamp == 0)
{
return "NEVER";
}
static char scratch[1024];
time_t t(timeStamp);
struct tm *gtm = gmtime(&t);
strftime(scratch, 1024, "%m/%d/%Y %H:%M:%S", gtm);
return scratch;
}
// This is a helper method to handle logging the output from scanning the blockchain
void logMessage(const char *fmt, ...)
{
static FILE *gLogFile = NULL;
char wbuff[2048];
va_list arg;
va_start(arg, fmt);
vsprintf(wbuff, fmt, arg);
va_end(arg);
printf("%s", wbuff);
if (gLogFile == NULL)
{
gLogFile = fopen("blockchain.txt", "wb");
}
if (gLogFile)
{
fprintf(gLogFile, "%s", wbuff);
fflush(gLogFile);
}
}
void printReverseHash(const uint8_t hash[32])
{
if (hash)
{
for (uint32_t i = 0; i < 32; i++)
{
logMessage("%02x", hash[31 - i]);
}
}
else
{
logMessage("NULL HASH");
}
}
void logBitcoinAddress(const uint8_t address[25])
{
char temp[512];
bitcoinAddressToAscii(address, temp, 512);
logMessage("%s", temp);
}
const char *getBitcoinAddressAscii(const uint8_t address[25])
{
static char temp[512];
bitcoinAddressToAscii(address, temp, 512);
return temp;
}
uint32_t getKey(void)
{
uint32_t ret = 0;
#ifdef _MSC_VER
ret = getch();
#endif
return ret;
}