Drizzled Public API Documentation

unireg.h
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; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019  */
00020 
00021 /*  Extra functions used by unireg library */
00022 
00023 #pragma once
00024 
00025 #include <drizzled/visibility.h>
00026 #include <sstream>
00027 
00028 namespace drizzled
00029 {
00030 
00031 void unireg_exit() __attribute__((noreturn));
00032 DRIZZLED_API void unireg_actual_abort(const char *file, int line, const char *func, const std::string& message) __attribute__((noreturn));
00033 void unireg_startup_finished();
00034 
00035 namespace stream {
00036 
00037 namespace detail {
00038 
00039 template<class Ch, class Tr, class A>
00040   class _unireg {
00041   private:
00042 
00043   public:
00044     typedef std::basic_ostringstream<Ch, Tr, A> stream_buffer;
00045 
00046   public:
00047     void operator()(const stream_buffer &s, const char *filename, int line, const char *func)
00048     {
00049       unireg_actual_abort(filename, line, func, s.str());
00050     }
00051   };
00052 
00053 template<template <class Ch, class Tr, class A> class OutputPolicy, class Ch = char, class Tr = std::char_traits<Ch>, class A = std::allocator<Ch> >
00054   class log {
00055   private:
00056     typedef OutputPolicy<Ch, Tr, A> output_policy;
00057     const char *_filename;
00058     int _line_number;
00059     const char *_func;
00060 
00061   public:
00062     log() :
00063       _filename(NULL),
00064       _line_number(0),
00065       _func(NULL)
00066     { }
00067 
00068     void set_filename(const char *filename, int line_number, const char *func)
00069     {
00070       _filename= filename;
00071       _line_number= line_number;
00072       _func= func;
00073     }
00074 
00075     ~log()
00076     {
00077       output_policy()(arg, _filename, _line_number, _func);
00078     }
00079 
00080   public:
00081     template<class T>
00082       log &operator<<(const T &x)
00083       {
00084         arg << x;
00085         return *this;
00086       }
00087 
00088   private:
00089     typename output_policy::stream_buffer arg;
00090   };
00091 } // namespace detail
00092 
00093 class _unireg : public detail::log<detail::_unireg> {
00094 public:
00095   _unireg(const char *filename, int line_number, const char *func)
00096   {
00097     set_filename(filename, line_number, func);
00098   }
00099 };
00100 
00101 } // namespace stream
00102 
00103 #define unireg_abort stream::_unireg(__FILE__, __LINE__, __func__)
00104 
00105 } /* namespace drizzled */