A shell that runs x86_64 assembly
c
x86-64
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

logger.c 4.3KB

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