Drizzled Public API Documentation

errmsg_print.cc

00001 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2008 Sun Microsystems, Inc.
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; version 2 of the License.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018  */
00019 
00020 /* This file and these functions are a stopgap until all the
00021    sql_print_foo() function calls are replaced with calls to
00022    errmsg_printf()
00023 */
00024 
00025 #include <config.h>
00026 
00027 #include <drizzled/definitions.h>
00028 #include <drizzled/errmsg_print.h>
00029 #include <drizzled/plugin/error_message.h>
00030 
00031 #include <cerrno>
00032 #include <cstring>
00033 
00034 namespace drizzled
00035 {
00036 
00037 void sql_perror(const char *message)
00038 {
00039   char *errmsg_ptr;
00040   char errmsg[STRERROR_MAX];
00041   errmsg[0]= 0;
00042 
00043 #ifdef STRERROR_R_CHAR_P
00044   errmsg_ptr= strerror_r(errno, errmsg, sizeof(errmsg));
00045 #else
00046   strerror_r(errno, errmsg, sizeof(errmsg));
00047   errmsg_ptr= errmsg;
00048 #endif
00049 
00050   errmsg_printf(error::ERROR, "%s: %s\n", message, errmsg);
00051 
00052 }
00053 
00054 // @todo Cap the size of message.
00055 void sql_perror(const std::string &message)
00056 {
00057   static std::string empty;
00058   sql_perror(message, empty);
00059 }
00060 
00061 // @todo Cap the size of message/extra.
00062 void sql_perror(std::string message, const std::string &extra)
00063 {
00064   char *errmsg_ptr;
00065   char errmsg[STRERROR_MAX];
00066   errmsg[0]= 0;
00067 
00068 #ifdef STRERROR_R_CHAR_P
00069   errmsg_ptr= strerror_r(errno, errmsg, sizeof(errmsg));
00070 #else
00071   strerror_r(errno, errmsg, sizeof(errmsg));
00072   errmsg_ptr= errmsg;
00073 #endif
00074 
00075   if (not extra.empty())
00076   {
00077     if (message.at(message.size()) != ' ')
00078       message+= " ";
00079 
00080     message+= "'";
00081     message+= extra;
00082     message+= "'";
00083   }
00084 
00085   errmsg_printf(error::ERROR, "%s: %s\n", message.c_str(), errmsg);
00086 }
00087 
00088 bool errmsg_printf (error::level_t priority, char const *format, ...)
00089 {
00090   bool rv;
00091   va_list args;
00092   va_start(args, format);
00093   rv= plugin::ErrorMessage::vprintf(priority, format, args);
00094   va_end(args);
00095   return rv;
00096 }
00097 
00098 } /* namespace drizzled */