-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathlog.c
98 lines (84 loc) · 2.21 KB
/
log.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
/*
* Copyright (c) 2003-2007 Andrea Luzzardi <[email protected]>
*
* This file is part of the pam_usb project. pam_usb is free software;
* you can redistribute it and/or modify it under the terms of the GNU General
* Public License version 2, as published by the Free Software Foundation.
*
* pam_usb 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., 59 Temple
* Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdio.h>
#include <unistd.h>
#include <syslog.h>
#include <stdarg.h>
#include "conf.h"
#include "log.h"
static t_pusb_options *pusb_opts = NULL;
static void pusb_log_syslog(int level, const char *format, va_list ap)
{
openlog("pam_usb", LOG_PID, LOG_AUTH);
vsyslog(level, format, ap);
closelog();
}
static void pusb_log_output(int level, const char *format, va_list ap)
{
if (!isatty(fileno(stdin)))
return ;
if (pusb_opts && !pusb_opts->quiet)
{
if (pusb_opts && pusb_opts->color_log)
{
if (level == LOG_ERR)
fprintf(stderr, "\033[01;31m*\033[00m ");
else if (level == LOG_NOTICE)
fprintf(stderr, "\033[01;32m*\033[00m ");
}
else
fprintf(stderr, "* ");
vfprintf(stderr, format, ap);
}
}
void __log_debug(const char *file, int line, const char *fmt, ...)
{
va_list ap;
if (!pusb_opts || !pusb_opts->debug)
return ;
fprintf(stderr, "[%s:%03d] ", file, line);
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
va_start(ap, fmt);
pusb_log_syslog(LOG_DEBUG, fmt, ap);
va_end(ap);
}
void log_error(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
pusb_log_syslog(LOG_ERR, fmt, ap);
va_end(ap);
va_start(ap, fmt);
pusb_log_output(LOG_ERR, fmt, ap);
va_end(ap);
}
void log_info(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
pusb_log_syslog(LOG_NOTICE, fmt, ap);
va_end(ap);
va_start(ap, fmt);
pusb_log_output(LOG_NOTICE, fmt, ap);
va_end(ap);
}
void pusb_log_init(t_pusb_options *opts)
{
pusb_opts = opts;
}