A clone of btpd with my configuration changes.
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
1.4 KiB

  1. #include <stdarg.h>
  2. #include <stdio.h>
  3. #include <time.h>
  4. #include "btpd.h"
  5. void *
  6. btpd_malloc(size_t size)
  7. {
  8. void *a;
  9. if ((a = malloc(size)) == NULL)
  10. btpd_err("Failed to allocate %d bytes.\n", (int)size);
  11. return a;
  12. }
  13. void *
  14. btpd_calloc(size_t nmemb, size_t size)
  15. {
  16. void *a;
  17. if ((a = calloc(nmemb, size)) == NULL)
  18. btpd_err("Failed to allocate %d bytes.\n", (int)(nmemb * size));
  19. return a;
  20. }
  21. static const char *
  22. logtype_str(uint32_t type)
  23. {
  24. if (type & BTPD_L_BTPD)
  25. return "btpd";
  26. else if (type & BTPD_L_ERROR)
  27. return "error";
  28. else if (type & BTPD_L_CONN)
  29. return "conn";
  30. else if (type & BTPD_L_TRACKER)
  31. return "tracker";
  32. else if (type & BTPD_L_MSG)
  33. return "msg";
  34. else
  35. return "";
  36. }
  37. void
  38. btpd_err(const char *fmt, ...)
  39. {
  40. va_list ap;
  41. va_start(ap, fmt);
  42. if (BTPD_L_ERROR & btpd_logmask) {
  43. char tbuf[20];
  44. time_t tp = time(NULL);
  45. strftime(tbuf, 20, "%b %e %T", localtime(&tp));
  46. printf("%s %s: ", tbuf, logtype_str(BTPD_L_ERROR));
  47. vprintf(fmt, ap);
  48. }
  49. va_end(ap);
  50. exit(1);
  51. }
  52. void
  53. btpd_log(uint32_t type, const char *fmt, ...)
  54. {
  55. va_list ap;
  56. va_start(ap, fmt);
  57. if (type & btpd_logmask) {
  58. char tbuf[20];
  59. time_t tp = time(NULL);
  60. strftime(tbuf, 20, "%b %e %T", localtime(&tp));
  61. printf("%s %s: ", tbuf, logtype_str(type));
  62. vprintf(fmt, ap);
  63. }
  64. va_end(ap);
  65. }