選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

elog.c 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #include <elog.h>
  2. #include <string.h>
  3. elog_t *elog_init(void *arena, size_t size)
  4. {
  5. elog_t *ptr = (elog_t *)arena;
  6. ptr->buflen = size - sizeof(elog_t); // Adjust for the size of the elog_t structure
  7. ptr->offset = 0;
  8. return ptr;
  9. }
  10. int elog_put(elog_t *log, const char *const msg, int n, msgparam_t args[])
  11. {
  12. elog_entry_t *e = (elog_entry_t *)(log->buffer + log->offset); // 计算当前可写的位置e(即log->buffer)
  13. long esize = n * sizeof(msgparam_t); // 参数占用字节数
  14. long newoff = log->offset + sizeof(elog_entry_t) + esize; // 计算新的偏移量,开始是elog_entry_t头 + n个msg(id和长度组成)
  15. if (newoff < log->buflen)
  16. {
  17. e->msgid = MSGPTR_MAKE(n, (msgptr_t)msg); // 通过字符串地址和个数生成msgid,并写入e
  18. memcpy(e->data, args, esize); // 参数列表写入e
  19. log->offset = newoff;
  20. return 1;
  21. }
  22. return 0;
  23. }
  24. void elog_flush(elog_t *log, elog_flush_func_t func, void *ctx)
  25. {
  26. long off = 0;
  27. while (off < log->offset) // 也可以直接发送出去上位机,在上位机端解码
  28. {
  29. elog_entry_t *e = ((elog_entry_t *)(log->buffer + off));
  30. size_t len = MSGPTR_LEN(e->msgid) * sizeof(long);
  31. size_t incr = sizeof(elog_entry_t) + len;
  32. off += incr;
  33. func(e, incr, ctx); // 解出一个e[id + data]
  34. }
  35. log->offset = 0;
  36. }