-
Notifications
You must be signed in to change notification settings - Fork 13
/
logging.c
165 lines (143 loc) · 4.13 KB
/
logging.c
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* Copyright 2016-2017 Fazio Bai <[email protected]>
* Copyright 2016-2017 Clement Duan <[email protected]>
* Copyright 2016 Miguel Padilla
* Copyright 2011-2012 Con Kolivas
* Copyright 2013 Andrew Smith
*
* This program 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. See COPYING for more details.
*/
#include "config.h"
#include <unistd.h>
#include "logging.h"
#include "miner.h"
bool opt_debug = false;
bool opt_log_output = false;
/* per default priorities higher than LOG_NOTICE are logged */
int opt_log_level = LOG_NOTICE;
FILE * g_log_file = NULL;
bool g_logfile_enable = false;
char g_logfile_path[256] = {0};
char g_logfile_openflag[32] = {0};
static void my_log_curses(int prio, const char *datetime, const char *str, bool force)
{
if (opt_quiet && prio != LOG_ERR)
{
return;
}
/* Mutex could be locked by dead thread on shutdown so forcelog will
* invalidate any console lock status. */
if (force)
{
mutex_trylock(&console_lock);
mutex_unlock(&console_lock);
}
#ifdef HAVE_CURSES
extern bool use_curses;
if (use_curses && log_curses_only(prio, datetime, str))
;
else
#endif
{
mutex_lock(&console_lock);
printf("%s%s%s", datetime, str, " \n");
mutex_unlock(&console_lock);
}
}
/* high-level logging function, based on global opt_log_level */
/*
* log function
*/
void _applog(int prio, const char *str, bool force)
{
#ifdef HAVE_SYSLOG_H
if (use_syslog)
{
syslog(LOG_LOCAL0 | prio, "%s", str);
}
#else
if (0) {} //for what do we need this?
#endif
else
{
char datetime[64];
struct timeval tv = {0, 0};
struct tm *tm;
cgtime(&tv);
const time_t tmp_time = tv.tv_sec;
int ms = (int)(tv.tv_usec / 1000);
tm = localtime(&tmp_time);
snprintf(datetime, sizeof(datetime), " [%d-%02d-%02d %02d:%02d:%02d.%03d] ",
tm->tm_year + 1900,
tm->tm_mon + 1,
tm->tm_mday,
tm->tm_hour,
tm->tm_min,
tm->tm_sec, ms);
/* Only output to stderr if it's not going to the screen as well */
if (!isatty(fileno((FILE *)stderr)))
{
fprintf(stderr, "%s%s\n", datetime, str); /* atomic write to stderr */
fflush(stderr);
}
if(g_logfile_enable)
{
if(!g_log_file)
{
g_log_file = fopen(g_logfile_path, g_logfile_openflag);
}
if(g_log_file)
{
fwrite(datetime, strlen(datetime), 1, g_log_file);
fwrite(str, strlen(str), 1, g_log_file);
fwrite("\n", 1, 1, g_log_file);
fflush(g_log_file);
}
}
my_log_curses(prio, datetime, str, force);
}
}
void _simplelog(int prio, const char *str, bool force)
{
#ifdef HAVE_SYSLOG_H
if (use_syslog)
{
syslog(LOG_LOCAL0 | prio, "%s", str);
}
#else
if (0) {}
#endif
else
{
/* Only output to stderr if it's not going to the screen as well */
if (!isatty(fileno((FILE *)stderr)))
{
fprintf(stderr, "%s\n", str); /* atomic write to stderr */
fflush(stderr);
}
my_log_curses(prio, "", str, force);
}
}
void save_last_quit(int status, const char *str)
{
FILE *fw;
/* Do not save reasons for exit with exit status 0.
* This is to not confuse user with messages that look like they mean
* something but they don't, ie.:
* "Last quit reason: Shutdown signal received."
* that reminiscences famous "Error: Success" error.
* The question is, should we also delete the quit file if we exit
* on request, not on error?
*/
if (status == 0)
return;
fw = fopen(SAVE_LAST_QUIT_FILE, "w");
if (fw == NULL)
return;
fputs(str, fw);
fputc('\n', fw);
fclose(fw);
}