~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/errmsg_notify/errmsg_notify.cc

  • Committer: Brian Aker
  • Date: 2010-01-20 22:32:33 UTC
  • mfrom: (1271.1.5 libnotify-errmsg)
  • Revision ID: brian@gaz-20100120223233-0znraf0mhnbh2bew
Merge Monty

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 <libnotifymm.h>
 
26
 
 
27
#include <string>
 
28
#include <vector>
 
29
 
 
30
#include <drizzled/plugin/error_message.h>
 
31
#include <drizzled/gettext.h>
 
32
#include <drizzled/plugin.h>
 
33
#include <drizzled/plugin/registry.h>
 
34
 
 
35
 
 
36
/* todo, make this dynamic as needed */
 
37
#define MAX_MSG_LEN 8192
 
38
 
 
39
using namespace drizzled;
 
40
using namespace std;
 
41
 
 
42
 
 
43
class Error_message_notify : public plugin::ErrorMessage
 
44
{
 
45
  vector<string> errmsg_tags;
 
46
public:
 
47
  Error_message_notify()
 
48
   : plugin::ErrorMessage("Error_message_notify"),
 
49
     errmsg_tags()
 
50
  {
 
51
    errmsg_tags.push_back("Unknown");
 
52
    errmsg_tags.push_back("Debug");
 
53
    errmsg_tags.push_back("Info");
 
54
    errmsg_tags.push_back("Warn");
 
55
    errmsg_tags.push_back("Error");
 
56
  }
 
57
 
 
58
  virtual bool errmsg(Session *, int priority, const char *format, va_list ap)
 
59
  {
 
60
    char msgbuf[MAX_MSG_LEN];
 
61
    int prv;
 
62
 
 
63
    prv= vsnprintf(msgbuf, MAX_MSG_LEN, format, ap);
 
64
    if (prv < 0) return true;
 
65
 
 
66
    Notify::Notification n(errmsg_tags[priority].c_str(), msgbuf);
 
67
    /**
 
68
     * @TODO: Make this timeout a system variable
 
69
     */
 
70
    n.set_timeout(3000);
 
71
 
 
72
#ifdef GLIBMM_EXCEPTIONS_ENABLED
 
73
    try
 
74
    {
 
75
      if (!n.show())
 
76
#else
 
77
      auto_ptr<Glib::Error> error;
 
78
      if (!n.show(error))
 
79
#endif
 
80
      {
 
81
        fprintf(stderr, _("Failed to send error message to libnotify\n"));
 
82
        return true;
 
83
      }
 
84
#ifdef GLIBMM_EXCEPTIONS_ENABLED
 
85
     }
 
86
     catch (Glib::Error& err)
 
87
     {
 
88
        fprintf(stderr, err.what().c_str());
 
89
     }
 
90
#endif
 
91
 
 
92
  return false;
 
93
  
 
94
  }
 
95
};
 
96
 
 
97
static Error_message_notify *handler= NULL;
 
98
static int plugin_init(plugin::Registry &registry)
 
99
{
 
100
  Notify::init("Drizzled");
 
101
  handler= new Error_message_notify();
 
102
  registry.add(handler);
 
103
 
 
104
  return 0;
 
105
}
 
106
 
 
107
static int plugin_deinit(plugin::Registry &registry)
 
108
{
 
109
 
 
110
  if (handler)
 
111
  {
 
112
    registry.remove(handler);
 
113
    delete handler;
 
114
  }
 
115
  return 0;
 
116
}
 
117
 
 
118
DRIZZLE_PLUGIN(plugin_init, plugin_deinit, NULL, NULL);