~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/query_log/file.cc

  • Committer: Daniel Nichter
  • Date: 2011-07-30 20:43:56 UTC
  • mto: This revision was merged to the branch mainline in revision 2387.
  • Revision ID: daniel@percona.com-20110730204356-kwkfnwl99162xwp8
Use C++ iostream instead of Boost format.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 */
19
19
 
20
20
#include <config.h>
21
 
#include <string>
22
 
#include <sys/stat.h>
23
 
#include <fcntl.h>
24
21
#include "file.h"
25
22
 
26
23
using namespace std;
27
24
 
28
25
QueryLoggerFile::QueryLoggerFile()
29
26
{
30
 
  _fd= LOG_FILE_CLOSED;
31
 
 
32
 
  // If you add something here, the number of params must match the number
33
 
  // of values pushed to the formatter in logEvent().
34
 
  _formatter.parse(
35
 
    "# %s\n"
36
 
    "# session_id=%d query_id=%d rows_examined=%d rows_sent=%d tmp_tables=%d warnings=%d\n"
37
 
    "# execution_time=%.6f lock_time=%.6f session_time=%.6f\n"
38
 
    "# error=%s\n"
39
 
    "# schema=\"%s\"\n"
40
 
    "%s;\n#\n"
41
 
  );
 
27
  _fh.setf(ios::fixed, ios::floatfield);
 
28
  _fh.precision(6);
42
29
}
43
30
 
44
31
QueryLoggerFile::~QueryLoggerFile()
48
35
 
49
36
bool QueryLoggerFile::logEvent(const event_t *event)
50
37
{
51
 
  if (_fd == LOG_FILE_CLOSED)
52
 
    return false;
53
 
 
54
 
  _formatter
55
 
    % event->ts
56
 
    % event->session_id
57
 
    % event->query_id
58
 
    % event->rows_examined
59
 
    % event->rows_sent
60
 
    % event->tmp_tables
61
 
    % event->warnings
62
 
    % event->execution_time
63
 
    % event->lock_time  // broken
64
 
    % event->session_time
65
 
    % event->error
66
 
    % event->schema
67
 
    % event->query;
68
 
  string msgbuf= _formatter.str();
69
 
 
70
 
  size_t wrv;
71
 
  wrv= write(_fd, msgbuf.c_str(), msgbuf.length());
72
 
  assert(wrv == msgbuf.length());
73
 
 
 
38
  if (_fh.is_open())
 
39
  {
 
40
    _fh << "# " << event->ts << "\n"
 
41
        << "# session_id="     << event->session_id
 
42
        <<  " query_id="       << event->query_id
 
43
        <<  " rows_examined="  << event->rows_examined
 
44
        <<  " rows_sent="      << event->rows_sent
 
45
        <<  " tmp_tables="     << event->tmp_tables
 
46
        <<  " warnings="       << event->warnings
 
47
        << "\n"
 
48
        << "# execution_time=" << event->execution_time
 
49
        <<  " lock_time="      << event->lock_time
 
50
        <<  " session_time="   << event->session_time
 
51
        << "\n"
 
52
        << "# error=" << event->error << "\n"
 
53
        << "# schema=\"" << event->schema << "\"\n"
 
54
        << event->query << ";\n#"
 
55
        << endl;
 
56
  }
74
57
  return false; // success
75
58
}
76
59
 
77
60
bool QueryLoggerFile::openLogFile(const char *file)
78
61
{
79
62
  assert(file != NULL);
80
 
 
81
 
  int new_fd= open(file, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
82
 
  if (new_fd < 0)
 
63
 
 
64
  closeLogFile();
 
65
 
 
66
  _fh.open(file, ios::app);
 
67
  if (_fh.fail())
83
68
    return true; // error
84
69
 
85
 
  closeLogFile();
86
 
  _fd= new_fd;
87
 
 
88
70
  return false; // success
89
71
}
90
72
 
91
73
bool QueryLoggerFile::closeLogFile()
92
74
{
93
 
  if (not _fd == LOG_FILE_CLOSED)
94
 
    close(_fd);  // TODO: catch errors
95
 
  _fd= LOG_FILE_CLOSED;
 
75
  if (_fh.is_open())
 
76
  {
 
77
    _fh.close();
 
78
    if (_fh.fail())
 
79
      return true;  // error
 
80
  }
 
81
 
96
82
  return false;  // success
97
83
}