123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- /*
- * log.h
- *
- * Copyright (C) 2018 Aleksandar Andrejevic <theflash@sdf.lonestar.org>
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- #ifndef _LOG_H_
- #define _LOG_H_
- #include <common.h>
- #include <stdarg.h>
- #include <stdio.h>
- #define MAX_LOG_MESSAGE_SIZE 256
- #ifdef DEBUG
- #define TRACE(x) log_write(LOG_DEBUG, x)
- #define FTRACE(x, args...) log_write(LOG_DEBUG, x, args)
- #else
- #define TRACE(x)
- #define FTRACE(x, ...)
- #endif
- typedef enum
- {
- LOG_DEBUG,
- LOG_NORMAL,
- LOG_WARNING,
- LOG_ERROR,
- LOG_CRITICAL,
- } log_level_t;
- extern char *debug_channel;
- extern log_level_t debug_min_level;
- void append_log_entry(const char *source, log_level_t level, const char *message);
- static inline void log_write(log_level_t level, const char *format, ...)
- {
- extern const char driver_name[];
- static char log_buffer[MAX_LOG_MESSAGE_SIZE] = "";
- va_list ap;
- va_start(ap, format);
- char message[MAX_LOG_MESSAGE_SIZE];
- vsnprintf(message, MAX_LOG_MESSAGE_SIZE, format, ap);
- va_end(ap);
- char *ptr = message;
- while (*ptr)
- {
- char *end = strchr(ptr, '\n');
- if (end) *end = '\0';
- else break;
- char full_message[2 * MAX_LOG_MESSAGE_SIZE];
- strcpy(full_message, log_buffer);
- strcat(full_message, ptr);
- append_log_entry(driver_name, level, full_message);
- *log_buffer = '\0';
- ptr = end + 1;
- }
- strcat(log_buffer, ptr);
- }
- #endif
|