forked from ethereum-mining/ethminer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Log.cpp
134 lines (116 loc) · 3.37 KB
/
Log.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
/*
This file is part of ethminer.
ethminer is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ethminer is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with ethminer. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Log.h"
#include <map>
#include <thread>
#ifdef __APPLE__
#include <pthread.h>
#endif
#include "Guards.h"
using namespace std;
using namespace dev;
//⊳⊲◀▶■▣▢□▷◁▧▨▩▲◆◉◈◇◎●◍◌○◼☑☒☎☢☣☰☀♽♥♠✩✭❓✔✓✖✕✘✓✔✅⚒⚡⦸⬌∅⁕«««»»»⚙
// Logging
unsigned g_logOptions = 0;
bool g_logNoColor = false;
bool g_logSyslog = false;
bool g_logStdout = false;
const char* LogChannel::name()
{
return EthGray "..";
}
const char* WarnChannel::name()
{
return EthRed " X";
}
const char* NoteChannel::name()
{
return EthBlue " i";
}
LogOutputStreamBase::LogOutputStreamBase(char const* _id)
{
static std::locale logLocl = std::locale("");
m_sstr.imbue(logLocl);
if (g_logSyslog)
m_sstr << std::left << std::setw(8) << getThreadName() << " " EthReset;
else
{
time_t rawTime = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
char buf[24];
if (strftime(buf, 24, "%X", localtime(&rawTime)) == 0)
buf[0] = '\0'; // empty if case strftime fails
m_sstr << _id << " " EthViolet << buf << " " EthBlue << std::left << std::setw(8)
<< getThreadName() << " " EthReset;
}
}
/// Associate a name with each thread for nice logging.
struct ThreadLocalLogName
{
ThreadLocalLogName(char const* _name) { name = _name; }
thread_local static char const* name;
};
thread_local char const* ThreadLocalLogName::name;
ThreadLocalLogName g_logThreadName("main");
string dev::getThreadName()
{
#if defined(__linux__) || defined(__APPLE__)
char buffer[128];
pthread_getname_np(pthread_self(), buffer, 127);
buffer[127] = 0;
return buffer;
#else
return ThreadLocalLogName::name ? ThreadLocalLogName::name : "<unknown>";
#endif
}
void dev::setThreadName(char const* _n)
{
#if defined(__linux__)
pthread_setname_np(pthread_self(), _n);
#elif defined(__APPLE__)
pthread_setname_np(_n);
#else
ThreadLocalLogName::name = _n;
#endif
}
void dev::simpleDebugOut(std::string const& _s)
{
try
{
std::ostream& os = g_logStdout ? std::cout : std::clog;
if (!g_logNoColor)
{
os << _s + '\n';
os.flush();
return;
}
bool skip = false;
std::stringstream ss;
for (auto it : _s)
{
if (!skip && it == '\x1b')
skip = true;
else if (skip && it == 'm')
skip = false;
else if (!skip)
ss << it;
}
ss << '\n';
os << ss.str();
os.flush();
}
catch (...)
{
return;
}
}