Drizzled Public API Documentation

errmsg_stderr.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 Mark Atwood
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 #include <config.h>
00021 #include <drizzled/plugin/error_message.h>
00022 #include <drizzled/gettext.h>
00023 #include <drizzled/plugin.h>
00024 
00025 #include <stdio.h>  /* for vsnprintf */
00026 #include <stdarg.h>  /* for va_list */
00027 #include <unistd.h>  /* for write(2) */
00028 
00029 using namespace drizzled;
00030 
00031 /* todo, make this dynamic as needed */
00032 #define MAX_MSG_LEN 8192
00033 
00034 class Error_message_stderr : public plugin::ErrorMessage
00035 {
00036 public:
00037   Error_message_stderr()
00038    : plugin::ErrorMessage("Error_message_stderr") {}
00039   virtual bool errmsg(error::level_t , const char *format, va_list ap)
00040   {
00041     char msgbuf[MAX_MSG_LEN];
00042     int prv, wrv;
00043 
00044     prv= vsnprintf(msgbuf, MAX_MSG_LEN, format, ap);
00045     if (prv < 0) return true;
00046 
00047     /* a single write has a OS level thread lock
00048        so there is no need to have mutexes guarding this write,
00049     */
00050     wrv= write(fileno(stderr), msgbuf, prv);
00051     fputc('\n', stderr);
00052     if ((wrv < 0) || (wrv != prv))
00053       return true;
00054 
00055     return false;
00056   }
00057 };
00058 
00059 static Error_message_stderr *handler= NULL;
00060 static int errmsg_stderr_plugin_init(module::Context &context)
00061 {
00062   handler= new Error_message_stderr();
00063   context.add(handler);
00064 
00065   return 0;
00066 }
00067 
00068 DRIZZLE_DECLARE_PLUGIN
00069 {
00070   DRIZZLE_VERSION_ID,
00071   "errmsg_stderr",
00072   "0.1",
00073   "Mark Atwood <mark@fallenpegasus.com>",
00074   N_("Error Messages to stderr"),
00075   PLUGIN_LICENSE_GPL,
00076   errmsg_stderr_plugin_init,
00077   NULL, /* depends */
00078   NULL
00079 }
00080 DRIZZLE_DECLARE_PLUGIN_END;