A shell that runs x86_64 assembly
c
x86-64
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

logger.h 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #ifndef ASMSH_LOGGER_H
  2. #define ASMSH_LOGGER_H
  3. #include "config.h"
  4. /** A logger suitable for a shell
  5. *
  6. * Collect messages and deliver them when needed
  7. */
  8. #include <errno.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <time.h>
  13. ///! TODO varargs macro & log functions, perror etc.
  14. #define asmsh_log_trace(msg) \
  15. asmsh_log(_default_logger, ASMSH_TRACE, __FUNCTION__, msg)
  16. #define asmsh_log_debug(msg) \
  17. asmsh_log(_default_logger, ASMSH_DEBUG, __FUNCTION__, msg)
  18. #define asmsh_log_info(msg) \
  19. asmsh_log(_default_logger, ASMSH_INFO, __FUNCTION__, msg)
  20. #define asmsh_log_warning(msg) \
  21. asmsh_log(_default_logger, ASMSH_WARN, __FUNCTION__, msg)
  22. #define asmsh_log_error(msg) \
  23. asmsh_log(_default_logger, ASMSH_ERR, __FUNCTION__, msg)
  24. #define asmsh_log_fatal(msg) \
  25. asmsh_log(_default_logger, ASMSH_FATAL, __FUNCTION__, msg)
  26. #define ASMSH_LOG_BUFFER_ALLOC 4096
  27. typedef struct asmsh_logger_s asmsh_logger_t;
  28. typedef struct asmsh_log_msg_s asmsh_log_msg_t;
  29. typedef enum asmsh_loglevel_e asmsh_loglevel_t;
  30. extern asmsh_logger_t *_default_logger;
  31. enum asmsh_loglevel_e
  32. {
  33. ASMSH_TRACE = 0,
  34. ASMSH_DEBUG = 10,
  35. ASMSH_INFO = 20,
  36. ASMSH_WARN = 30,
  37. ASMSH_ERR = 40,
  38. ASMSH_FATAL = 50
  39. };
  40. struct asmsh_log_msg_s
  41. {
  42. asmsh_loglevel_t level;
  43. time_t timestamp;
  44. char *caller;
  45. char *msg;
  46. asmsh_log_msg_t *nxt;
  47. };
  48. struct asmsh_logger_s
  49. {
  50. asmsh_loglevel_t min_level;
  51. /** Buffered messages
  52. *
  53. * A single buffer is used to store asmsh_log_msg_t and the
  54. * associated strings (caller & msg)
  55. * msgs will be a sequence of [asmsh_log_msg_t][char*(caller)][char*(msg)]...
  56. */
  57. void *msgs;
  58. /** Point on the address of the next message */
  59. asmsh_log_msg_t *nxt;
  60. /** Memory used by stored messages */
  61. size_t msgs_sz;
  62. /** Allocated buffer for messages */
  63. size_t msgs_alloc;
  64. };
  65. static inline int asmsh_logger_msg_islast(asmsh_logger_t *logger, asmsh_log_msg_t *msg) {
  66. return ((void*)msg) == ((void*)logger->nxt);
  67. }
  68. static inline int asmsh_logger_empty(asmsh_logger_t *logger) {
  69. return asmsh_logger_msg_islast(logger, (asmsh_log_msg_t*)logger->msgs);
  70. }
  71. /** If null use default min_level of WARN
  72. * @param asmsh_logger_t* Optionnal logger to set
  73. * @return 0 if ok else -1
  74. */
  75. int asmsh_logger_setup(asmsh_logger_t *logger);
  76. asmsh_logger_t* asmsh_logger_new(asmsh_loglevel_t min_level);
  77. void asmsh_logger_free(asmsh_logger_t* logger);
  78. int asmsh_logger_dprint(int fd, asmsh_logger_t *logger);
  79. /// Collect & format messages before printing them on stderr
  80. static inline int asmsh_logger_stderr(asmsh_logger_t *logger) {
  81. return asmsh_logger_dprint(2, logger);
  82. }
  83. int asmsh_log(asmsh_logger_t *logger, asmsh_loglevel_t lvl, const char caller[], const char *msg);
  84. const char * asmsh_loglevel_name(asmsh_loglevel_t lvl);
  85. int asmsh_log_default_fmt(asmsh_log_msg_t *msg, char *res, int sz);
  86. #endif