drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
file.cc
1/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 *
4 * Copyright 2011 Daniel Nichter
5 * Copyright 2013 Stewart Smith
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include <config.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>
30
31using namespace std;
32
33QueryLoggerFile::QueryLoggerFile()
34{
35 _fd= -1;
36}
37
38QueryLoggerFile::~QueryLoggerFile()
39{
41}
42
44{
45 ostringstream ss;
46 ss.setf(ios::fixed, ios::floatfield);
47 ss.precision(6);
48
49 if (_fd != -1)
50 {
51 ss << "# start_ts=" << event->ts
52 << "\n"
53 << "# session_id=" << event->session_id
54 << " query_id=" << event->query_id
55 << " rows_examined=" << event->rows_examined
56 << " rows_sent=" << event->rows_sent
57 << " tmp_tables=" << event->tmp_tables
58 << " warnings=" << event->warnings
59 << "\n"
60 << "# execution_time=" << event->execution_time
61 << " lock_time=" << event->lock_time
62 << " session_time=" << event->session_time
63 << "\n"
64 << "# error=" << event->error << "\n"
65 << "# schema=\"" << event->schema << "\"\n"
66 << event->query << ";\n#"
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 }
78 }
79 return false;
80}
81
82bool QueryLoggerFile::openLogFile(const char *file)
83{
85
86 _fd= open(file, O_CREAT|O_APPEND|O_WRONLY, 0600);
87 if (_fd == -1)
88 {
89 fprintf(stderr, "query_log: Unable to lopen log file %d: %s\n",
90 errno, strerror(errno));
91 return true;
92 }
93
94 return false;
95}
96
98{
99 if (_fd != -1)
100 {
101 int r= close(_fd);
102 if (r == -1)
103 return true;
104 }
105
106 return false;
107}
bool openLogFile(const char *file)
Open new log file, close old log file if successful.
Definition file.cc:82
bool closeLogFile()
Close the log file.
Definition file.cc:97
int _fd
File handle for open log file.
Definition file.h:88
bool logEvent(const event_t *event)
Format and write the event to the log file.
Definition file.cc:43
STL namespace.