pa_debugprint.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * $Id: pa_log.c $
  3. * Portable Audio I/O Library Multi-Host API front end
  4. * Validate function parameters and manage multiple host APIs.
  5. *
  6. * Based on the Open Source API proposed by Ross Bencina
  7. * Copyright (c) 1999-2006 Ross Bencina, Phil Burk
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining
  10. * a copy of this software and associated documentation files
  11. * (the "Software"), to deal in the Software without restriction,
  12. * including without limitation the rights to use, copy, modify, merge,
  13. * publish, distribute, sublicense, and/or sell copies of the Software,
  14. * and to permit persons to whom the Software is furnished to do so,
  15. * subject to the following conditions:
  16. *
  17. * The above copyright notice and this permission notice shall be
  18. * included in all copies or substantial portions of the Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  23. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  24. * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  25. * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. */
  28. /*
  29. * The text above constitutes the entire PortAudio license; however,
  30. * the PortAudio community also makes the following non-binding requests:
  31. *
  32. * Any person wishing to distribute modifications to the Software is
  33. * requested to send the modifications to the original developer so that
  34. * they can be incorporated into the canonical version. It is also
  35. * requested that these non-binding requests be included along with the
  36. * license above.
  37. */
  38. /** @file
  39. @ingroup common_src
  40. @brief Implements log function.
  41. PaUtil_SetLogPrintFunction can be user called to replace the provided
  42. DefaultLogPrint function, which writes to stderr.
  43. One can NOT pass var_args across compiler/dll boundaries as it is not
  44. "byte code/abi portable". So the technique used here is to allocate a local
  45. a static array, write in it, then callback the user with a pointer to its
  46. start.
  47. */
  48. #include <stdio.h>
  49. #include <stdarg.h>
  50. #include "pa_debugprint.h"
  51. // for OutputDebugStringA
  52. #if defined(_MSC_VER) && defined(PA_ENABLE_MSVC_DEBUG_OUTPUT)
  53. #define WIN32_LEAN_AND_MEAN // exclude rare headers
  54. #include "windows.h"
  55. #endif
  56. // User callback
  57. static PaUtilLogCallback userCB = NULL;
  58. // Sets user callback
  59. void PaUtil_SetDebugPrintFunction(PaUtilLogCallback cb)
  60. {
  61. userCB = cb;
  62. }
  63. /*
  64. If your platform doesn’t have vsnprintf, you are stuck with a
  65. VERY dangerous alternative, vsprintf (with no n)
  66. */
  67. #if _MSC_VER
  68. /* Some Windows Mobile SDKs don't define vsnprintf but all define _vsnprintf (hopefully).
  69. According to MSDN "vsnprintf is identical to _vsnprintf". So we use _vsnprintf with MSC.
  70. */
  71. #define VSNPRINTF _vsnprintf
  72. #else
  73. #define VSNPRINTF vsnprintf
  74. #endif
  75. #define PA_LOG_BUF_SIZE 2048
  76. void PaUtil_DebugPrint( const char *format, ... )
  77. {
  78. // Optional logging into Output console of Visual Studio
  79. #if defined(_MSC_VER) && defined(PA_ENABLE_MSVC_DEBUG_OUTPUT)
  80. {
  81. char buf[PA_LOG_BUF_SIZE];
  82. va_list ap;
  83. va_start(ap, format);
  84. VSNPRINTF(buf, sizeof(buf), format, ap);
  85. buf[sizeof(buf)-1] = 0;
  86. OutputDebugStringA(buf);
  87. va_end(ap);
  88. }
  89. #endif
  90. // Output to User-Callback
  91. if (userCB != NULL)
  92. {
  93. char strdump[PA_LOG_BUF_SIZE];
  94. va_list ap;
  95. va_start(ap, format);
  96. VSNPRINTF(strdump, sizeof(strdump), format, ap);
  97. strdump[sizeof(strdump)-1] = 0;
  98. userCB(strdump);
  99. va_end(ap);
  100. }
  101. else
  102. // Standard output to stderr
  103. {
  104. va_list ap;
  105. va_start(ap, format);
  106. vfprintf(stderr, format, ap);
  107. va_end(ap);
  108. fflush(stderr);
  109. }
  110. }