forked from LINBIT/csync2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.c
148 lines (121 loc) · 3.47 KB
/
error.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
/*
* csync2 - cluster synchronization tool, 2nd generation
* Copyright (C) 2004 - 2015 LINBIT Information Technologies GmbH
* http://www.linbit.com; see also AUTHORS
*
* 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "csync2.h"
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <unistd.h>
#include <syslog.h>
long csync_last_printtime = 0;
FILE *csync_timestamp_out = 0;
int csync_messages_printed = 0;
time_t csync_startup_time = 0;
void csync_printtime()
{
if (csync_timestamps || csync_timestamp_out)
{
time_t now = time(0);
char ftbuffer[128];
if (!csync_startup_time)
csync_startup_time = now;
if (csync_last_printtime+300 < now) {
csync_last_printtime = now;
strftime(ftbuffer, 128, "%Y-%m-%d %H:%M:%S %Z (GMT%z)", localtime(&now));
if (csync_timestamp_out)
fprintf(csync_timestamp_out, "<%d> TIMESTAMP: %s\n", (int)getpid(), ftbuffer);
if (csync_timestamps) {
if (csync_server_child_pid)
fprintf(csync_debug_out, "<%d> ", csync_server_child_pid);
fprintf(csync_debug_out, "TIMESTAMP: %s\n", ftbuffer);
}
}
}
}
void csync_printtotaltime()
{
if (csync_timestamps || csync_timestamp_out)
{
time_t now = time(0);
int seconds = now - csync_startup_time;
csync_last_printtime = 0;
csync_printtime();
if (csync_timestamp_out)
fprintf(csync_timestamp_out, "<%d> TOTALTIME: %d:%02d:%02d\n",
(int)getpid(), seconds / (60*60), (seconds/60) % 60, seconds % 60);
if (csync_timestamps) {
if (csync_server_child_pid)
fprintf(csync_debug_out, "<%d> ", csync_server_child_pid);
fprintf(csync_debug_out, "TOTALTIME: %d:%02d:%02d\n",
seconds / (60*60), (seconds/60) % 60, seconds % 60);
}
}
}
void csync_printtime_prefix()
{
time_t now = time(0);
char ftbuffer[32];
strftime(ftbuffer, 32, "%H:%M:%S", localtime(&now));
fprintf(csync_debug_out, "[%s] ", ftbuffer);
}
static int csync_log_level_to_sys_log_level(int lv)
{
return (lv < 0) ? LOG_ERR :
(lv == 0) ? LOG_WARNING :
(lv == 1) ? LOG_INFO :
/* lv >= 2 */ LOG_DEBUG;
}
void csync_vdebug(int lv, const char *fmt, va_list ap)
{
if (csync_debug_level < lv)
return;
if (!csync_syslog) {
csync_printtime();
if (csync_timestamps)
csync_printtime_prefix();
if (csync_server_child_pid)
fprintf(csync_debug_out, "<%d> ",
csync_server_child_pid);
vfprintf(csync_debug_out, fmt, ap);
} else {
vsyslog(csync_log_level_to_sys_log_level(lv), fmt, ap);
}
csync_messages_printed++;
}
void csync_fatal(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
csync_vdebug(0, fmt, ap);
va_end(ap);
csync_db_close();
csync_last_printtime = 0;
csync_printtime();
exit(1);
}
void csync_debug(int lv, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
csync_vdebug(lv, fmt, ap);
va_end(ap);
}