-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.c
136 lines (120 loc) · 3.12 KB
/
util.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
/*
* util.c
*
* Copyright (c) 2015-2017, Parallels International GmbH
* Copyright (c) 2017-2019 Virtuozzo International GmbH. All rights reserved.
*
* This file is part of OpenVZ. OpenVZ 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.
*
* Our contact details: Virtuozzo International GmbH, Vordergasse 59, 8200
* Schaffhausen, Switzerland.
*/
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>
#include <stdarg.h>
#include <error.h>
#include <time.h>
#include <rfb/rfb.h>
#include "util.h"
static int loglevel = VZ_VNC_INFO;
static char *logfile = NULL;
void init_logger(const char * log_file, int log_level, int is_verbose)
{
rfbLogEnable(is_verbose);
if (logfile)
free(logfile);
logfile = strdup(log_file);
loglevel = log_level;
if (!is_verbose) {
freopen("/dev/null", "w", stdout);
freopen("/dev/null", "w", stderr);
}
}
int get_loglevel()
{
return loglevel;
}
static char *get_date()
{
struct tm *p_tm_time;
time_t ptime;
char str[100];
*str = 0;
ptime = time(NULL);
p_tm_time = localtime(&ptime);
strftime(str, sizeof(str), "%Y-%m-%dT%T%z", p_tm_time);
return strdup(str);
}
static void write_log_rec(int log_level, const char *format, va_list ap)
{
/* Put log message in log file and debug messages with
* level <= DEBUG_LEVEL to stderr
* Log message format: date script : message :
* err_message(based on errno)
* date script : message : err_message(based on errno)
* errno passed in the function as
*/
FILE *log_file;
FILE *out;
char *p;
if (loglevel < log_level)
return;
if ((log_level == VZ_VNC_ERR) || (log_level == VZ_VNC_WARN))
out = stderr;
else
out = stdout;
p = get_date();
/* Print formatted message */
va_list ap_save;
va_copy(ap_save, ap);
if (log_level == 0)
fprintf(out, "Error: ");
vfprintf(out, format, ap_save);
va_end(ap_save);
fprintf(out, "\n");
if (logfile) {
if ((log_file = fopen(logfile, "a"))) {
fprintf(log_file, "%s : ", p);
if (log_level == 0)
fprintf(log_file, "Error: ");
vfprintf(log_file, format, ap);
fprintf(log_file, "\n");
fclose(log_file);
}
}
fflush(out);
if (p != NULL) free(p);
}
void vzvnc_logger(int log_level, const char * format, ...)
{
va_list ap;
va_start(ap, format);
write_log_rec(log_level, format, ap);
va_end(ap);
}
int vzvnc_error(int err_code, const char * format, ...)
{
va_list ap;
va_start(ap, format);
write_log_rec(VZ_VNC_ERR, format, ap);
va_end(ap);
return err_code;
}