Merge lp:~stewart/drizzle/bug1001199 into lp:drizzle

Proposed by Stewart Smith
Status: Merged
Approved by: Brian Aker
Approved revision: 2614
Merged at revision: 2615
Proposed branch: lp:~stewart/drizzle/bug1001199
Merge into: lp:drizzle
Diff against target: 139 lines (+56/-31)
2 files modified
plugin/query_log/file.cc (+55/-30)
plugin/query_log/file.h (+1/-1)
To merge this branch: bzr merge lp:~stewart/drizzle/bug1001199
Reviewer Review Type Date Requested Status
Drizzle Trunk Pending
Review via email: mp+142002@code.launchpad.net

Description of the change

query_log wasn't thread safe, fix that.

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'plugin/query_log/file.cc'
--- plugin/query_log/file.cc 2011-09-18 20:03:12 +0000
+++ plugin/query_log/file.cc 2013-01-05 02:11:23 +0000
@@ -2,6 +2,7 @@
2 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:2 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 *3 *
4 * Copyright 2011 Daniel Nichter4 * Copyright 2011 Daniel Nichter
5 * Copyright 2013 Stewart Smith
5 *6 *
6 * This program is free software: you can redistribute it and/or modify7 * 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 by8 * it under the terms of the GNU General Public License as published by
@@ -19,13 +20,19 @@
1920
20#include <config.h>21#include <config.h>
21#include "file.h"22#include "file.h"
23#include <sstream>
24#include <sys/types.h>
25#include <sys/stat.h>
26#include <fcntl.h>
27#include <unistd.h>
28#include <string.h>
29#include <errno.h>
2230
23using namespace std;31using namespace std;
2432
25QueryLoggerFile::QueryLoggerFile()33QueryLoggerFile::QueryLoggerFile()
26{34{
27 _fh.setf(ios::fixed, ios::floatfield);35 _fd= -1;
28 _fh.precision(6);
29}36}
3037
31QueryLoggerFile::~QueryLoggerFile()38QueryLoggerFile::~QueryLoggerFile()
@@ -35,48 +42,66 @@
3542
36bool QueryLoggerFile::logEvent(const event_t *event)43bool QueryLoggerFile::logEvent(const event_t *event)
37{44{
38 if (_fh.is_open())45 ostringstream ss;
46 ss.setf(ios::fixed, ios::floatfield);
47 ss.precision(6);
48
49 if (_fd != -1)
39 {50 {
40 _fh << "# start_ts=" << event->ts51 ss << "# start_ts=" << event->ts
41 << "\n"52 << "\n"
42 << "# session_id=" << event->session_id53 << "# session_id=" << event->session_id
43 << " query_id=" << event->query_id54 << " query_id=" << event->query_id
44 << " rows_examined=" << event->rows_examined55 << " rows_examined=" << event->rows_examined
45 << " rows_sent=" << event->rows_sent56 << " rows_sent=" << event->rows_sent
46 << " tmp_tables=" << event->tmp_tables57 << " tmp_tables=" << event->tmp_tables
47 << " warnings=" << event->warnings58 << " warnings=" << event->warnings
48 << "\n"59 << "\n"
49 << "# execution_time=" << event->execution_time60 << "# execution_time=" << event->execution_time
50 << " lock_time=" << event->lock_time61 << " lock_time=" << event->lock_time
51 << " session_time=" << event->session_time62 << " session_time=" << event->session_time
52 << "\n"63 << "\n"
53 << "# error=" << event->error << "\n"64 << "# error=" << event->error << "\n"
54 << "# schema=\"" << event->schema << "\"\n"65 << "# schema=\"" << event->schema << "\"\n"
55 << event->query << ";\n#"66 << event->query << ";\n#"
56 << endl;67 << endl;
68
69 string logmessage= ss.str();
70
71 ssize_t r= write(_fd, logmessage.c_str(), logmessage.length());
72 if (r != (ssize_t)logmessage.length())
73 {
74 fprintf(stderr, "query_log: Incomplete write() to log %d: %s\n",
75 errno, strerror(errno));
76 return true;
77 }
57 }78 }
58 return false; // success79 return false;
59}80}
6081
61bool QueryLoggerFile::openLogFile(const char *file)82bool QueryLoggerFile::openLogFile(const char *file)
62{83{
63 closeLogFile();84 closeLogFile();
6485
65 _fh.open(file, ios::app);86 _fd= open(file, O_CREAT|O_APPEND|O_WRONLY, 0600);
66 if (_fh.fail())87 if (_fd == -1)
67 return true; // error88 {
89 fprintf(stderr, "query_log: Unable to lopen log file %d: %s\n",
90 errno, strerror(errno));
91 return true;
92 }
6893
69 return false; // success94 return false;
70}95}
7196
72bool QueryLoggerFile::closeLogFile()97bool QueryLoggerFile::closeLogFile()
73{98{
74 if (_fh.is_open())99 if (_fd != -1)
75 {100 {
76 _fh.close();101 int r= close(_fd);
77 if (_fh.fail())102 if (r == -1)
78 return true; // error103 return true;
79 }104 }
80105
81 return false; // success106 return false;
82}107}
83108
=== modified file 'plugin/query_log/file.h'
--- plugin/query_log/file.h 2011-07-30 20:43:56 +0000
+++ plugin/query_log/file.h 2013-01-05 02:11:23 +0000
@@ -85,5 +85,5 @@
85 bool closeLogFile();85 bool closeLogFile();
8686
87private:87private:
88 std::ofstream _fh; ///< File handle for open log file88 int _fd; ///< File handle for open log file
89};89};

Subscribers

People subscribed via source and target branches

to all changes: