libcm is a C development framework with an emphasis on audio signal processing applications.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

cmErr.c 3.4KB

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