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
);
47 if (crit
== LOG_DEBUG
) {
56 urgency
= "[WARNING] ";
62 urgency
= "[NOTICE] ";
71 len
= (size_t) evutil_vsnprintf(line
, sizeof line
, format
, va
);
74 if (len
>= sizeof line
) {
75 assert(sizeof line
> (size_t) 0U);
76 len
= sizeof line
- (size_t) 1U;
80 if (context
!= NULL
&& context
->log_fd
== -1 && context
->daemonize
) {
81 syslog(crit
, "%s", line
);
85 if (memcmp(previous_line
, line
, len
) == 0) {
87 if (burst_counter
> LOGGER_ALLOWED_BURST_FOR_IDENTICAL_LOG_ENTRIES
&&
88 now
- last_log_ts
< LOGGER_DELAY_BETWEEN_IDENTICAL_LOG_ENTRIES
) {
95 assert(sizeof previous_line
>= sizeof line
);
96 memcpy(previous_line
, line
, len
);
97 if (context
== NULL
|| context
->log_fd
== -1) {
98 log_fd
= STDERR_FILENO
;
100 log_fd
= context
->log_fd
;
103 safe_write(log_fd
, urgency
, strlen(urgency
), LOG_WRITE_TIMEOUT
);
104 safe_write(log_fd
, line
, strlen(line
), LOG_WRITE_TIMEOUT
);
105 safe_write(log_fd
, "\n", (size_t) 1U, LOG_WRITE_TIMEOUT
);
108 printf("%s%s\n", urgency
, line
);
116 logger_noformat(struct ProxyContext_
* const context
,
117 const int crit
, const char * const msg
)
119 return logger(context
, crit
, "%s", msg
);
123 logger_error(struct ProxyContext_
* const context
,
124 const char * const msg
)
126 const char *const err_msg
= strerror(errno
);
128 return logger(context
, LOG_ERR
, "%s: %s", msg
, err_msg
);
132 logger_close(struct ProxyContext_
* const context
)
137 if (context
->daemonize
) {
140 if (context
->log_fd
!= -1) {
141 fsync(context
->log_fd
);
142 return close(context
->log_fd
);