callbacks.c 699 B

12345678910111213141516171819202122232425262728293031
  1. // callbacks.c - Sets alpm callbacks to Go functions.
  2. //
  3. // Copyright (c) 2013 The go-alpm Authors
  4. //
  5. // MIT Licensed. See LICENSE for details.
  6. #include <stdint.h>
  7. #include <stdio.h>
  8. #include <stdarg.h>
  9. #include <alpm.h>
  10. void logCallback(uint16_t level, char *cstring);
  11. void go_alpm_log_cb(alpm_loglevel_t level, const char *fmt, va_list arg) {
  12. char *s = malloc(128);
  13. if (s == NULL) return;
  14. int16_t length = vsnprintf(s, 128, fmt, arg);
  15. if (length > 128) {
  16. length = (length + 16) & ~0xf;
  17. s = realloc(s, length);
  18. }
  19. if (s != NULL) {
  20. logCallback(level, s);
  21. free(s);
  22. }
  23. }
  24. void go_alpm_set_logging(alpm_handle_t *handle) {
  25. alpm_option_set_logcb(handle, go_alpm_log_cb);
  26. }