~mordred/drizzle/codestyle

« back to all changes in this revision

Viewing changes to plugin/logging_query/logging_query.cc

  • Committer: Brian Aker
  • Date: 2008-10-03 17:17:25 UTC
  • mfrom: (383.6.7 pluglog)
  • Revision ID: brian@tangent.org-20081003171725-ijf4hf0zm87kaszw
Merging Mark's logging work.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* drizzle/plugin/logging_query/logging_query.cc */
 
2
 
 
3
/* need to define DRIZZLE_SERVER to get inside the THD */
 
4
#define DRIZZLE_SERVER 1
 
5
#include <drizzled/server_includes.h>
 
6
#include <drizzled/plugin_logging.h>
 
7
 
 
8
#define MAX_MSG_LEN (32*1024)
 
9
 
 
10
static int fd= -1;
 
11
 
 
12
// copied from drizzled/sql_parse.cc
 
13
 
 
14
const LEX_STRING command_name[]={
 
15
  { C_STRING_WITH_LEN("Sleep") },
 
16
  { C_STRING_WITH_LEN("Quit") },
 
17
  { C_STRING_WITH_LEN("InitDB") },
 
18
  { C_STRING_WITH_LEN("Query") },
 
19
  { C_STRING_WITH_LEN("FieldList") },
 
20
  { C_STRING_WITH_LEN("CreateDB") },
 
21
  { C_STRING_WITH_LEN("DropDB") },
 
22
  { C_STRING_WITH_LEN("Refresh") },
 
23
  { C_STRING_WITH_LEN("Shutdown") },
 
24
  { C_STRING_WITH_LEN("Processlist") },
 
25
  { C_STRING_WITH_LEN("Connect") },
 
26
  { C_STRING_WITH_LEN("Kill") },
 
27
  { C_STRING_WITH_LEN("Ping") },
 
28
  { C_STRING_WITH_LEN("Time") },
 
29
  { C_STRING_WITH_LEN("ChangeUser") },
 
30
  { C_STRING_WITH_LEN("BinlogDump") },
 
31
  { C_STRING_WITH_LEN("ConnectOut") },
 
32
  { C_STRING_WITH_LEN("RegisterSlave") },
 
33
  { C_STRING_WITH_LEN("SetOption") },
 
34
  { C_STRING_WITH_LEN("Daemon") },
 
35
  { C_STRING_WITH_LEN("Error") }
 
36
};
 
37
 
 
38
 
 
39
bool logging_query_func_pre (THD *thd)
 
40
{
 
41
  char msgbuf[MAX_MSG_LEN];
 
42
  int msgbuf_len = 0;
 
43
  int wrv;
 
44
 
 
45
  assert(thd != NULL);
 
46
  assert(fd > 0);
 
47
 
 
48
  msgbuf_len=
 
49
    snprintf(msgbuf, MAX_MSG_LEN,
 
50
             "log bgn thread_id=%ld query_id=%ld command=%.*s"
 
51
             " db=\"%.*s\" query=\"%.*s\"\n",
 
52
             (unsigned long) thd->thread_id,
 
53
             (unsigned long) thd->query_id,
 
54
             (uint32_t)command_name[thd->command].length, command_name[thd->command].str,
 
55
             thd->db_length, thd->db,
 
56
             thd->query_length, thd->query);
 
57
  wrv= write(fd, msgbuf, msgbuf_len);
 
58
  assert(wrv == msgbuf_len);
 
59
 
 
60
  return false;
 
61
}
 
62
 
 
63
bool logging_query_func_post (THD *thd)
 
64
{
 
65
  char msgbuf[MAX_MSG_LEN];
 
66
  int msgbuf_len = 0;
 
67
  int wrv;
 
68
 
 
69
  assert(thd != NULL);
 
70
  assert(fd > 0);
 
71
 
 
72
  msgbuf_len=
 
73
    snprintf(msgbuf, MAX_MSG_LEN,
 
74
             "log end thread_id=%ld query_id=%ld command=%.*s"
 
75
             " utime=%u rows.sent=%ld rows.exam=%u\n",
 
76
             (unsigned long) thd->thread_id, 
 
77
             (unsigned long) thd->query_id,
 
78
             (uint32_t)command_name[thd->command].length, command_name[thd->command].str,
 
79
             (uint32_t)(thd->current_utime() - thd->start_utime),
 
80
             (unsigned long) thd->sent_row_count,
 
81
             (uint32_t) thd->examined_row_count);
 
82
  wrv= write(fd, msgbuf, msgbuf_len);
 
83
  assert(wrv == msgbuf_len);
 
84
 
 
85
  // some other interesting things in the THD
 
86
  // thd->enable_slow_log
 
87
 
 
88
  return false;
 
89
}
 
90
 
 
91
static int logging_query_plugin_init(void *p)
 
92
{
 
93
  logging_t *l= (logging_t *) p;
 
94
 
 
95
  l->logging_pre= logging_query_func_pre;
 
96
  l->logging_post= logging_query_func_post;
 
97
 
 
98
  fd= open("/tmp/drizzle.log", O_WRONLY | O_APPEND);
 
99
  if (fd < 0) {
 
100
    fprintf(stderr,
 
101
            "MRA fail open /tmp/drizzle.log fd=%d er=%s\n",
 
102
            fd, strerror(errno));
 
103
    return fd;
 
104
  }
 
105
 
 
106
  /* need to do something better with the fd */
 
107
 
 
108
  return 0;
 
109
}
 
110
 
 
111
static int logging_query_plugin_deinit(void *p)
 
112
{
 
113
  logging_st *l= (logging_st *) p;
 
114
 
 
115
  close(fd);
 
116
 
 
117
  l->logging_pre= NULL;
 
118
  l->logging_post= NULL;
 
119
 
 
120
  return 0;
 
121
}
 
122
 
 
123
mysql_declare_plugin(logging_query)
 
124
{
 
125
  DRIZZLE_LOGGER_PLUGIN,
 
126
  "logging_query",
 
127
  "0.1",
 
128
  "Mark Atwood <mark@fallenpegasus.com>",
 
129
  "Log queries",
 
130
  PLUGIN_LICENSE_GPL,
 
131
  logging_query_plugin_init,
 
132
  logging_query_plugin_deinit,
 
133
  NULL,   /* status variables */
 
134
  NULL,   /* system variables */
 
135
  NULL    /* config options */
 
136
}
 
137
mysql_declare_plugin_end;