A shell that runs x86_64 assembly
c
x86-64
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. * @warning This module is totally overkill. Most of the features (log collection
  21. * etc.) seems to be useless ! Apparently, in a shell, we just want errors to be printed
  22. * on stderr ?
  23. *
  24. * TODO continue to simplify the module & implement direct logging features
  25. */
  26. #include <errno.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <stdarg.h>
  31. #include <time.h>
  32. #include <unistd.h>
  33. #define asmsh_log_trace(msg_args...) \
  34. asmsh_log(_default_logger, ASMSH_TRACE, __FUNCTION__, msg_args)
  35. #define asmsh_log_debug(msg_args...) \
  36. asmsh_log(_default_logger, ASMSH_DEBUG, __FUNCTION__, msg_args)
  37. #define asmsh_log_info(msg_args...) \
  38. asmsh_log(_default_logger, ASMSH_INFO, __FUNCTION__, msg_args)
  39. #define asmsh_log_warning(msg_args...) \
  40. asmsh_log(_default_logger, ASMSH_WARN, __FUNCTION__, msg_args)
  41. #define asmsh_log_error(msg_args...) \
  42. asmsh_log(_default_logger, ASMSH_ERR, __FUNCTION__, msg_args)
  43. #define asmsh_log_fatal(msg_args...) \
  44. asmsh_log(_default_logger, ASMSH_FATAL, __FUNCTION__, msg_args)
  45. #define asmsh_log_perror(msg) \
  46. asmsh_log_error("%s : %s", msg, strerror(errno));
  47. #define ASMSH_LOG_BUFFER_ALLOC 4096
  48. typedef struct asmsh_logger_s asmsh_logger_t;
  49. typedef struct asmsh_log_msg_s asmsh_log_msg_t;
  50. typedef enum asmsh_loglevel_e asmsh_loglevel_t;
  51. /**@param asmsh_log_msg_t* the log message to format
  52. * @param char* the result buffer
  53. * @param int the buffer size
  54. * @return The needed size for the message in buffer (@see snprintf)
  55. * if no error, else -1
  56. */
  57. typedef int (asmsh_log_fmt_f)(asmsh_log_msg_t*, char*, int);
  58. enum asmsh_loglevel_e
  59. {
  60. ASMSH_TRACE = 0,
  61. ASMSH_DEBUG = 10,
  62. ASMSH_INFO = 20,
  63. ASMSH_WARN = 30,
  64. ASMSH_ERR = 40,
  65. ASMSH_FATAL = 50
  66. };
  67. extern asmsh_logger_t *_default_logger;
  68. struct asmsh_log_msg_s
  69. {
  70. asmsh_loglevel_t level;
  71. time_t timestamp;
  72. char *caller;
  73. char *msg;
  74. asmsh_log_msg_t *nxt;
  75. };
  76. struct asmsh_logger_s
  77. {
  78. asmsh_loglevel_t min_level;
  79. /** Direct log fd */
  80. //int dlog_fd; // TODO should be multiple FDs !?
  81. /** Buffered messages
  82. *
  83. * A single buffer is used to store asmsh_log_msg_t and the
  84. * associated strings (caller & msg)
  85. * msgs will be a sequence of [asmsh_log_msg_t][char*(caller)][char*(msg)]...
  86. */
  87. void *msgs;
  88. /** Point on the address of the next message */
  89. asmsh_log_msg_t *nxt;
  90. /** Memory used by stored messages */
  91. size_t msgs_sz;
  92. /** Allocated buffer for messages */
  93. size_t msgs_alloc;
  94. /** Default formatter */
  95. asmsh_log_fmt_f *fmt;
  96. /** Default direct log formatter */
  97. asmsh_log_fmt_f *dfmt;
  98. };
  99. static inline int asmsh_logger_msg_islast(asmsh_logger_t *logger, asmsh_log_msg_t *msg) {
  100. return ((void*)msg) == ((void*)logger->nxt);
  101. }
  102. static inline int asmsh_logger_empty(asmsh_logger_t *logger) {
  103. return asmsh_logger_msg_islast(logger, (asmsh_log_msg_t*)logger->msgs);
  104. }
  105. /** If null use default min_level of WARN
  106. * @param asmsh_logger_t* Optionnal logger to set
  107. * @return 0 if ok else -1
  108. */
  109. int asmsh_logger_setup(asmsh_logger_t *logger);
  110. /** Returns a pointer on a newly allocated logger
  111. * @param asmsh_log_level_t The minimum level for a message must have to be printed
  112. * @returns A newly allocated logger that must be freed after use by @ref asmsh_logger_ferr()
  113. */
  114. asmsh_logger_t* asmsh_logger_new(asmsh_loglevel_t min_level);
  115. /** Free a logger */
  116. void asmsh_logger_free(asmsh_logger_t* logger);
  117. int asmsh_logger_dprint_fmt(int fd, asmsh_logger_t *logger, asmsh_log_fmt_f *custom_fmt);
  118. static int asmsh_logger_dprint(int fd, asmsh_logger_t *logger)
  119. {
  120. return asmsh_logger_dprint_fmt(fd, logger, NULL);
  121. }
  122. /// Collect & format messages before printing them on stderr
  123. static inline int asmsh_logger_stderr(asmsh_logger_t *logger) {
  124. return asmsh_logger_dprint(2, logger);
  125. }
  126. /** Log a message( see macros ) */
  127. int asmsh_log(asmsh_logger_t *logger, asmsh_loglevel_t lvl, const char caller[], const char *msg, ...);
  128. /** Return a pointer on a static string representing a loglevel name */
  129. const char * asmsh_loglevel_name(asmsh_loglevel_t lvl);
  130. /** Default formatter with UTC datetime, lvl, caller and message */
  131. int asmsh_log_default_fmt(asmsh_log_msg_t *msg, char *res, int sz);
  132. /** Simple formatter with level and message */
  133. int asmsh_log_lvl_fmt(asmsh_log_msg_t *msg, char *res, int sz);
  134. #endif