2
0

err_prn.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/crypto.h>
  12. #include <openssl/buffer.h>
  13. #include <openssl/err.h>
  14. void ERR_print_errors_cb(int (*cb) (const char *str, size_t len, void *u),
  15. void *u)
  16. {
  17. unsigned long l;
  18. char buf[256];
  19. char buf2[4096];
  20. const char *file, *data;
  21. int line, flags;
  22. /*
  23. * We don't know what kind of thing CRYPTO_THREAD_ID is. Here is our best
  24. * attempt to convert it into something we can print.
  25. */
  26. union {
  27. CRYPTO_THREAD_ID tid;
  28. unsigned long ltid;
  29. } tid;
  30. tid.ltid = 0;
  31. tid.tid = CRYPTO_THREAD_get_current_id();
  32. while ((l = ERR_get_error_line_data(&file, &line, &data, &flags)) != 0) {
  33. ERR_error_string_n(l, buf, sizeof(buf));
  34. BIO_snprintf(buf2, sizeof(buf2), "%lu:%s:%s:%d:%s\n", tid.ltid, buf,
  35. file, line, (flags & ERR_TXT_STRING) ? data : "");
  36. if (cb(buf2, strlen(buf2), u) <= 0)
  37. break; /* abort outputting the error report */
  38. }
  39. }
  40. static int print_bio(const char *str, size_t len, void *bp)
  41. {
  42. return BIO_write((BIO *)bp, str, len);
  43. }
  44. void ERR_print_errors(BIO *bp)
  45. {
  46. ERR_print_errors_cb(print_bio, bp);
  47. }
  48. #ifndef OPENSSL_NO_STDIO
  49. void ERR_print_errors_fp(FILE *fp)
  50. {
  51. BIO *bio = BIO_new_fp(fp, BIO_NOCLOSE);
  52. if (bio == NULL)
  53. return;
  54. ERR_print_errors_cb(print_bio, bio);
  55. BIO_free(bio);
  56. }
  57. #endif