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.c 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. #include "logger.h"
  14. asmsh_logger_t *_default_logger=NULL;
  15. int asmsh_logger_setup(asmsh_logger_t *logger)
  16. {
  17. if(logger)
  18. {
  19. _default_logger = logger;
  20. return 0;
  21. }
  22. _default_logger = asmsh_logger_new(ASMSH_WARN);
  23. return (_default_logger == NULL) ? -1 : 0;
  24. }
  25. asmsh_logger_t* asmsh_logger_new(asmsh_loglevel_t min_level)
  26. {
  27. asmsh_logger_t *res;
  28. int err;
  29. if((res = malloc(sizeof(*res))) == NULL)
  30. {
  31. err = errno;
  32. dprintf(2, "Error allocating logger");
  33. errno = err;
  34. return NULL;
  35. }
  36. if((res->msgs = malloc(ASMSH_LOG_BUFFER_ALLOC)) == NULL)
  37. {
  38. err = errno;
  39. dprintf(2, "Error allocating logger's buffer");
  40. goto err_msgs;
  41. }
  42. res->min_level = min_level;
  43. res->msgs_sz = 0;
  44. res->msgs_alloc = ASMSH_LOG_BUFFER_ALLOC;
  45. res->nxt = res->msgs;
  46. res->fmt = asmsh_log_default_fmt;
  47. return res;
  48. err_msgs:
  49. free(res);
  50. errno = err;
  51. return NULL;
  52. }
  53. void asmsh_logger_free(asmsh_logger_t* logger)
  54. {
  55. if(logger->msgs) { free(logger->msgs); }
  56. free(logger);
  57. }
  58. int asmsh_logger_dprint_fmt(int fd, asmsh_logger_t *logger, asmsh_log_fmt_f *custom_fmt)
  59. {
  60. const int BUF_ALLOC = 4096;
  61. char *buf;
  62. int bufsz, ret, res;
  63. asmsh_log_msg_t *cur;
  64. asmsh_log_fmt_f *fmt;
  65. fmt = custom_fmt?custom_fmt:logger->fmt;
  66. if((buf = malloc(BUF_ALLOC)) == NULL)
  67. {
  68. return -1;
  69. }
  70. bufsz = BUF_ALLOC;
  71. cur = (asmsh_log_msg_t*)logger->msgs;
  72. res = 0;
  73. while(cur != logger->nxt)
  74. {
  75. ret = fmt(cur, buf, bufsz);
  76. if(ret < 0)
  77. {
  78. perror("Unable to format log message");
  79. continue;
  80. }
  81. else if (ret == BUF_ALLOC)
  82. {
  83. buf[BUF_ALLOC-1] = '\0';
  84. for(int i=0; i<3; i++)
  85. {
  86. buf[BUF_ALLOC-(2+i)] = '.';
  87. }
  88. }
  89. res += dprintf(fd, buf); /// TODO use write & check result
  90. cur = cur->nxt;
  91. }
  92. free(buf);
  93. logger->nxt = logger->msgs;
  94. return res;
  95. }
  96. int asmsh_logger_strdup(asmsh_logger_t *logger, char **result, size_t *res_sz)
  97. {
  98. const int BUF_ALLOC = 4096;
  99. size_t buf_sz;
  100. asmsh_log_msg_t *cur;
  101. char *buf_ptr;
  102. int ret;
  103. *res_sz = 0;
  104. buf_ptr = *result = malloc(buf_sz = BUF_ALLOC);
  105. if(*result == NULL)
  106. {
  107. perror("Enable to allocate buffer for log messages");
  108. return -1;
  109. }
  110. cur = (asmsh_log_msg_t*)logger->msgs;
  111. while(cur != logger->nxt)
  112. {
  113. ret = logger->fmt(cur, buf_ptr, buf_sz - *res_sz);
  114. if(ret == (*res_sz - buf_sz))
  115. {
  116. buf_sz += BUF_ALLOC;
  117. void *tmp = realloc(*result, buf_sz);
  118. }
  119. else
  120. {
  121. *res_sz += ret;
  122. cur = cur->nxt;
  123. }
  124. }
  125. }
  126. int asmsh_log(asmsh_logger_t *logger, asmsh_loglevel_t lvl, const char caller[],
  127. const char *msg)
  128. {
  129. if(!logger)
  130. {
  131. logger = _default_logger = asmsh_logger_new(ASMSH_TRACE);
  132. }
  133. if(lvl < logger->min_level) { return 0; }
  134. size_t caller_len, msg_len, total_len;
  135. caller_len = strlen(caller);
  136. msg_len = strlen(msg);
  137. total_len = msg_len + caller_len + 2 + sizeof(asmsh_log_msg_t);
  138. if(logger->msgs_alloc <= logger->msgs_sz + total_len)
  139. {
  140. void *tmp;
  141. logger->msgs_alloc += ASMSH_LOG_BUFFER_ALLOC;
  142. tmp = realloc(logger->msgs, logger->msgs_alloc);
  143. if(tmp <= 0)
  144. {
  145. return -1;
  146. }
  147. if(tmp != logger->msgs)
  148. {
  149. logger->nxt = tmp + ((void*)logger->nxt - (void*)logger->msgs);
  150. }
  151. logger->msgs = tmp;
  152. }
  153. if(logger->msgs_alloc <= logger->msgs_sz + total_len)
  154. {
  155. // still to short just after realloc, more than
  156. // ASMSH_LOG_BUFFER_ALLOC is needed, something seems
  157. // to be wrong
  158. errno = EMSGSIZE;
  159. return -1;
  160. }
  161. logger->msgs_sz += total_len;
  162. logger->nxt->level = lvl;
  163. if(time(&(logger->nxt->timestamp)) < 0)
  164. {
  165. return -1;
  166. }
  167. logger->nxt->caller = (char*)((logger->nxt)+1);
  168. logger->nxt->msg = logger->nxt->caller + caller_len + 1;
  169. strncpy(logger->nxt->caller, caller, caller_len+1);
  170. strncpy(logger->nxt->msg, msg, msg_len+1);
  171. logger->nxt->nxt = (asmsh_log_msg_t*)((void*)logger->nxt->msg+total_len);
  172. logger->nxt = logger->nxt->nxt;
  173. return 0;
  174. }
  175. const char * asmsh_loglevel_name(asmsh_loglevel_t lvl)
  176. {
  177. switch(lvl)
  178. {
  179. case ASMSH_TRACE:
  180. return "TRACE";
  181. case ASMSH_DEBUG:
  182. return "DEBUG";
  183. case ASMSH_INFO:
  184. return "INFO";
  185. case ASMSH_WARN:
  186. return "WARN";
  187. case ASMSH_ERR:
  188. return "ERR";
  189. case ASMSH_FATAL:
  190. return "FATAL";
  191. default:
  192. return "UNKNW";
  193. }
  194. }
  195. int asmsh_log_default_fmt(asmsh_log_msg_t *msg, char *res, int sz)
  196. {
  197. int ret;
  198. struct tm msg_tm;
  199. char dtstr[32];
  200. if(gmtime_r(&msg->timestamp, &msg_tm) == NULL)
  201. {
  202. strncpy(res, "DATETIME ERROR", sz);
  203. return -1;
  204. }
  205. if(strftime(dtstr, sizeof(dtstr), "%FT%H:%M:%S+00:00", &msg_tm) == 0)
  206. {
  207. strncpy(res, "DATETIME FMT ERROR", sz);
  208. return -1;
  209. }
  210. ret = snprintf(res, sz, "%s [%s](%s) : %s\n",
  211. dtstr, asmsh_loglevel_name(msg->level),
  212. msg->caller, msg->msg);
  213. return ret;
  214. }
  215. int asmsh_log_lvl_fmt(asmsh_log_msg_t *msg, char *res, int sz)
  216. {
  217. int ret;
  218. ret = snprintf(res, sz, "%s: %s\n",
  219. asmsh_loglevel_name(msg->level),
  220. msg->msg);
  221. return ret;
  222. }