Skip to content
This repository was archived by the owner on Apr 24, 2022. It is now read-only.

Commit b5e1992

Browse files
SnowLeopard71chfast
authored andcommittedMay 1, 2018
Logging format option (#831)
When SYSLOG environment variable set, adopt NO_COLOR behaviour and also strip time from output. Make the output suitable for use a systemd service where the output is processed by journald. Without this option, logging would have double timestamp.
1 parent 2fd4c87 commit b5e1992

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed
 

‎ethminer/main.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ void help()
4444
<< " -h,--help Show this help message and exit." << endl
4545
<< " Environment variables:" << endl
4646
<< " NO_COLOR - set to any value to disable color output. Unset to re-enable color output." << endl
47+
<< " SYSLOG - set to any value to strip time and disable color from output, for logging under systemd" << endl
4748
;
4849
exit(0);
4950
}
@@ -63,6 +64,11 @@ int main(int argc, char** argv)
6364
setenv("GPU_MAX_ALLOC_PERCENT", "100");
6465
setenv("GPU_SINGLE_ALLOC_PERCENT", "100");
6566

67+
if (getenv("SYSLOG"))
68+
{
69+
g_syslog = true;
70+
g_useColor = false;
71+
}
6672
if (getenv("NO_COLOR"))
6773
g_useColor = false;
6874
#if defined(_WIN32)

‎libdevcore/Log.cpp

+12-5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ using namespace dev;
3232
// Logging
3333
int dev::g_logVerbosity = 5;
3434
bool dev::g_useColor = true;
35+
bool dev::g_syslog = false;
3536

3637
mutex x_logOverride;
3738

@@ -66,15 +67,21 @@ LogOutputStreamBase::LogOutputStreamBase(char const* _id, std::type_info const*
6667
auto it = s_logOverride.find(_info);
6768
if ((it != s_logOverride.end() && it->second) || it == s_logOverride.end())
6869
{
69-
time_t rawTime = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
70-
char buf[24];
71-
if (strftime(buf, 24, "%X", localtime(&rawTime)) == 0)
72-
buf[0] = '\0'; // empty if case strftime fails
7370
static char const* c_begin = " " EthViolet;
7471
static char const* c_sep1 = EthReset EthBlack "|" EthNavy;
7572
static char const* c_sep2 = EthReset EthBlack "|" EthTeal;
7673
static char const* c_end = EthReset " ";
77-
m_sstr << _id << c_begin << buf << c_sep1 << std::left << std::setw(8) << getThreadName() << c_sep2 << c_end;
74+
if (g_syslog)
75+
{
76+
c_begin = " " EthNavy;
77+
m_sstr << c_begin << std::left << std::setw(8) << getThreadName() << c_sep2 << c_end;
78+
} else {
79+
time_t rawTime = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
80+
char buf[24];
81+
if (strftime(buf, 24, "%X", localtime(&rawTime)) == 0)
82+
buf[0] = '\0'; // empty if case strftime fails
83+
m_sstr << _id << c_begin << buf << c_sep1 << std::left << std::setw(8) << getThreadName() << c_sep2 << c_end;
84+
}
7885
}
7986
}
8087
}

‎libdevcore/Log.h

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ void simpleDebugOut(std::string const&);
4949
/// The logging system's current verbosity.
5050
extern int g_logVerbosity;
5151
extern bool g_useColor;
52+
extern bool g_syslog;
5253

5354
class ThreadContext
5455
{

0 commit comments

Comments
 (0)
This repository has been archived.