libcm is a C development framework with an emphasis on audio signal processing applications.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

cmErr.c 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include "cmPrefix.h"
  2. #include "cmGlobal.h"
  3. #include "cmRpt.h"
  4. #include "cmErr.h"
  5. void cmErrSetup( cmErr_t* err, cmRpt_t* rpt, const cmChar_t* label )
  6. {
  7. err->rpt = rpt;
  8. err->label = label;
  9. err->rc = cmOkRC;
  10. }
  11. void cmErrClone( cmErr_t* dstErr, const cmErr_t* srcErr )
  12. { memcpy(dstErr,srcErr,sizeof(*dstErr)); }
  13. void _cmErrVMsg(cmErr_t* err, bool warnFl, cmRC_t rc, const cmChar_t* fmt, va_list vl )
  14. {
  15. if( err->rpt == NULL )
  16. return;
  17. const cmChar_t* hdrFmt = warnFl ? "%s warning: " : "%s error: ";
  18. const cmChar_t* codeFmt = " (RC:%i)";
  19. int n0 = snprintf( NULL,0,hdrFmt,cmStringNullGuard(err->label));
  20. int n1 = vsnprintf(NULL,0,fmt,vl);
  21. int n2 = snprintf( NULL,0,codeFmt,rc);
  22. int n = n0+n1+n2+1;
  23. cmChar_t s[n];
  24. n0 = snprintf(s,n,hdrFmt,cmStringNullGuard(err->label));
  25. n0 += vsnprintf(s+n0,n-n0,fmt,vl);
  26. n0 += snprintf(s+n0,n-n0,codeFmt,rc);
  27. assert(n0 <= n );
  28. cmRptErrorf(err->rpt,"%s\n",s);
  29. }
  30. void _cmErrMsg( cmErr_t* err, bool warnFl, cmRC_t rc, const cmChar_t* fmt, ... )
  31. {
  32. va_list vl;
  33. va_start(vl,fmt);
  34. _cmErrVMsg(err,warnFl,rc,fmt,vl);
  35. va_end(vl);
  36. }
  37. cmRC_t cmErrVMsg(cmErr_t* err, cmRC_t rc, const cmChar_t* fmt, va_list vl )
  38. {
  39. cmErrSetRC(err,rc);
  40. _cmErrVMsg(err,false,rc,fmt,vl);
  41. return rc;
  42. }
  43. cmRC_t cmErrMsg( cmErr_t* err, cmRC_t rc, const cmChar_t* fmt, ... )
  44. {
  45. va_list vl;
  46. va_start(vl,fmt);
  47. rc = cmErrVMsg(err,rc,fmt,vl);
  48. va_end(vl);
  49. return rc;
  50. }
  51. void _cmErrSysVMsg(cmErr_t* err, bool warnFl, cmRC_t rc, cmSysErrCode_t sysErrCode, const cmChar_t* fmt, va_list vl )
  52. {
  53. const char* sysFmt = "\n System Error: (code:%i) %s.";
  54. int n0 = snprintf(NULL,0,sysFmt,sysErrCode,strerror(sysErrCode));
  55. int n1 = vsnprintf(NULL,0,fmt,vl);
  56. int n = n0 + n1 + 1;
  57. cmChar_t s[n0+n1+1];
  58. n0 = snprintf(s,n,sysFmt,sysErrCode,strerror(sysErrCode));
  59. n0 += vsnprintf(s+n0,n-n0,fmt,vl);
  60. assert( n0 <= n );
  61. _cmErrMsg(err,warnFl,rc,s);
  62. }
  63. cmRC_t cmErrVSysMsg(cmErr_t* err, cmRC_t rc, cmSysErrCode_t sysErrCode, const cmChar_t* fmt, va_list vl )
  64. {
  65. cmErrSetRC(err,rc);
  66. _cmErrSysVMsg(err,false,rc,sysErrCode,fmt,vl);
  67. return rc;
  68. }
  69. cmRC_t cmErrSysMsg( cmErr_t* err, cmRC_t rc, cmSysErrCode_t sysErrCode, const cmChar_t* fmt, ... )
  70. {
  71. va_list vl;
  72. va_start(vl,fmt);
  73. rc = cmErrVSysMsg(err,rc,sysErrCode,fmt,vl);
  74. va_end(vl);
  75. return rc;
  76. }
  77. cmRC_t cmErrWarnVMsg(cmErr_t* err, cmRC_t rc, const cmChar_t* fmt, va_list vl )
  78. {
  79. _cmErrVMsg(err,true,rc,fmt,vl);
  80. err->warnRC = rc;
  81. return rc;
  82. }
  83. cmRC_t cmErrWarnMsg( cmErr_t* err, cmRC_t rc, const cmChar_t* fmt, ... )
  84. {
  85. va_list vl;
  86. va_start(vl,fmt);
  87. rc = cmErrWarnVMsg(err,rc,fmt,vl);
  88. va_end(vl);
  89. return rc;
  90. }
  91. cmRC_t cmErrWarnVSysMsg(cmErr_t* err, cmRC_t rc, cmSysErrCode_t sysErrCode, const cmChar_t* fmt, va_list vl )
  92. {
  93. _cmErrSysVMsg(err,true,rc,sysErrCode,fmt,vl);
  94. err->warnRC = rc;
  95. return rc;
  96. }
  97. cmRC_t cmErrWarnSysMsg( cmErr_t* err, cmRC_t rc, cmSysErrCode_t sysErrCode, const cmChar_t* fmt, ... )
  98. {
  99. va_list vl;
  100. va_start(vl,fmt);
  101. rc = cmErrWarnVSysMsg(err,rc,sysErrCode,fmt,vl);
  102. va_end(vl);
  103. return rc;
  104. }
  105. cmRC_t cmErrLastRC( cmErr_t* err )
  106. { return err->rc; }
  107. cmRC_t cmErrSetRC( cmErr_t* err, cmRC_t rc )
  108. {
  109. cmRC_t retVal = err->rc;
  110. err->rc = rc;
  111. return retVal;
  112. }
  113. cmRC_t cmErrClearRC( cmErr_t* err )
  114. { return cmErrSetRC(err,cmOkRC); }