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
1=== modified file 'plugin/query_log/file.cc'
2--- plugin/query_log/file.cc 2011-09-18 20:03:12 +0000
3+++ plugin/query_log/file.cc 2013-01-05 02:11:23 +0000
4@@ -2,6 +2,7 @@
5 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
6 *
7 * Copyright 2011 Daniel Nichter
8+ * Copyright 2013 Stewart Smith
9 *
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12@@ -19,13 +20,19 @@
13
14 #include <config.h>
15 #include "file.h"
16+#include <sstream>
17+#include <sys/types.h>
18+#include <sys/stat.h>
19+#include <fcntl.h>
20+#include <unistd.h>
21+#include <string.h>
22+#include <errno.h>
23
24 using namespace std;
25
26 QueryLoggerFile::QueryLoggerFile()
27 {
28- _fh.setf(ios::fixed, ios::floatfield);
29- _fh.precision(6);
30+ _fd= -1;
31 }
32
33 QueryLoggerFile::~QueryLoggerFile()
34@@ -35,48 +42,66 @@
35
36 bool QueryLoggerFile::logEvent(const event_t *event)
37 {
38- if (_fh.is_open())
39+ ostringstream ss;
40+ ss.setf(ios::fixed, ios::floatfield);
41+ ss.precision(6);
42+
43+ if (_fd != -1)
44 {
45- _fh << "# start_ts=" << event->ts
46- << "\n"
47- << "# session_id=" << event->session_id
48- << " query_id=" << event->query_id
49- << " rows_examined=" << event->rows_examined
50- << " rows_sent=" << event->rows_sent
51- << " tmp_tables=" << event->tmp_tables
52- << " warnings=" << event->warnings
53- << "\n"
54- << "# execution_time=" << event->execution_time
55- << " lock_time=" << event->lock_time
56- << " session_time=" << event->session_time
57- << "\n"
58- << "# error=" << event->error << "\n"
59- << "# schema=\"" << event->schema << "\"\n"
60- << event->query << ";\n#"
61- << endl;
62+ ss << "# start_ts=" << event->ts
63+ << "\n"
64+ << "# session_id=" << event->session_id
65+ << " query_id=" << event->query_id
66+ << " rows_examined=" << event->rows_examined
67+ << " rows_sent=" << event->rows_sent
68+ << " tmp_tables=" << event->tmp_tables
69+ << " warnings=" << event->warnings
70+ << "\n"
71+ << "# execution_time=" << event->execution_time
72+ << " lock_time=" << event->lock_time
73+ << " session_time=" << event->session_time
74+ << "\n"
75+ << "# error=" << event->error << "\n"
76+ << "# schema=\"" << event->schema << "\"\n"
77+ << event->query << ";\n#"
78+ << endl;
79+
80+ string logmessage= ss.str();
81+
82+ ssize_t r= write(_fd, logmessage.c_str(), logmessage.length());
83+ if (r != (ssize_t)logmessage.length())
84+ {
85+ fprintf(stderr, "query_log: Incomplete write() to log %d: %s\n",
86+ errno, strerror(errno));
87+ return true;
88+ }
89 }
90- return false; // success
91+ return false;
92 }
93
94 bool QueryLoggerFile::openLogFile(const char *file)
95 {
96 closeLogFile();
97
98- _fh.open(file, ios::app);
99- if (_fh.fail())
100- return true; // error
101+ _fd= open(file, O_CREAT|O_APPEND|O_WRONLY, 0600);
102+ if (_fd == -1)
103+ {
104+ fprintf(stderr, "query_log: Unable to lopen log file %d: %s\n",
105+ errno, strerror(errno));
106+ return true;
107+ }
108
109- return false; // success
110+ return false;
111 }
112
113 bool QueryLoggerFile::closeLogFile()
114 {
115- if (_fh.is_open())
116+ if (_fd != -1)
117 {
118- _fh.close();
119- if (_fh.fail())
120- return true; // error
121+ int r= close(_fd);
122+ if (r == -1)
123+ return true;
124 }
125
126- return false; // success
127+ return false;
128 }
129
130=== modified file 'plugin/query_log/file.h'
131--- plugin/query_log/file.h 2011-07-30 20:43:56 +0000
132+++ plugin/query_log/file.h 2013-01-05 02:11:23 +0000
133@@ -85,5 +85,5 @@
134 bool closeLogFile();
135
136 private:
137- std::ofstream _fh; ///< File handle for open log file
138+ int _fd; ///< File handle for open log file
139 };

Subscribers

People subscribed via source and target branches

to all changes: