#include <pthread.h>
-void log_message(int level, const char *format, ...) {
+void log_message(int level, const char *file, int line, const char *format, ...) {
va_list ap;
const char *level_string;
}
switch (level) {
- case LOG_ERROR: level_string = "ERROR"; break;
- case LOG_WARNING: level_string = "WARN "; break;
- case LOG_DEBUG: level_string = "DEBUG"; break;
- default: level_string = "UNKNOWN";
+ case LOG_ERROR_LEVEL: level_string = "ERROR"; break;
+ case LOG_WARNING_LEVEL: level_string = "WARN "; break;
+ case LOG_DEBUG_LEVEL: level_string = "DEBUG"; break;
+ default: level_string = "UNKNOWN";
}
va_start(ap, format);
+#if DEBUG
+ fprintf(stdout, "%-12s:%-3d ", file, line);
+#else
+ (void)file;
+ (void)line;
+#endif
fprintf(stdout, "[%s] [%d] ", level_string, (int)pthread_self());
vfprintf(stdout, format, ap);
fprintf(stdout, "\n");
#ifndef LOG_H
#define LOG_H
+/* Log constants to be used with LOG() and LOG_PERROR() macros. */
+#define LOG_ERROR LOG_ERROR_LEVEL, __FILE__, __LINE__
+#define LOG_WARNING LOG_WARNING_LEVEL, __FILE__, __LINE__
+#define LOG_DEBUG LOG_DEBUG_LEVEL, __FILE__, __LINE__
+
/* Log level constants. */
-#define LOG_ERROR 0
-#define LOG_WARNING 1
-#define LOG_DEBUG 2
+#define LOG_ERROR_LEVEL 0
+#define LOG_WARNING_LEVEL 1
+#define LOG_DEBUG_LEVEL 2
-/* Helper macro for LOG/LOG_PERROR. Print file/line number if compiled with
- * debug output. */
-#ifdef DEBUG
-#define LOG_PRINT_LOCATION fprintf(stdout, "%-12s:%-3d ", __FILE__, __LINE__);
-#else
-#define LOG_PRINT_LOCATION
-#endif
/* Call log_message() and print current file and line number. */
#define LOG \
- LOG_PRINT_LOCATION \
log_message
/* perror() replacement with debug level support. */
#define LOG_PERROR(level, message) \
- LOG_PRINT_LOCATION \
log_message(level, "%s: %s", message, strerror(errno))
-void log_message(int level, const char *format, ...);
+void log_message(int level, const char *file, int line, const char *format, ...);
#endif
}
/* Fast rebinding for debug mode, could cause invalid packets. */
- if (LOG_DEBUG <= global_log_level) {
+ if (LOG_DEBUG_LEVEL <= global_log_level) {
int socket_option = 1;
setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR,
&socket_option, sizeof(socket_option));
return EXIT_FAILURE;
}
- if (LOG_DEBUG <= global_log_level) {
+ if (LOG_DEBUG_LEVEL <= global_log_level) {
printf("Listening for connections on port %d.\n", port);
if (NULL != global_proxy_host && NULL != global_proxy_port) {
/* Default values. */
thread_count = 10;
#ifdef DEBUG
- global_log_level = LOG_DEBUG;
+ global_log_level = LOG_DEBUG_LEVEL;
#else
- global_log_level = LOG_WARNING;
+ global_log_level = LOG_WARNING_LEVEL;
#endif
global_passthrough_unknown = 0;
/* Open the stored certificate file. */
*file = fopen(path, "rb");
if (NULL == *file) {
- LOG(global_passthrough_unknown ? LOG_DEBUG : LOG_WARNING,
- "server_certificate_path(): failed to open '%s': %s",
- path, strerror(errno));
+ if (global_passthrough_unknown) {
+ LOG(LOG_DEBUG,
+ "server_certificate_path(): failed to open '%s': %s",
+ path, strerror(errno));
+ } else {
+ LOG(LOG_WARNING,
+ "server_certificate_path(): failed to open '%s': %s",
+ path, strerror(errno));
+ }
/* Couldn't open the file, special case. */
return -2;
}