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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. va_list ap;
  146. size_t caller_len, msg_len, total_len;
  147. caller_len = strlen(caller);
  148. va_start(ap, msg);
  149. msg_len = vsnprintf(NULL, 0, msg, ap) + 1;
  150. va_end(ap);
  151. total_len = msg_len + caller_len + 1 + sizeof(asmsh_log_msg_t);
  152. if(logger->msgs_alloc <= logger->msgs_sz + total_len)
  153. {
  154. asmsh_log_msg_t *tmp;
  155. logger->msgs_alloc += ASMSH_LOG_BUFFER_ALLOC;
  156. tmp = realloc(logger->msgs, logger->msgs_alloc);
  157. if(tmp == NULL)
  158. {
  159. return -1;
  160. }
  161. if(tmp != logger->msgs)
  162. {
  163. logger->nxt = tmp + ((void*)logger->nxt - (void*)logger->msgs);
  164. }
  165. logger->msgs = tmp;
  166. }
  167. if(logger->msgs_alloc <= logger->msgs_sz + total_len)
  168. {
  169. // still to short just after realloc, more than
  170. // ASMSH_LOG_BUFFER_ALLOC is needed, something seems
  171. // to be wrong
  172. errno = EMSGSIZE;
  173. return -1;
  174. }
  175. logger->msgs_sz += total_len;
  176. logger->nxt->level = lvl;
  177. if(time(&(logger->nxt->timestamp)) < 0)
  178. {
  179. return -1;
  180. }
  181. logger->nxt->caller = (char*)((logger->nxt)+1);
  182. logger->nxt->msg = logger->nxt->caller + caller_len + 1;
  183. strncpy(logger->nxt->caller, caller, caller_len+1);
  184. va_start(ap, msg);
  185. vsnprintf(logger->nxt->msg, msg_len, msg, ap);
  186. va_end(ap);
  187. logger->nxt->nxt = (asmsh_log_msg_t*)((char*)logger->nxt->msg+total_len);
  188. logger->nxt = logger->nxt->nxt;
  189. return 0;
  190. }
  191. const char * asmsh_loglevel_name(asmsh_loglevel_t lvl)
  192. {
  193. switch(lvl)
  194. {
  195. case ASMSH_TRACE:
  196. return "TRACE";
  197. case ASMSH_DEBUG:
  198. return "DEBUG";
  199. case ASMSH_INFO:
  200. return "INFO";
  201. case ASMSH_WARN:
  202. return "WARN";
  203. case ASMSH_ERR:
  204. return "ERR";
  205. case ASMSH_FATAL:
  206. return "FATAL";
  207. default:
  208. return "UNKNW";
  209. }
  210. }
  211. int asmsh_log_default_fmt(asmsh_log_msg_t *msg, char *res, int sz)
  212. {
  213. int ret;
  214. struct tm msg_tm;
  215. char dtstr[32];
  216. if(gmtime_r(&msg->timestamp, &msg_tm) == NULL)
  217. {
  218. strncpy(res, "DATETIME ERROR", sz);
  219. return -1;
  220. }
  221. if(strftime(dtstr, sizeof(dtstr), "%FT%H:%M:%S+00:00", &msg_tm) == 0)
  222. {
  223. strncpy(res, "DATETIME FMT ERROR", sz);
  224. return -1;
  225. }
  226. ret = snprintf(res, sz, "%s [%s](%s) : %s\n",
  227. dtstr, asmsh_loglevel_name(msg->level),
  228. msg->caller, msg->msg);
  229. return ret;
  230. }
  231. int asmsh_log_lvl_fmt(asmsh_log_msg_t *msg, char *res, int sz)
  232. {
  233. int ret;
  234. ret = snprintf(res, sz, "%s: %s\n",
  235. asmsh_loglevel_name(msg->level),
  236. msg->msg);
  237. return ret;
  238. }