~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/errmsg_notify/errmsg_notify.cc

  • Committer: Padraig O'Sullivan
  • Date: 2010-12-03 19:16:09 UTC
  • mto: (1975.1.2 build)
  • mto: This revision was merged to the branch mainline in revision 1976.
  • Revision ID: osullivan.padraig@gmail.com-20101203191609-7s81iwt33vrgmz9v
Some re-factoring based on feedback from Monty on IRC.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2010 Monty Taylor
 
5
 *
 
6
 *  This program is free software; you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License as published by
 
8
 *  the Free Software Foundation; version 2 of the License.
 
9
 *
 
10
 *  This program is distributed in the hope that it will be useful,
 
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *  GNU General Public License for more details.
 
14
 *
 
15
 *  You should have received a copy of the GNU General Public License
 
16
 *  along with this program; if not, write to the Free Software
 
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
18
 */
 
19
 
 
20
#include "config.h"
 
21
 
 
22
#include <cstdio>  /* for vsnprintf */
 
23
#include <stdarg.h>  /* for va_list */
 
24
#include <unistd.h>  /* for write(2) */
 
25
#include <iostream>
 
26
#include <libnotifymm.h>
 
27
#include <boost/scoped_ptr.hpp>
 
28
 
 
29
#include <string>
 
30
#include <vector>
 
31
 
 
32
#include <drizzled/plugin/error_message.h>
 
33
#include <drizzled/gettext.h>
 
34
#include <drizzled/plugin.h>
 
35
 
 
36
 
 
37
/* todo, make this dynamic as needed */
 
38
#define MAX_MSG_LEN 8192
 
39
 
 
40
using namespace drizzled;
 
41
using namespace std;
 
42
 
 
43
 
 
44
class Error_message_notify : public plugin::ErrorMessage
 
45
{
 
46
  vector<string> errmsg_tags;
 
47
public:
 
48
  Error_message_notify()
 
49
   : plugin::ErrorMessage("Error_message_notify"),
 
50
     errmsg_tags()
 
51
  {
 
52
    errmsg_tags.push_back("Unknown");
 
53
    errmsg_tags.push_back("Debug");
 
54
    errmsg_tags.push_back("Info");
 
55
    errmsg_tags.push_back("Warn");
 
56
    errmsg_tags.push_back("Error");
 
57
  }
 
58
 
 
59
  virtual bool errmsg(Session *, int priority, const char *format, va_list ap)
 
60
  {
 
61
    char msgbuf[MAX_MSG_LEN];
 
62
    int prv;
 
63
 
 
64
    prv= vsnprintf(msgbuf, MAX_MSG_LEN, format, ap);
 
65
    if (prv < 0) return true;
 
66
 
 
67
    Notify::Notification n(errmsg_tags[priority].c_str(), msgbuf);
 
68
    /**
 
69
     * @TODO: Make this timeout a system variable
 
70
     */
 
71
    n.set_timeout(3000);
 
72
 
 
73
#ifdef GLIBMM_EXCEPTIONS_ENABLED
 
74
    try
 
75
    {
 
76
      if (!n.show())
 
77
#else
 
78
      boost::scoped_ptr<Glib::Error> error;
 
79
      if (!n.show(error))
 
80
#endif
 
81
      {
 
82
        fprintf(stderr, _("Failed to send error message to libnotify\n"));
 
83
        return true;
 
84
      }
 
85
#ifdef GLIBMM_EXCEPTIONS_ENABLED
 
86
     }
 
87
     catch (Glib::Error& err)
 
88
     {
 
89
        cerr << err.what() << endl;
 
90
     }
 
91
#endif
 
92
 
 
93
  return false;
 
94
  
 
95
  }
 
96
};
 
97
 
 
98
static Error_message_notify *handler= NULL;
 
99
static int plugin_init(module::Context &context)
 
100
{
 
101
  Notify::init("Drizzled");
 
102
  handler= new Error_message_notify();
 
103
  context.add(handler);
 
104
 
 
105
  return 0;
 
106
}
 
107
 
 
108
DRIZZLE_PLUGIN(plugin_init, NULL, NULL);