PageRenderTime 54ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/src/libbson/bson/bson-error.c

https://github.com/pfriedland/rmongodb
C | 128 lines | 50 code | 16 blank | 62 comment | 7 complexity | 6f472a3d319f3de05cc45a6337a6f13b MD5 | raw file
  1. /*
  2. * Copyright 2013 MongoDB, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <stdio.h>
  17. #include <stdarg.h>
  18. #include "bson-compat.h"
  19. #include "bson-config.h"
  20. #include "bson-error.h"
  21. #include "bson-memory.h"
  22. #include "bson-string.h"
  23. #include "bson-types.h"
  24. /*
  25. *--------------------------------------------------------------------------
  26. *
  27. * bson_set_error --
  28. *
  29. * Initializes @error using the parameters specified.
  30. *
  31. * @domain is an application specific error domain which should
  32. * describe which module initiated the error. Think of this as the
  33. * exception type.
  34. *
  35. * @code is the @domain specific error code.
  36. *
  37. * @format is used to generate the format string. It uses vsnprintf()
  38. * internally so the format should match what you would use there.
  39. *
  40. * Parameters:
  41. * @error: A #bson_error_t.
  42. * @domain: The error domain.
  43. * @code: The error code.
  44. * @format: A printf style format string.
  45. *
  46. * Returns:
  47. * None.
  48. *
  49. * Side effects:
  50. * @error is initialized.
  51. *
  52. *--------------------------------------------------------------------------
  53. */
  54. void
  55. bson_set_error (bson_error_t *error, /* OUT */
  56. uint32_t domain, /* IN */
  57. uint32_t code, /* IN */
  58. const char *format, /* IN */
  59. ...) /* IN */
  60. {
  61. va_list args;
  62. if (error) {
  63. error->domain = domain;
  64. error->code = code;
  65. va_start (args, format);
  66. bson_vsnprintf (error->message, sizeof error->message, format, args);
  67. va_end (args);
  68. error->message[sizeof error->message - 1] = '\0';
  69. }
  70. }
  71. /*
  72. *--------------------------------------------------------------------------
  73. *
  74. * bson_strerror_r --
  75. *
  76. * This is a reentrant safe macro for strerror.
  77. *
  78. * The resulting string is stored in @buf and @buf is returned.
  79. *
  80. * Returns:
  81. * A pointer to a static string or @buf.
  82. *
  83. * Side effects:
  84. * None.
  85. *
  86. *--------------------------------------------------------------------------
  87. */
  88. char *
  89. bson_strerror_r (int err_code, /* IN */
  90. char *buf, /* IN */
  91. size_t buflen) /* IN */
  92. {
  93. static const char *unknown_msg = "Unknown error";
  94. char *ret = NULL;
  95. #if defined(__GNUC__) && defined(_GNU_SOURCE)
  96. ret = strerror_r (err_code, buf, buflen);
  97. #elif defined(_WIN32)
  98. if (strerror_s (buf, buflen, err_code) != 0) {
  99. ret = buf;
  100. }
  101. #else /* XSI strerror_r */
  102. if (strerror_r (err_code, buf, buflen) != 0) {
  103. ret = buf;
  104. }
  105. #endif
  106. if (!ret) {
  107. memcpy (buf, unknown_msg, MIN (buflen, strlen (unknown_msg)));
  108. buf [buflen - 1] = '\0';
  109. ret = buf;
  110. }
  111. return buf;
  112. }