~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/logging_query/logging_query.cc

Removed dead variable, sorted authors file.

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 char* logging_query_filename= NULL;
11
 
 
12
 
static int fd= -1;
13
 
 
14
 
// copied from drizzled/sql_parse.cc
15
 
 
16
 
const LEX_STRING command_name[]={
17
 
  { C_STRING_WITH_LEN("Sleep") },
18
 
  { C_STRING_WITH_LEN("Quit") },
19
 
  { C_STRING_WITH_LEN("InitDB") },
20
 
  { C_STRING_WITH_LEN("Query") },
21
 
  { C_STRING_WITH_LEN("FieldList") },
22
 
  { C_STRING_WITH_LEN("CreateDB") },
23
 
  { C_STRING_WITH_LEN("DropDB") },
24
 
  { C_STRING_WITH_LEN("Refresh") },
25
 
  { C_STRING_WITH_LEN("Shutdown") },
26
 
  { C_STRING_WITH_LEN("Processlist") },
27
 
  { C_STRING_WITH_LEN("Connect") },
28
 
  { C_STRING_WITH_LEN("Kill") },
29
 
  { C_STRING_WITH_LEN("Ping") },
30
 
  { C_STRING_WITH_LEN("Time") },
31
 
  { C_STRING_WITH_LEN("ChangeUser") },
32
 
  { C_STRING_WITH_LEN("BinlogDump") },
33
 
  { C_STRING_WITH_LEN("ConnectOut") },
34
 
  { C_STRING_WITH_LEN("RegisterSlave") },
35
 
  { C_STRING_WITH_LEN("SetOption") },
36
 
  { C_STRING_WITH_LEN("Daemon") },
37
 
  { C_STRING_WITH_LEN("Error") }
38
 
};
39
 
 
40
 
 
41
 
bool logging_query_func_pre (THD *thd)
42
 
{
43
 
  char msgbuf[MAX_MSG_LEN];
44
 
  int msgbuf_len= 0;
45
 
  int wrv;
46
 
 
47
 
  if (fd < 0) 
48
 
    return false;
49
 
 
50
 
  assert(thd != NULL);
51
 
 
52
 
  msgbuf_len=
53
 
    snprintf(msgbuf, MAX_MSG_LEN,
54
 
             "log bgn thread_id=%ld query_id=%ld command=%.*s"
55
 
             " db=\"%.*s\" query=\"%.*s\"\n",
56
 
             (unsigned long) thd->thread_id,
57
 
             (unsigned long) thd->query_id,
58
 
             (uint32_t)command_name[thd->command].length, command_name[thd->command].str,
59
 
             thd->db_length, thd->db,
60
 
             thd->query_length, thd->query);
61
 
  /* a single write has a OS level thread lock
62
 
     so there is no need to have mutexes guarding this write,
63
 
  */
64
 
  wrv= write(fd, msgbuf, msgbuf_len);
65
 
  assert(wrv == msgbuf_len);
66
 
 
67
 
  return false;
68
 
}
69
 
 
70
 
bool logging_query_func_post (THD *thd)
71
 
{
72
 
  char msgbuf[MAX_MSG_LEN];
73
 
  int msgbuf_len= 0;
74
 
  int wrv;
75
 
 
76
 
  if (fd < 0) return false;
77
 
 
78
 
  assert(thd != NULL);
79
 
 
80
 
  msgbuf_len=
81
 
    snprintf(msgbuf, MAX_MSG_LEN,
82
 
             "log end thread_id=%ld query_id=%ld command=%.*s"
83
 
             " rows.sent=%ld rows.exam=%u\n",
84
 
             (unsigned long) thd->thread_id, 
85
 
             (unsigned long) thd->query_id,
86
 
             (uint32_t)command_name[thd->command].length, command_name[thd->command].str,
87
 
             (unsigned long) thd->sent_row_count,
88
 
             (uint32_t) thd->examined_row_count);
89
 
  /* a single write has a OS level thread lock
90
 
     so there is no need to have mutexes guarding this write,
91
 
  */
92
 
  wrv= write(fd, msgbuf, msgbuf_len);
93
 
  assert(wrv == msgbuf_len);
94
 
 
95
 
 
96
 
  return false;
97
 
}
98
 
 
99
 
static int logging_query_plugin_init(void *p)
100
 
{
101
 
  logging_t *l= (logging_t *) p;
102
 
 
103
 
  if (logging_query_filename == NULL)
104
 
  {
105
 
    /* no destination filename was specified via system variables
106
 
       return now, dont set the callback pointers 
107
 
    */
108
 
    return 0;
109
 
  }
110
 
 
111
 
  fd= open(logging_query_filename, O_WRONLY | O_APPEND | O_CREAT);
112
 
  if (fd < 0) 
113
 
  {
114
 
    fprintf(stderr, "fail open fn=%s er=%s\n",
115
 
            logging_query_filename,
116
 
            strerror(errno));
117
 
 
118
 
    /* we should return an error here, so the plugin doesnt load
119
 
       but this causes Drizzle to crash
120
 
       so until that is fixed,
121
 
       just return a success,
122
 
       but leave the function pointers as NULL and the fd as -1
123
 
    */
124
 
    return 0;
125
 
  }
126
 
 
127
 
  l->logging_pre= logging_query_func_pre;
128
 
  l->logging_post= logging_query_func_post;
129
 
 
130
 
  return 0;
131
 
}
132
 
 
133
 
static int logging_query_plugin_deinit(void *p)
134
 
{
135
 
  logging_st *l= (logging_st *) p;
136
 
 
137
 
  if (fd >= 0) 
138
 
  {
139
 
    close(fd);
140
 
    fd= -1;
141
 
  }
142
 
 
143
 
  l->logging_pre= NULL;
144
 
  l->logging_post= NULL;
145
 
 
146
 
  return 0;
147
 
}
148
 
 
149
 
static DRIZZLE_SYSVAR_STR(filename, logging_query_filename,
150
 
  PLUGIN_VAR_READONLY,
151
 
  "File to log queries to.",
152
 
  NULL, NULL, NULL);
153
 
 
154
 
static struct st_mysql_sys_var* logging_query_system_variables[]= {
155
 
  DRIZZLE_SYSVAR(filename),
156
 
  NULL
157
 
};
158
 
 
159
 
mysql_declare_plugin(logging_query)
160
 
{
161
 
  DRIZZLE_LOGGER_PLUGIN,
162
 
  "logging_query",
163
 
  "0.1",
164
 
  "Mark Atwood <mark@fallenpegasus.com>",
165
 
  "Log queries",
166
 
  PLUGIN_LICENSE_GPL,
167
 
  logging_query_plugin_init,
168
 
  logging_query_plugin_deinit,
169
 
  NULL,   /* status variables */
170
 
  logging_query_system_variables,
171
 
  NULL
172
 
}
173
 
mysql_declare_plugin_end;