~drizzle-trunk/drizzle/development

2311.1.3 by Daniel Nichter
Change to GPLv3 license.
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2311.1.1 by Daniel Nichter
Add query_log plugin. It's tested and documented.
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
2311.1.3 by Daniel Nichter
Change to GPLv3 license.
4
 *  Copyright 2011 Daniel Nichter
2311.1.1 by Daniel Nichter
Add query_log plugin. It's tested and documented.
5
 *
2311.1.3 by Daniel Nichter
Change to GPLv3 license.
6
 *  This program is free software: you can redistribute it and/or modify
2311.1.1 by Daniel Nichter
Add query_log plugin. It's tested and documented.
7
 *  it under the terms of the GNU General Public License as published by
2311.1.3 by Daniel Nichter
Change to GPLv3 license.
8
 *  the Free Software Foundation, either version 3 of the License, or
9
 *  (at your option) any later version.
2311.1.1 by Daniel Nichter
Add query_log plugin. It's tested and documented.
10
 *
11
 *  This program is distributed in the hope that it will be useful,
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 *  GNU General Public License for more details.
15
 *
16
 *  You should have received a copy of the GNU General Public License
2311.1.3 by Daniel Nichter
Change to GPLv3 license.
17
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
2311.1.1 by Daniel Nichter
Add query_log plugin. It's tested and documented.
18
 */
19
20
#include <config.h>
21
#include "file.h"
22
23
using namespace std;
24
25
QueryLoggerFile::QueryLoggerFile()
26
{
2311.1.8 by Daniel Nichter
Use C++ iostream instead of Boost format.
27
  _fh.setf(ios::fixed, ios::floatfield);
28
  _fh.precision(6);
2311.1.1 by Daniel Nichter
Add query_log plugin. It's tested and documented.
29
}
30
31
QueryLoggerFile::~QueryLoggerFile()
32
{
33
  closeLogFile();
34
}
35
36
bool QueryLoggerFile::logEvent(const event_t *event)
37
{
2311.1.8 by Daniel Nichter
Use C++ iostream instead of Boost format.
38
  if (_fh.is_open())
39
  {
2420.1.1 by Daniel
Make first event line start_ts=TS. Update and expand docu; fix a typo (wrong version).
40
    _fh << "# start_ts=" << event->ts
41
        << "\n"
2311.1.8 by Daniel Nichter
Use C++ iostream instead of Boost format.
42
        << "# session_id="     << event->session_id
43
        <<  " query_id="       << event->query_id
44
        <<  " rows_examined="  << event->rows_examined
45
        <<  " rows_sent="      << event->rows_sent
46
        <<  " tmp_tables="     << event->tmp_tables
47
        <<  " warnings="       << event->warnings
48
        << "\n"
49
        << "# execution_time=" << event->execution_time
50
        <<  " lock_time="      << event->lock_time
51
        <<  " session_time="   << event->session_time
52
        << "\n"
53
        << "# error=" << event->error << "\n"
54
        << "# schema=\"" << event->schema << "\"\n"
55
        << event->query << ";\n#"
56
        << endl;
57
  }
2311.1.1 by Daniel Nichter
Add query_log plugin. It's tested and documented.
58
  return false; // success
59
}
60
61
bool QueryLoggerFile::openLogFile(const char *file)
62
{
2311.1.8 by Daniel Nichter
Use C++ iostream instead of Boost format.
63
  closeLogFile();
64
65
  _fh.open(file, ios::app);
66
  if (_fh.fail())
2311.1.1 by Daniel Nichter
Add query_log plugin. It's tested and documented.
67
    return true; // error
68
69
  return false; // success
70
}
71
72
bool QueryLoggerFile::closeLogFile()
73
{
2311.1.8 by Daniel Nichter
Use C++ iostream instead of Boost format.
74
  if (_fh.is_open())
75
  {
76
    _fh.close();
77
    if (_fh.fail())
78
      return true;  // error
79
  }
80
2311.1.1 by Daniel Nichter
Add query_log plugin. It's tested and documented.
81
  return false;  // success
82
}