~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/errmsg.cc

  • Committer: Jay Pipes
  • Date: 2009-09-15 21:01:42 UTC
  • mto: (1126.2.5 merge)
  • mto: This revision was merged to the branch mainline in revision 1128.
  • Revision ID: jpipes@serialcoder-20090915210142-x8mwiqn1q0vzjspp
Moves Alter_info out into its own header and source file, cleans up some related include mess in sql_lex.h, and renames Alter_info to AlterInfo.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems, Inc.
 
4
 *  Copyright (C) 2008 Sun Microsystems
5
5
 *
6
6
 *  This program is free software; you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License as published by
17
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
18
 */
19
19
 
20
 
#include "config.h"
21
 
#include "drizzled/plugin/error_message.h"
22
 
 
23
 
#include "drizzled/gettext.h"
24
 
 
25
 
#include <cstdio>
26
 
#include <algorithm>
 
20
#include <drizzled/server_includes.h>
 
21
#include <drizzled/errmsg.h>
 
22
#include <drizzled/gettext.h>
 
23
#include "drizzled/plugin/registry.h"
 
24
 
27
25
#include <vector>
28
26
 
29
 
namespace drizzled
30
 
{
31
 
 
32
 
std::vector<plugin::ErrorMessage *> all_errmsg_handler;
33
 
 
34
 
bool plugin::ErrorMessage::addPlugin(plugin::ErrorMessage *handler)
 
27
using namespace std;
 
28
 
 
29
static vector<Error_message_handler *> all_errmsg_handler;
 
30
 
 
31
static bool errmsg_has= false;
 
32
 
 
33
void add_errmsg_handler(Error_message_handler *handler)
35
34
{
36
35
  all_errmsg_handler.push_back(handler);
37
 
  return false;
38
 
}
39
 
 
40
 
void plugin::ErrorMessage::removePlugin(plugin::ErrorMessage *)
41
 
{
42
 
  all_errmsg_handler.clear();
43
 
}
44
 
 
45
 
 
46
 
class Print : public std::unary_function<plugin::ErrorMessage *, bool>
47
 
{
48
 
  error::level_t priority;
 
36
  errmsg_has= true;
 
37
}
 
38
 
 
39
void remove_errmsg_handler(Error_message_handler *handler)
 
40
{
 
41
  all_errmsg_handler.erase(find(all_errmsg_handler.begin(),
 
42
                                all_errmsg_handler.end(), handler));
 
43
}
 
44
 
 
45
 
 
46
class ErrorMessagePrint : public unary_function<Error_message_handler *, bool>
 
47
{
 
48
  Session *session;
 
49
  int priority;
49
50
  const char *format;
50
51
  va_list ap;
51
 
 
52
52
public:
53
 
  Print(error::level_t priority_arg,
54
 
        const char *format_arg, va_list ap_arg) : 
55
 
    std::unary_function<plugin::ErrorMessage *, bool>(),
 
53
  ErrorMessagePrint(Session *session_arg, int priority_arg,
 
54
                    const char *format_arg, va_list ap_arg) :
 
55
    unary_function<Error_message_handler *, bool>(), session(session_arg),
56
56
    priority(priority_arg), format(format_arg)
57
 
  {
58
 
    va_copy(ap, ap_arg);
59
 
  }
 
57
    {
 
58
      va_copy(ap, ap_arg);
 
59
    }
60
60
 
61
 
  ~Print()  { va_end(ap); }
 
61
  ~ErrorMessagePrint()  { va_end(ap); }
62
62
 
63
63
  inline result_type operator()(argument_type handler)
64
64
  {
65
 
    va_list handler_ap;
66
 
    va_copy(handler_ap, ap);
67
 
    if (handler->errmsg(priority, format, handler_ap))
 
65
    if (handler->errmsg(session, priority, format, ap))
68
66
    {
69
67
      /* we're doing the errmsg plugin api,
70
68
         so we can't trust the errmsg api to emit our error messages
74
72
      fprintf(stderr,
75
73
              _("errmsg plugin '%s' errmsg() failed"),
76
74
              handler->getName().c_str());
77
 
      va_end(handler_ap);
78
75
      return true;
79
76
    }
80
 
    va_end(handler_ap);
81
77
    return false;
82
78
  }
83
 
}; 
84
 
 
85
 
 
86
 
bool plugin::ErrorMessage::vprintf(error::level_t priority, char const *format, va_list ap)
 
79
};
 
80
 
 
81
bool errmsg_vprintf (Session *session, int priority,
 
82
                     char const *format, va_list ap)
87
83
{
88
84
 
89
 
  /* 
90
 
    Check to see if any errmsg plugin has been loaded
91
 
    if not, just fall back to emitting the message to stderr.
92
 
  */
93
 
  if (not all_errmsg_handler.size())
 
85
  /* check to see if any errmsg plugin has been loaded
 
86
     if not, just fall back to emitting the message to stderr */
 
87
  if (!errmsg_has)
94
88
  {
95
89
    /* if it turns out that the vfprintf doesnt do one single write
96
90
       (single writes are atomic), then this needs to be rewritten to
97
91
       vsprintf into a char buffer, and then write() that char buffer
98
92
       to stderr */
99
93
    vfprintf(stderr, format, ap);
100
 
    fputc('\n', stderr);
101
94
    return false;
102
95
  }
103
96
 
104
97
  /* Use find_if instead of foreach so that we can collect return codes */
105
 
  std::vector<plugin::ErrorMessage *>::iterator iter=
106
 
    std::find_if(all_errmsg_handler.begin(), all_errmsg_handler.end(),
107
 
                 Print(priority, format, ap)); 
108
 
 
 
98
  vector<Error_message_handler *>::iterator iter=
 
99
    find_if(all_errmsg_handler.begin(), all_errmsg_handler.end(),
 
100
            ErrorMessagePrint(session, priority, format, ap)); 
109
101
  /* If iter is == end() here, that means that all of the plugins returned
110
102
   * false, which in this case means they all succeeded. Since we want to 
111
103
   * return false on success, we return the value of the two being != 
113
105
  return iter != all_errmsg_handler.end();
114
106
}
115
107
 
116
 
} /* namespace drizzled */
 
108