A clone of btpd with my configuration changes.
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.

71 lines
1.4 KiB

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