123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- #ifndef ASMSH_LOGGER_H
- #define ASMSH_LOGGER_H
- #include "config.h"
- /** A logger suitable for a shell
- *
- * Collect messages and deliver them when needed
- */
-
- #include <errno.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
-
- ///! TODO varargs macro & log functions, perror etc.
- #define asmsh_log_trace(msg) \
- asmsh_log(_default_logger, ASMSH_TRACE, __FUNCTION__, msg)
- #define asmsh_log_debug(msg) \
- asmsh_log(_default_logger, ASMSH_DEBUG, __FUNCTION__, msg)
- #define asmsh_log_info(msg) \
- asmsh_log(_default_logger, ASMSH_INFO, __FUNCTION__, msg)
- #define asmsh_log_warning(msg) \
- asmsh_log(_default_logger, ASMSH_WARN, __FUNCTION__, msg)
- #define asmsh_log_error(msg) \
- asmsh_log(_default_logger, ASMSH_ERR, __FUNCTION__, msg)
- #define asmsh_log_fatal(msg) \
- asmsh_log(_default_logger, ASMSH_FATAL, __FUNCTION__, msg)
-
- #define ASMSH_LOG_BUFFER_ALLOC 4096
-
- typedef struct asmsh_logger_s asmsh_logger_t;
- typedef struct asmsh_log_msg_s asmsh_log_msg_t;
- typedef enum asmsh_loglevel_e asmsh_loglevel_t;
-
- extern asmsh_logger_t *_default_logger;
-
-
- enum asmsh_loglevel_e
- {
- ASMSH_TRACE = 0,
- ASMSH_DEBUG = 10,
- ASMSH_INFO = 20,
- ASMSH_WARN = 30,
- ASMSH_ERR = 40,
- ASMSH_FATAL = 50
- };
-
-
- struct asmsh_log_msg_s
- {
- asmsh_loglevel_t level;
- time_t timestamp;
- char *caller;
- char *msg;
- asmsh_log_msg_t *nxt;
- };
-
-
- struct asmsh_logger_s
- {
- asmsh_loglevel_t min_level;
- /** Buffered messages
- *
- * A single buffer is used to store asmsh_log_msg_t and the
- * associated strings (caller & msg)
- * msgs will be a sequence of [asmsh_log_msg_t][char*(caller)][char*(msg)]...
- */
- void *msgs;
- /** Point on the address of the next message */
- asmsh_log_msg_t *nxt;
- /** Memory used by stored messages */
- size_t msgs_sz;
- /** Allocated buffer for messages */
- size_t msgs_alloc;
- };
-
-
- static inline int asmsh_logger_msg_islast(asmsh_logger_t *logger, asmsh_log_msg_t *msg) {
- return ((void*)msg) == ((void*)logger->nxt);
- }
-
-
- static inline int asmsh_logger_empty(asmsh_logger_t *logger) {
- return asmsh_logger_msg_islast(logger, (asmsh_log_msg_t*)logger->msgs);
- }
-
-
- /** If null use default min_level of WARN
- * @param asmsh_logger_t* Optionnal logger to set
- * @return 0 if ok else -1
- */
- int asmsh_logger_setup(asmsh_logger_t *logger);
-
- asmsh_logger_t* asmsh_logger_new(asmsh_loglevel_t min_level);
-
- void asmsh_logger_free(asmsh_logger_t* logger);
-
- int asmsh_logger_dprint(int fd, asmsh_logger_t *logger);
-
- /// Collect & format messages before printing them on stderr
- static inline int asmsh_logger_stderr(asmsh_logger_t *logger) {
- return asmsh_logger_dprint(2, logger);
- }
-
-
- int asmsh_log(asmsh_logger_t *logger, asmsh_loglevel_t lvl, const char caller[], const char *msg);
-
-
- const char * asmsh_loglevel_name(asmsh_loglevel_t lvl);
-
- int asmsh_log_default_fmt(asmsh_log_msg_t *msg, char *res, int sz);
-
- #endif
|