A shell that runs x86_64 assembly
c
x86-64
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

logger.h 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* Copyright Yann Weber <asmsh@yannweb.net>
  2. This file is part of asmsh.
  3. asmsh is free software: you can redistribute it and/or modify it under the
  4. terms of the GNU General Public License as published by the Free Software
  5. Foundation, either version 3 of the License, or any later version.
  6. asmsh is distributed in the hope that it will be useful, but WITHOUT ANY
  7. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  9. details.
  10. You should have received a copy of the GNU General Public License along
  11. with asmsh. If not, see <https://www.gnu.org/licenses/>.
  12. */
  13. #ifndef ASMSH_LOGGER_H
  14. #define ASMSH_LOGGER_H
  15. #include "config.h"
  16. /** A logger suitable for a shell
  17. *
  18. * Collect messages and deliver them when needed
  19. */
  20. #include <errno.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <time.h>
  25. ///! TODO varargs macro & log functions, perror etc.
  26. #define asmsh_log_trace(msg) \
  27. asmsh_log(_default_logger, ASMSH_TRACE, __FUNCTION__, msg)
  28. #define asmsh_log_debug(msg) \
  29. asmsh_log(_default_logger, ASMSH_DEBUG, __FUNCTION__, msg)
  30. #define asmsh_log_info(msg) \
  31. asmsh_log(_default_logger, ASMSH_INFO, __FUNCTION__, msg)
  32. #define asmsh_log_warning(msg) \
  33. asmsh_log(_default_logger, ASMSH_WARN, __FUNCTION__, msg)
  34. #define asmsh_log_error(msg) \
  35. asmsh_log(_default_logger, ASMSH_ERR, __FUNCTION__, msg)
  36. #define asmsh_log_fatal(msg) \
  37. asmsh_log(_default_logger, ASMSH_FATAL, __FUNCTION__, msg)
  38. #define ASMSH_LOG_BUFFER_ALLOC 4096
  39. typedef struct asmsh_logger_s asmsh_logger_t;
  40. typedef struct asmsh_log_msg_s asmsh_log_msg_t;
  41. typedef enum asmsh_loglevel_e asmsh_loglevel_t;
  42. typedef int (asmsh_log_fmt_f)(asmsh_log_msg_t*, char*, int);
  43. extern asmsh_logger_t *_default_logger;
  44. enum asmsh_loglevel_e
  45. {
  46. ASMSH_TRACE = 0,
  47. ASMSH_DEBUG = 10,
  48. ASMSH_INFO = 20,
  49. ASMSH_WARN = 30,
  50. ASMSH_ERR = 40,
  51. ASMSH_FATAL = 50
  52. };
  53. struct asmsh_log_msg_s
  54. {
  55. asmsh_loglevel_t level;
  56. time_t timestamp;
  57. char *caller;
  58. char *msg;
  59. asmsh_log_msg_t *nxt;
  60. };
  61. struct asmsh_logger_s
  62. {
  63. asmsh_loglevel_t min_level;
  64. /** Buffered messages
  65. *
  66. * A single buffer is used to store asmsh_log_msg_t and the
  67. * associated strings (caller & msg)
  68. * msgs will be a sequence of [asmsh_log_msg_t][char*(caller)][char*(msg)]...
  69. */
  70. void *msgs;
  71. /** Point on the address of the next message */
  72. asmsh_log_msg_t *nxt;
  73. /** Memory used by stored messages */
  74. size_t msgs_sz;
  75. /** Allocated buffer for messages */
  76. size_t msgs_alloc;
  77. /** Default formatter */
  78. asmsh_log_fmt_f *fmt;
  79. };
  80. static inline int asmsh_logger_msg_islast(asmsh_logger_t *logger, asmsh_log_msg_t *msg) {
  81. return ((void*)msg) == ((void*)logger->nxt);
  82. }
  83. static inline int asmsh_logger_empty(asmsh_logger_t *logger) {
  84. return asmsh_logger_msg_islast(logger, (asmsh_log_msg_t*)logger->msgs);
  85. }
  86. /** If null use default min_level of WARN
  87. * @param asmsh_logger_t* Optionnal logger to set
  88. * @return 0 if ok else -1
  89. */
  90. int asmsh_logger_setup(asmsh_logger_t *logger);
  91. asmsh_logger_t* asmsh_logger_new(asmsh_loglevel_t min_level);
  92. void asmsh_logger_free(asmsh_logger_t* logger);
  93. int asmsh_logger_dprint_fmt(int fd, asmsh_logger_t *logger, asmsh_log_fmt_f *custom_fmt);
  94. static int asmsh_logger_dprint(int fd, asmsh_logger_t *logger)
  95. {
  96. return asmsh_logger_dprint_fmt(fd, logger, NULL);
  97. }
  98. /// Collect & format messages before printing them on stderr
  99. static inline int asmsh_logger_stderr(asmsh_logger_t *logger) {
  100. return asmsh_logger_dprint(2, logger);
  101. }
  102. int asmsh_log(asmsh_logger_t *logger, asmsh_loglevel_t lvl, const char caller[], const char *msg);
  103. const char * asmsh_loglevel_name(asmsh_loglevel_t lvl);
  104. /** Default formatter with UTC datetime, lvl, caller and message */
  105. int asmsh_log_default_fmt(asmsh_log_msg_t *msg, char *res, int sz);
  106. /** Simple formatter with level and message */
  107. int asmsh_log_lvl_fmt(asmsh_log_msg_t *msg, char *res, int sz);
  108. #endif