libcm is a C development framework with an emphasis on audio signal processing applications.
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.

cmRpt.c 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //| Copyright: (C) 2009-2020 Kevin Larke <contact AT larke DOT org>
  2. //| License: GNU GPL version 3.0 or above. See the accompanying LICENSE file.
  3. #include "cmPrefix.h"
  4. #include "cmGlobal.h"
  5. #include "cmRpt.h"
  6. #include "cmErr.h"
  7. #include "cmCtx.h"
  8. #include "cmMem.h"
  9. #include "cmMallocDebug.h"
  10. cmRpt_t cmRptNull = { NULL, NULL, NULL };
  11. void _cmDefaultPrint( void* userPtr, const cmChar_t* text )
  12. {
  13. if( text != NULL )
  14. fputs(text,stdout);
  15. }
  16. void _cmDefaultError( void* userPtr, const cmChar_t* text )
  17. {
  18. if( text != NULL )
  19. fputs(text,stderr);
  20. }
  21. void _cmOut( cmRptPrintFunc_t printFunc, void* userData, const cmChar_t* text )
  22. {
  23. if( printFunc == NULL )
  24. _cmDefaultPrint(userData,text);
  25. else
  26. printFunc(userData,text);
  27. }
  28. void _cmVOut( cmRptPrintFunc_t printFunc, void* userData, const cmChar_t* fmt, va_list vl )
  29. {
  30. va_list vl1;
  31. va_copy(vl1,vl);
  32. unsigned n = vsnprintf(NULL,0,fmt,vl1);
  33. va_end(vl1);
  34. unsigned bufN = 511;
  35. cmChar_t buf[bufN+1];
  36. cmChar_t* b = buf;
  37. unsigned bn = bufN;
  38. if( n > bufN )
  39. {
  40. b = cmMemAllocZ(cmChar_t,n+1);
  41. bn = n;
  42. }
  43. b[0]=0;
  44. if( fmt != NULL )
  45. if( vsnprintf(b,bn,fmt,vl) > bn )
  46. _cmOut(printFunc,userData,"The following error message was truncated because the character buffer in cmRpt::_cmVOut() was too small.");
  47. _cmOut(printFunc,userData,b);
  48. if( n > bufN )
  49. cmMemFree(b);
  50. }
  51. void cmRptSetup( cmRpt_t* rpt, cmRptPrintFunc_t printFunc, cmRptPrintFunc_t errorFunc, void* userPtr )
  52. {
  53. rpt->printFuncPtr = printFunc==NULL ? _cmDefaultPrint : printFunc;
  54. rpt->errorFuncPtr = errorFunc==NULL ? _cmDefaultError : errorFunc;
  55. rpt->userPtr = userPtr;
  56. }
  57. void cmRptPrint( cmRpt_t* rpt, const cmChar_t* text )
  58. {
  59. cmRptPrintFunc_t pfp = rpt==NULL ? NULL : rpt->printFuncPtr;
  60. void* udp = rpt==NULL ? NULL : rpt->userPtr;
  61. _cmOut(pfp,udp,text);
  62. }
  63. void cmRptVPrintf( cmRpt_t* rpt, const cmChar_t* fmt, va_list vl )
  64. {
  65. cmRptPrintFunc_t pfp = rpt==NULL ? NULL : rpt->printFuncPtr;
  66. void* udp = rpt==NULL ? NULL : rpt->userPtr;
  67. _cmVOut(pfp,udp,fmt,vl);
  68. }
  69. void cmRptPrintf( cmRpt_t* rpt, const cmChar_t* fmt, ... )
  70. {
  71. va_list vl;
  72. va_start(vl,fmt);
  73. cmRptVPrintf(rpt,fmt,vl);
  74. va_end(vl);
  75. }
  76. void cmRptError( cmRpt_t* rpt, const cmChar_t* text )
  77. {
  78. cmRptPrintFunc_t pfp = rpt==NULL ? NULL : rpt->errorFuncPtr;
  79. void* udp = rpt==NULL ? NULL : rpt->userPtr;
  80. _cmOut(pfp,udp,text);
  81. }
  82. void cmRptVErrorf( cmRpt_t* rpt, const cmChar_t* fmt, va_list vl )
  83. {
  84. cmRptPrintFunc_t pfp = rpt==NULL ? NULL : rpt->errorFuncPtr;
  85. void* udp = rpt==NULL ? NULL : rpt->userPtr;
  86. _cmVOut(pfp,udp,fmt,vl);
  87. }
  88. void cmRptErrorf( cmRpt_t* rpt, const char* fmt, ... )
  89. {
  90. va_list vl;
  91. va_start(vl,fmt);
  92. cmRptVErrorf(rpt,fmt,vl);
  93. va_end(vl);
  94. }