16 #include <event2/util.h>
18 #include "dnscrypt_proxy.h"
23 logger_open_syslog(struct ProxyContext_
* const context
)
25 assert(context
->daemonize
!= 0);
27 openlog(PACKAGE_TARNAME
, LOG_NDELAY
| LOG_PID
, LOG_DAEMON
);
33 logger(struct ProxyContext_
* const context
,
34 const int crit
, const char * const format
, ...)
36 static char previous_line
[MAX_LOG_LINE
];
37 static time_t last_log_ts
= (time_t) 0;
38 static unsigned int burst_counter
= 0U;
39 char line
[MAX_LOG_LINE
];
42 time_t now
= time(NULL
);
46 if (context
!= NULL
) {
47 if (crit
> context
->max_log_level
) {
52 if (crit
> LOG_INFO
) {
62 urgency
= "[WARNING] ";
68 urgency
= "[NOTICE] ";
77 len
= (size_t) evutil_vsnprintf(line
, sizeof line
, format
, va
);
80 if (len
>= sizeof line
) {
81 assert(sizeof line
> (size_t) 0U);
82 len
= sizeof line
- (size_t) 1U;
86 if (context
!= NULL
&& context
->log_fd
== -1 && context
->daemonize
) {
87 syslog(crit
, "%s", line
);
91 if (memcmp(previous_line
, line
, len
) == 0) {
93 if (burst_counter
> LOGGER_ALLOWED_BURST_FOR_IDENTICAL_LOG_ENTRIES
&&
94 now
- last_log_ts
< LOGGER_DELAY_BETWEEN_IDENTICAL_LOG_ENTRIES
) {
101 assert(sizeof previous_line
>= sizeof line
);
102 memcpy(previous_line
, line
, len
);
103 if (context
== NULL
|| context
->log_fd
== -1) {
104 log_fd
= STDERR_FILENO
;
106 log_fd
= context
->log_fd
;
109 safe_write(log_fd
, urgency
, strlen(urgency
), LOG_WRITE_TIMEOUT
);
110 safe_write(log_fd
, line
, strlen(line
), LOG_WRITE_TIMEOUT
);
111 safe_write(log_fd
, "\n", (size_t) 1U, LOG_WRITE_TIMEOUT
);
114 printf("%s%s\n", urgency
, line
);
122 logger_noformat(struct ProxyContext_
* const context
,
123 const int crit
, const char * const msg
)
125 return logger(context
, crit
, "%s", msg
);
129 logger_error(struct ProxyContext_
* const context
,
130 const char * const msg
)
132 const char *const err_msg
= strerror(errno
);
134 return logger(context
, LOG_ERR
, "%s: %s", msg
, err_msg
);
138 logger_close(struct ProxyContext_
* const context
)
143 if (context
->daemonize
) {
146 if (context
->log_fd
!= -1) {
147 fsync(context
->log_fd
);
148 return close(context
->log_fd
);