swlog.c 952 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #include "swapi.h"
  2. #include "swlog.h"
  3. // 声明Go中的日志函数(Go实现, 供C/C++调用)
  4. extern void LogFromGo(const char *level, const char *file, int line, const char *message);
  5. // 带有打印级别的打印输出(该函数会自动在行尾加入回车换行符)
  6. void sw_log_writeWithLevel(ELogLevel level, const char *file, int line, const char *format, ...)
  7. {
  8. char buf[8192] = { 0 };
  9. va_list ap;
  10. va_start(ap, format);
  11. vsnprintf(buf, sizeof(buf), format, ap);
  12. va_end(ap);
  13. switch(level)
  14. {
  15. case LEVEL_TRACE:
  16. LogFromGo("trace", file, line, buf);
  17. break;
  18. case LEVEL_DEBUG:
  19. LogFromGo("debug", file, line, buf);
  20. break;
  21. case LEVEL_INFO:
  22. LogFromGo("info", file, line, buf);
  23. break;
  24. case LEVEL_WARN:
  25. LogFromGo("warn", file, line, buf);
  26. break;
  27. case LEVEL_ERROR:
  28. LogFromGo("error", file, line, buf);
  29. break;
  30. case LEVEL_FATAL:
  31. LogFromGo("fatal", file, line, buf);
  32. break;
  33. }
  34. }