PageRenderTime 58ms CodeModel.GetById 41ms app.highlight 11ms 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
 17
 18#include <stdio.h>
 19#include <stdarg.h>
 20
 21#include "bson-compat.h"
 22#include "bson-config.h"
 23#include "bson-error.h"
 24#include "bson-memory.h"
 25#include "bson-string.h"
 26#include "bson-types.h"
 27
 28
 29/*
 30 *--------------------------------------------------------------------------
 31 *
 32 * bson_set_error --
 33 *
 34 *       Initializes @error using the parameters specified.
 35 *
 36 *       @domain is an application specific error domain which should
 37 *       describe which module initiated the error. Think of this as the
 38 *       exception type.
 39 *
 40 *       @code is the @domain specific error code.
 41 *
 42 *       @format is used to generate the format string. It uses vsnprintf()
 43 *       internally so the format should match what you would use there.
 44 *
 45 * Parameters:
 46 *       @error: A #bson_error_t.
 47 *       @domain: The error domain.
 48 *       @code: The error code.
 49 *       @format: A printf style format string.
 50 *
 51 * Returns:
 52 *       None.
 53 *
 54 * Side effects:
 55 *       @error is initialized.
 56 *
 57 *--------------------------------------------------------------------------
 58 */
 59
 60void
 61bson_set_error (bson_error_t *error,  /* OUT */
 62                uint32_t      domain, /* IN */
 63                uint32_t      code,   /* IN */
 64                const char   *format, /* IN */
 65                ...)                  /* IN */
 66{
 67   va_list args;
 68
 69   if (error) {
 70      error->domain = domain;
 71      error->code = code;
 72
 73      va_start (args, format);
 74      bson_vsnprintf (error->message, sizeof error->message, format, args);
 75      va_end (args);
 76
 77      error->message[sizeof error->message - 1] = '\0';
 78   }
 79}
 80
 81
 82/*
 83 *--------------------------------------------------------------------------
 84 *
 85 * bson_strerror_r --
 86 *
 87 *       This is a reentrant safe macro for strerror.
 88 *
 89 *       The resulting string is stored in @buf and @buf is returned.
 90 *
 91 * Returns:
 92 *       A pointer to a static string or @buf.
 93 *
 94 * Side effects:
 95 *       None.
 96 *
 97 *--------------------------------------------------------------------------
 98 */
 99
100char *
101bson_strerror_r (int     err_code,  /* IN */
102                 char   *buf,       /* IN */
103                 size_t  buflen)    /* IN */
104{
105   static const char *unknown_msg = "Unknown error";
106   char *ret = NULL;
107
108#if defined(__GNUC__) && defined(_GNU_SOURCE)
109   ret = strerror_r (err_code, buf, buflen);
110#elif defined(_WIN32)
111   if (strerror_s (buf, buflen, err_code) != 0) {
112      ret = buf;
113   }
114#else /* XSI strerror_r */
115   if (strerror_r (err_code, buf, buflen) != 0) {
116      ret = buf;
117   }
118#endif
119
120   if (!ret) {
121      memcpy (buf, unknown_msg, MIN (buflen, strlen (unknown_msg)));
122      buf [buflen - 1] = '\0';
123      ret = buf;
124   }
125
126   return buf;
127}
128