[29] | 1 | ///////////////////////////////////////////////////////////////////////////// |
---|
| 2 | // Name: simplelog.h |
---|
| 3 | // Purpose: simple logger |
---|
| 4 | // Author: Cesar Mauri Loba (cesar at crea-si dot com) |
---|
| 5 | // Copyright: (C) 2012-13 Cesar Mauri Loba - CREA Software Systems |
---|
| 6 | // |
---|
| 7 | // This program is free software: you can redistribute it and/or modify |
---|
| 8 | // it under the terms of the GNU General Public License as published by |
---|
| 9 | // the Free Software Foundation, either version 3 of the License, or |
---|
| 10 | // (at your option) any later version. |
---|
| 11 | // |
---|
| 12 | // This program is distributed in the hope that it will be useful, |
---|
| 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | // GNU General Public License for more details. |
---|
| 16 | // |
---|
| 17 | // You should have received a copy of the GNU General Public License |
---|
| 18 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
| 19 | ///////////////////////////////////////////////////////////////////////////// |
---|
| 20 | #ifndef SIMPLELOG_H |
---|
| 21 | #define SIMPLELOG_H |
---|
| 22 | |
---|
| 23 | #include <stdio.h> |
---|
| 24 | #include <stdarg.h> |
---|
| 25 | |
---|
| 26 | #if defined (__cplusplus) |
---|
| 27 | extern "C" { |
---|
| 28 | #endif /* __cplusplus */ |
---|
| 29 | |
---|
| 30 | /** |
---|
| 31 | * @def SLOG_DECLSPEC |
---|
| 32 | * Some compilers use a special export keyword |
---|
| 33 | */ |
---|
| 34 | #ifndef SLOG_DECLSPEC |
---|
| 35 | # if defined(WIN32) |
---|
| 36 | # define SLOG_DECLSPEC |
---|
| 37 | # else |
---|
| 38 | # define SLOG_DECLSPEC |
---|
| 39 | # endif |
---|
| 40 | #endif |
---|
| 41 | |
---|
| 42 | /** |
---|
| 43 | * @brief SLOG priority |
---|
| 44 | */ |
---|
| 45 | typedef enum { |
---|
| 46 | |
---|
| 47 | /** EMERGENCY level */ |
---|
| 48 | SLOG_PRIO_EMERG=0, |
---|
| 49 | |
---|
| 50 | /** ALERT level */ |
---|
| 51 | SLOG_PRIO_ALERT=1, |
---|
| 52 | |
---|
| 53 | /** CRITICAL level */ |
---|
| 54 | SLOG_PRIO_CRIT=2, |
---|
| 55 | |
---|
| 56 | /** ERROR level */ |
---|
| 57 | SLOG_PRIO_ERR=3, |
---|
| 58 | |
---|
| 59 | /** WARNING level */ |
---|
| 60 | SLOG_PRIO_WARNING=4, |
---|
| 61 | |
---|
| 62 | /** NOTICE level */ |
---|
| 63 | SLOG_PRIO_NOTICE=5, |
---|
| 64 | |
---|
| 65 | /** INFORMATION level */ |
---|
| 66 | SLOG_PRIO_INFO=6, |
---|
| 67 | |
---|
| 68 | /** DEBUG level */ |
---|
| 69 | SLOG_PRIO_DEBUG=7 |
---|
| 70 | |
---|
| 71 | } slog_priority_t; |
---|
| 72 | |
---|
| 73 | /** |
---|
| 74 | * @brief Log callback function type |
---|
| 75 | **/ |
---|
| 76 | typedef void (*slog_callback_t)(slog_priority_t, const char*, va_list); |
---|
| 77 | |
---|
| 78 | /** |
---|
| 79 | * @brief Get priority name |
---|
| 80 | */ |
---|
| 81 | SLOG_DECLSPEC const char* slog_get_priority_name (slog_priority_t prio); |
---|
| 82 | |
---|
| 83 | /** |
---|
| 84 | * @brief Set log callback |
---|
| 85 | * |
---|
| 86 | * If not set, slog_stream_callback is used |
---|
| 87 | * |
---|
| 88 | * @param c. Pointer the callback function. If NULL, all logging is supressed. |
---|
| 89 | */ |
---|
| 90 | void SLOG_DECLSPEC slog_set_callback (slog_callback_t c); |
---|
| 91 | |
---|
| 92 | /** |
---|
| 93 | * @brief Set log priority |
---|
| 94 | * |
---|
| 95 | * Default priority is SLOG_PRIO_ERR |
---|
| 96 | * |
---|
| 97 | * @param pio priority of the message to log. |
---|
| 98 | * @see slog_priority_t |
---|
| 99 | */ |
---|
| 100 | void SLOG_DECLSPEC slog_set_priority (slog_priority_t prio); |
---|
| 101 | |
---|
| 102 | /** |
---|
| 103 | * @brief Get log priority |
---|
| 104 | * |
---|
| 105 | * @see slog_priority_t |
---|
| 106 | */ |
---|
| 107 | slog_priority_t SLOG_DECLSPEC slog_get_priority (); |
---|
| 108 | |
---|
| 109 | /** |
---|
| 110 | * @brief Log a message to output or file |
---|
| 111 | * |
---|
| 112 | * The message fmt must be used like printf(). |
---|
| 113 | * |
---|
| 114 | * @param pio priority of the message to log. |
---|
| 115 | * @param fmt message to log. |
---|
| 116 | * @param ... Variables to include into the message. |
---|
| 117 | */ |
---|
| 118 | void SLOG_DECLSPEC slog_write (slog_priority_t prio, const char* fmt, ...) |
---|
| 119 | #ifdef __GNUC__ |
---|
| 120 | __attribute__ ((format (printf, 2, 3))) // Compile time format check |
---|
| 121 | #endif |
---|
| 122 | ; |
---|
| 123 | |
---|
| 124 | /** @name log macros |
---|
| 125 | */ |
---|
| 126 | /*@{*/ |
---|
| 127 | #define SLOG_EMERG(fmt,...) slog_write(SLOG_PRIO_EMERG, fmt, ## __VA_ARGS__) |
---|
| 128 | #define SLOG_ALERT(fmt,...) slog_write(SLOG_PRIO_ALERT, fmt, ## __VA_ARGS__) |
---|
| 129 | #define SLOG_CRIT(fmt,...) slog_write(SLOG_PRIO_CRIT, fmt, ## __VA_ARGS__) |
---|
| 130 | #define SLOG_ERR(fmt,...) slog_write(SLOG_PRIO_ERR, fmt, ## __VA_ARGS__) |
---|
| 131 | #define SLOG_WARNING(fmt,...) slog_write(SLOG_PRIO_WARNING, fmt, ## __VA_ARGS__) |
---|
| 132 | #define SLOG_NOTICE(fmt,...) slog_write(SLOG_PRIO_NOTICE, fmt, ## __VA_ARGS__) |
---|
| 133 | #define SLOG_INFO(fmt,...) slog_write(SLOG_PRIO_INFO, fmt, ## __VA_ARGS__) |
---|
| 134 | #define SLOG_DEBUG(fmt,...) slog_write(SLOG_PRIO_DEBUG, fmt, ## __VA_ARGS__) |
---|
| 135 | /*@}*/ |
---|
| 136 | |
---|
| 137 | /** |
---|
| 138 | * @name stream based log callback funtions |
---|
| 139 | */ |
---|
| 140 | /*@{*/ |
---|
| 141 | /** |
---|
| 142 | * @brief Stream based log callback function. |
---|
| 143 | * |
---|
| 144 | * Send the message to a stream (stderr by default) |
---|
| 145 | * The message will be logged only if (prio<= current log level) |
---|
| 146 | * see slog_stream_callback function |
---|
| 147 | * |
---|
| 148 | * To be used as parameter for slog_set_callback and NOT to be |
---|
| 149 | * called directly. |
---|
| 150 | */ |
---|
| 151 | void SLOG_DECLSPEC slog_stream_callback (slog_priority_t, const char*, va_list); |
---|
| 152 | |
---|
| 153 | /** |
---|
| 154 | * @brief Set the logging priority for the stream based log callback function. |
---|
| 155 | * |
---|
| 156 | * @param stream. If NULL defaults to stderr |
---|
| 157 | */ |
---|
| 158 | void SLOG_DECLSPEC slog_stream_set_stream (FILE *stream); |
---|
| 159 | /*@}*/ |
---|
| 160 | |
---|
| 161 | #if defined (__cplusplus) |
---|
| 162 | } |
---|
| 163 | #endif /* __cplusplus */ |
---|
| 164 | |
---|
| 165 | #endif /* SIMPLELOG_H */ |
---|