log.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright (C) 2004-2021 Artifex Software, Inc.
  2. //
  3. // This file is part of MuPDF.
  4. //
  5. // MuPDF is free software: you can redistribute it and/or modify it under the
  6. // terms of the GNU Affero General Public License as published by the Free
  7. // Software Foundation, either version 3 of the License, or (at your option)
  8. // any later version.
  9. //
  10. // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
  11. // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. // details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
  17. //
  18. // Alternative licensing terms are available from the licensor.
  19. // For commercial licensing, see <https://www.artifex.com/> or contact
  20. // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
  21. // CA 94129, USA, for further information.
  22. #include "mupdf/fitz.h"
  23. #include <assert.h>
  24. #include <limits.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27. fz_output *fz_new_log_for_module(fz_context *ctx, const char *module)
  28. {
  29. char text[256];
  30. char *s = NULL;
  31. if (module)
  32. {
  33. fz_snprintf(text, sizeof(text), "FZ_LOG_FILE_%s", module);
  34. s = getenv(text);
  35. }
  36. if (s == NULL)
  37. s = getenv("FZ_LOG_FILE");
  38. if (s == NULL)
  39. s = "fitz_log.txt";
  40. return fz_new_output_with_path(ctx, s, 1);
  41. }
  42. void fz_log(fz_context *ctx, const char *fmt, ...)
  43. {
  44. fz_output *out;
  45. va_list args;
  46. va_start(args, fmt);
  47. fz_try(ctx)
  48. {
  49. out = fz_new_log_for_module(ctx, NULL);
  50. fz_write_vprintf(ctx, out, fmt, args);
  51. fz_close_output(ctx, out);
  52. }
  53. fz_always(ctx)
  54. {
  55. va_end(args);
  56. fz_drop_output(ctx, out);
  57. }
  58. fz_catch(ctx)
  59. fz_rethrow(ctx);
  60. }
  61. void fz_log_module(fz_context *ctx, const char *module, const char *fmt, ...)
  62. {
  63. fz_output *out;
  64. va_list args;
  65. va_start(args, fmt);
  66. fz_try(ctx)
  67. {
  68. out = fz_new_log_for_module(ctx, module);
  69. if (module)
  70. fz_write_printf(ctx, out, "%s\t", module);
  71. fz_write_vprintf(ctx, out, fmt, args);
  72. fz_close_output(ctx, out);
  73. }
  74. fz_always(ctx)
  75. {
  76. va_end(args);
  77. fz_drop_output(ctx, out);
  78. }
  79. fz_catch(ctx)
  80. fz_rethrow(ctx);
  81. }