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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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, 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. int 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 err;
  103. *res_sz = 0;
  104. buf_ptr = *result = malloc(buf_sz = BUF_ALLOC);
  105. if(*result == NULL)
  106. {
  107. err=errno;
  108. perror("Enable to allocate buffer for log messages");
  109. errno=err;
  110. return -1;
  111. }
  112. cur = (asmsh_log_msg_t*)logger->msgs;
  113. while(cur != logger->nxt)
  114. {
  115. int ret = logger->fmt(cur, buf_ptr, buf_sz - *res_sz);
  116. if(ret == (*res_sz - buf_sz))
  117. {
  118. buf_sz += BUF_ALLOC;
  119. void *tmp = realloc(*result, buf_sz);
  120. if(!tmp)
  121. {
  122. err=errno;
  123. perror("Unable to strdup logline");
  124. errno=err;
  125. return -1;
  126. }
  127. result=tmp;
  128. }
  129. else
  130. {
  131. *res_sz += ret;
  132. cur = cur->nxt;
  133. }
  134. }
  135. return 0;
  136. }
  137. int asmsh_log(asmsh_logger_t *logger, asmsh_loglevel_t lvl, const char caller[],
  138. const char *msg)
  139. {
  140. if(!logger)
  141. {
  142. logger = _default_logger = asmsh_logger_new(ASMSH_TRACE);
  143. }
  144. if(lvl < logger->min_level) { return 0; }
  145. size_t caller_len, msg_len, total_len;
  146. caller_len = strlen(caller);
  147. msg_len = strlen(msg);
  148. total_len = msg_len + caller_len + 2 + sizeof(asmsh_log_msg_t);
  149. if(logger->msgs_alloc <= logger->msgs_sz + total_len)
  150. {
  151. asmsh_log_msg_t *tmp;
  152. logger->msgs_alloc += ASMSH_LOG_BUFFER_ALLOC;
  153. tmp = realloc(logger->msgs, logger->msgs_alloc);
  154. if(tmp == NULL)
  155. {
  156. return -1;
  157. }
  158. if(tmp != logger->msgs)
  159. {
  160. logger->nxt = tmp + ((void*)logger->nxt - (void*)logger->msgs);
  161. }
  162. logger->msgs = tmp;
  163. }
  164. if(logger->msgs_alloc <= logger->msgs_sz + total_len)
  165. {
  166. // still to short just after realloc, more than
  167. // ASMSH_LOG_BUFFER_ALLOC is needed, something seems
  168. // to be wrong
  169. errno = EMSGSIZE;
  170. return -1;
  171. }
  172. logger->msgs_sz += total_len;
  173. logger->nxt->level = lvl;
  174. if(time(&(logger->nxt->timestamp)) < 0)
  175. {
  176. return -1;
  177. }
  178. logger->nxt->caller = (char*)((logger->nxt)+1);
  179. logger->nxt->msg = logger->nxt->caller + caller_len + 1;
  180. strncpy(logger->nxt->caller, caller, caller_len+1);
  181. strncpy(logger->nxt->msg, msg, msg_len+1);
  182. logger->nxt->nxt = (asmsh_log_msg_t*)((char*)logger->nxt->msg+total_len);
  183. logger->nxt = logger->nxt->nxt;
  184. return 0;
  185. }
  186. const char * asmsh_loglevel_name(asmsh_loglevel_t lvl)
  187. {
  188. switch(lvl)
  189. {
  190. case ASMSH_TRACE:
  191. return "TRACE";
  192. case ASMSH_DEBUG:
  193. return "DEBUG";
  194. case ASMSH_INFO:
  195. return "INFO";
  196. case ASMSH_WARN:
  197. return "WARN";
  198. case ASMSH_ERR:
  199. return "ERR";
  200. case ASMSH_FATAL:
  201. return "FATAL";
  202. default:
  203. return "UNKNW";
  204. }
  205. }
  206. int asmsh_log_default_fmt(asmsh_log_msg_t *msg, char *res, int sz)
  207. {
  208. int ret;
  209. struct tm msg_tm;
  210. char dtstr[32];
  211. if(gmtime_r(&msg->timestamp, &msg_tm) == NULL)
  212. {
  213. strncpy(res, "DATETIME ERROR", sz);
  214. return -1;
  215. }
  216. if(strftime(dtstr, sizeof(dtstr), "%FT%H:%M:%S+00:00", &msg_tm) == 0)
  217. {
  218. strncpy(res, "DATETIME FMT ERROR", sz);
  219. return -1;
  220. }
  221. ret = snprintf(res, sz, "%s [%s](%s) : %s\n",
  222. dtstr, asmsh_loglevel_name(msg->level),
  223. msg->caller, msg->msg);
  224. return ret;
  225. }
  226. int asmsh_log_lvl_fmt(asmsh_log_msg_t *msg, char *res, int sz)
  227. {
  228. int ret;
  229. ret = snprintf(res, sz, "%s: %s\n",
  230. asmsh_loglevel_name(msg->level),
  231. msg->msg);
  232. return ret;
  233. }