/
log.cc
2214 lines (1845 loc) · 61.9 KB
/
log.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2000, 2023, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is also distributed with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have included with MySQL.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
/**
@file
@brief
logging of commands
@todo
Abort logging when we get an error in reading or writing log files
*/
#include "log.h"
#include "sql_audit.h" // mysql_audit_general_log
#include "sql_base.h" // close_log_table
#include "sql_class.h" // THD
#include "sql_parse.h" // sql_command_flags
#include "sql_time.h" // calc_time_from_sec
#include "table.h" // TABLE_FIELD_TYPE
#include "pfs_file_provider.h"
#include "mysql/psi/mysql_file.h"
#include <string>
#include <sstream>
#ifdef _WIN32
#include <message.h>
#else
#include <syslog.h>
#endif
using std::min;
using std::max;
#ifndef _WIN32
static int log_syslog_facility= 0;
#endif
static char *log_syslog_ident = NULL;
static bool log_syslog_enabled = false;
enum enum_slow_query_log_table_field
{
SQLT_FIELD_START_TIME = 0,
SQLT_FIELD_USER_HOST,
SQLT_FIELD_QUERY_TIME,
SQLT_FIELD_LOCK_TIME,
SQLT_FIELD_ROWS_SENT,
SQLT_FIELD_ROWS_EXAMINED,
SQLT_FIELD_DATABASE,
SQLT_FIELD_LAST_INSERT_ID,
SQLT_FIELD_INSERT_ID,
SQLT_FIELD_SERVER_ID,
SQLT_FIELD_SQL_TEXT,
SQLT_FIELD_THREAD_ID,
SQLT_FIELD_COUNT
};
static const TABLE_FIELD_TYPE slow_query_log_table_fields[SQLT_FIELD_COUNT] =
{
{
{ C_STRING_WITH_LEN("start_time") },
{ C_STRING_WITH_LEN("timestamp(6)") },
{ NULL, 0 }
},
{
{ C_STRING_WITH_LEN("user_host") },
{ C_STRING_WITH_LEN("mediumtext") },
{ C_STRING_WITH_LEN("utf8") }
},
{
{ C_STRING_WITH_LEN("query_time") },
{ C_STRING_WITH_LEN("time(6)") },
{ NULL, 0 }
},
{
{ C_STRING_WITH_LEN("lock_time") },
{ C_STRING_WITH_LEN("time(6)") },
{ NULL, 0 }
},
{
{ C_STRING_WITH_LEN("rows_sent") },
{ C_STRING_WITH_LEN("int(11)") },
{ NULL, 0 }
},
{
{ C_STRING_WITH_LEN("rows_examined") },
{ C_STRING_WITH_LEN("int(11)") },
{ NULL, 0 }
},
{
{ C_STRING_WITH_LEN("db") },
{ C_STRING_WITH_LEN("varchar(512)") },
{ C_STRING_WITH_LEN("utf8") }
},
{
{ C_STRING_WITH_LEN("last_insert_id") },
{ C_STRING_WITH_LEN("int(11)") },
{ NULL, 0 }
},
{
{ C_STRING_WITH_LEN("insert_id") },
{ C_STRING_WITH_LEN("int(11)") },
{ NULL, 0 }
},
{
{ C_STRING_WITH_LEN("server_id") },
{ C_STRING_WITH_LEN("int(10) unsigned") },
{ NULL, 0 }
},
{
{ C_STRING_WITH_LEN("sql_text") },
{ C_STRING_WITH_LEN("mediumblob") },
{ NULL, 0 }
},
{
{ C_STRING_WITH_LEN("thread_id") },
{ C_STRING_WITH_LEN("bigint(21) unsigned") },
{ NULL, 0 }
}
};
static const TABLE_FIELD_DEF
slow_query_log_table_def= {SQLT_FIELD_COUNT, slow_query_log_table_fields};
enum enum_general_log_table_field
{
GLT_FIELD_EVENT_TIME = 0,
GLT_FIELD_USER_HOST,
GLT_FIELD_THREAD_ID,
GLT_FIELD_SERVER_ID,
GLT_FIELD_COMMAND_TYPE,
GLT_FIELD_ARGUMENT,
GLT_FIELD_COUNT
};
static const TABLE_FIELD_TYPE general_log_table_fields[GLT_FIELD_COUNT] =
{
{
{ C_STRING_WITH_LEN("event_time") },
{ C_STRING_WITH_LEN("timestamp(6)") },
{ NULL, 0 }
},
{
{ C_STRING_WITH_LEN("user_host") },
{ C_STRING_WITH_LEN("mediumtext") },
{ C_STRING_WITH_LEN("utf8") }
},
{
{ C_STRING_WITH_LEN("thread_id") },
{ C_STRING_WITH_LEN("bigint(21) unsigned") },
{ NULL, 0 }
},
{
{ C_STRING_WITH_LEN("server_id") },
{ C_STRING_WITH_LEN("int(10) unsigned") },
{ NULL, 0 }
},
{
{ C_STRING_WITH_LEN("command_type") },
{ C_STRING_WITH_LEN("varchar(64)") },
{ C_STRING_WITH_LEN("utf8") }
},
{
{ C_STRING_WITH_LEN("argument") },
{ C_STRING_WITH_LEN("mediumblob") },
{ NULL, 0 }
}
};
static const TABLE_FIELD_DEF
general_log_table_def= {GLT_FIELD_COUNT, general_log_table_fields};
class Query_log_table_intact : public Table_check_intact
{
protected:
void report_error(uint, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
error_log_print(ERROR_LEVEL, fmt, args);
va_end(args);
}
};
/** In case of an error, a message is printed to the error log. */
static Query_log_table_intact log_table_intact;
/**
Silence all errors and warnings reported when performing a write
to a log table.
Errors and warnings are not reported to the client or SQL exception
handlers, so that the presence of logging does not interfere and affect
the logic of an application.
*/
class Silence_log_table_errors : public Internal_error_handler
{
char m_message[MYSQL_ERRMSG_SIZE];
public:
Silence_log_table_errors() { m_message[0]= '\0'; }
virtual bool handle_condition(THD *thd,
uint sql_errno,
const char* sql_state,
Sql_condition::enum_severity_level *level,
const char* msg)
{
strmake(m_message, msg, sizeof(m_message)-1);
return true;
}
const char *message() const { return m_message; }
};
#ifndef _WIN32
/**
On being handed a syslog facility name tries to look it up.
If successful, fills in a struct with the facility ID and
the facility's canonical name.
@param f [in] Name of the faciltiy we're trying to look up.
Lookup is case-insensitive; leading "log_" is ignored.
@param rsf [out] A buffer in which to return the ID and canonical name.
@return
false No errors; buffer contains valid result
true Something went wrong, no valid result set returned
*/
bool log_syslog_find_facility(char *f, SYSLOG_FACILITY *rsf)
{
if (!f || !*f || !rsf)
return true;
if (strncasecmp(f, "log_", 4) == 0)
f+= 4;
for(int i= 0; syslog_facility[i].name != NULL; i++)
if (!strcasecmp(f, syslog_facility[i].name))
{
rsf->id= syslog_facility[i].id;
rsf->name= syslog_facility[i].name;
return false;
}
return true;
}
#endif
/**
Close POSIX syslog / Windows EventLog.
*/
static void log_syslog_close()
{
if (log_syslog_enabled)
{
log_syslog_enabled= false;
my_closelog();
}
}
/**
Update syslog / Windows EventLog characteristics (on/off,
identify-as, log-PIDs, facility, ...) from global variables.
@return
false No errors; all characteristics updated.
true Unable to update characteristics.
*/
bool log_syslog_update_settings()
{
const char *prefix;
if (!opt_log_syslog_enable && log_syslog_enabled)
{
log_syslog_close();
return false;
}
#ifndef _WIN32
{
/*
make facility
*/
SYSLOG_FACILITY rsf = { LOG_DAEMON, "daemon" };
assert(opt_log_syslog_facility != NULL);
if (log_syslog_find_facility(opt_log_syslog_facility, &rsf))
{
log_syslog_find_facility((char *) "daemon", &rsf);
sql_print_warning("failed to set syslog facility to \"%s\", "
"setting to \"%s\" (%d) instead.",
opt_log_syslog_facility, rsf.name, rsf.id);
rsf.name= NULL;
}
log_syslog_facility= rsf.id;
// If NaN, set to the canonical form (cut "log_", fix case)
if ((rsf.name != NULL) && (strcmp(opt_log_syslog_facility, rsf.name) != 0))
strcpy(opt_log_syslog_facility, rsf.name);
}
/*
Logs historically have subtly different names, to meet each platform's
conventions -- "mysqld" on unix (via mysqld_safe), and "MySQL" for the
Win NT EventLog.
*/
prefix= "mysqld";
#else
prefix= "MySQL";
#endif
// tag must not contain directory separators
if ((opt_log_syslog_tag != NULL) &&
(strchr(opt_log_syslog_tag, FN_LIBCHAR) != NULL))
return true;
if (opt_log_syslog_enable)
{
/*
make ident
*/
char *ident= NULL;
if ((opt_log_syslog_tag == NULL) ||
(*opt_log_syslog_tag == '\0'))
ident= my_strdup(PSI_NOT_INSTRUMENTED, prefix, MYF(0));
else
{
size_t l= 6 + 1 + 1 + strlen(opt_log_syslog_tag);
ident= (char *) my_malloc(PSI_NOT_INSTRUMENTED, l, MYF(0));
if (ident)
my_snprintf(ident, l, "%s-%s", prefix, opt_log_syslog_tag);
}
// if we succeeded in making an ident, replace the old one
if (ident)
{
char *i= log_syslog_ident;
log_syslog_ident= ident;
if (i)
my_free(i);
}
else
return true;
log_syslog_close();
int ret;
ret= my_openlog(log_syslog_ident,
#ifndef _WIN32
opt_log_syslog_include_pid ? MY_SYSLOG_PIDS : 0,
log_syslog_facility
#else
0, 0
#endif
);
if (ret == -1)
return true;
log_syslog_enabled= true;
if (ret == -2)
{
my_syslog(system_charset_info, ERROR_LEVEL, "could not update log settings!");
return true;
}
}
return false;
}
/**
Stop using syslog / EventLog. Call as late as possible.
*/
void log_syslog_exit(void)
{
log_syslog_close();
if (log_syslog_ident != NULL)
{
my_free(log_syslog_ident);
log_syslog_ident= NULL;
}
}
/**
Start using syslog / EventLog.
@return
true could not open syslog / EventLog with the requested characteristics
false no issues encountered
*/
bool log_syslog_init(void)
{
if (log_syslog_update_settings())
{
#ifdef _WIN32
const char *l = "Windows EventLog";
#else
const char *l = "syslog";
#endif
sql_print_error("Cannot open %s; check privileges, or start server with --log_syslog=0", l);
return true;
}
return false;
}
static void ull2timeval(ulonglong utime, struct timeval *tv)
{
assert(tv != NULL);
assert(utime > 0); /* should hold true in this context */
tv->tv_sec= static_cast<long>(utime / 1000000);
tv->tv_usec=utime % 1000000;
}
/**
Make and return an ISO 8601 / RFC 3339 compliant timestamp.
Heeds log_timestamps.
@param buf A buffer of at least 26 bytes to store the timestamp in
(19 + tzinfo tail + \0)
@param seconds Seconds since the epoch, or 0 for "now"
@return length of timestamp (excluding \0)
*/
int make_iso8601_timestamp(char *buf, ulonglong utime)
{
struct tm my_tm;
char tzinfo[7]="Z"; // max 6 chars plus \0
size_t len;
time_t seconds;
if (utime == 0)
utime= my_micro_time();
seconds= utime / 1000000;
utime = utime % 1000000;
if (opt_log_timestamps == 0)
gmtime_r(&seconds, &my_tm);
else
{
localtime_r(&seconds, &my_tm);
#ifdef HAVE_TM_GMTOFF
/*
The field tm_gmtoff is the offset (in seconds) of the time represented
from UTC, with positive values indicating east of the Prime Meridian.
Originally a BSDism, this is also supported in glibc, so this should
cover the majority of our platforms.
*/
long tim= -my_tm.tm_gmtoff;
#else
/*
Work this out "manually".
*/
struct tm my_gm;
long tim, gm;
gmtime_r(&seconds, &my_gm);
gm = (my_gm.tm_sec + 60 * (my_gm.tm_min + 60 * my_gm.tm_hour));
tim = (my_tm.tm_sec + 60 * (my_tm.tm_min + 60 * my_tm.tm_hour));
tim = gm - tim;
#endif
char dir= '-';
if (tim < 0)
{
dir= '+';
tim= -tim;
}
my_snprintf(tzinfo, sizeof(tzinfo), "%c%02d:%02d",
dir, (int) (tim / (60 * 60)), (int) ((tim / 60) % 60));
}
len= my_snprintf(buf, iso8601_size, "%04d-%02d-%02dT%02d:%02d:%02d.%06lu%s",
my_tm.tm_year + 1900,
my_tm.tm_mon + 1,
my_tm.tm_mday,
my_tm.tm_hour,
my_tm.tm_min,
my_tm.tm_sec,
(unsigned long) utime,
tzinfo);
return min<int>(len, iso8601_size - 1);
}
bool is_valid_log_name(const char *name, size_t len)
{
if (len > 3)
{
const char *tail= name + len - 4;
if (my_strcasecmp(system_charset_info, tail, ".ini") == 0 ||
my_strcasecmp(system_charset_info, tail, ".cnf") == 0)
{
return false;
}
}
return true;
}
/**
Get the real log file name, and possibly reopen file.
The implementation is platform dependent due to differences in how this is
supported:
On Windows, we get the actual path based on the file descriptor. This path is
copied into the supplied buffer. The 'file' parameter is returned without
re-opening.
On other platforms, we use realpath() to get the path with symbolic links
expanded. Then, we close the file, and reopen the real path using the
O_NOFOLLOW flag. This will reject folowing symbolic links.
@param file File descriptor.
@param log_file_key Key for P_S instrumentation.
@param open_flags Flags to use for opening the file.
@param opened_file_name Name of the open fd.
@param [out] real_file_name Buffer for actual name of the fd.
@retval file descriptor to open file with 'real_file_name', or '-1'
in case of errors.
*/
static File mysql_file_real_name_reopen(File file,
#ifdef HAVE_PSI_INTERFACE
PSI_file_key log_file_key,
#endif
int open_flags,
const char *opened_file_name,
char *real_file_name)
{
assert(file);
assert(opened_file_name);
assert(real_file_name);
#ifdef _WIN32
/* On Windows, O_NOFOLLOW is not supported. Verify real path from fd. */
DWORD real_length= GetFinalPathNameByHandle(my_get_osfhandle(file),
real_file_name,
FN_REFLEN,
FILE_NAME_OPENED);
/* May ret 0 if e.g. on a ramdisk. Ignore - return open file and name. */
if (real_length == 0)
{
strcpy(real_file_name, opened_file_name);
return file;
}
if (real_length > FN_REFLEN)
{
mysql_file_close(file, MYF(0));
return -1;
}
return file;
#else
/* On *nix, get realpath, open realpath with O_NOFOLLOW. */
if (realpath(opened_file_name, real_file_name) == NULL)
{
(void) mysql_file_close(file, MYF(0));
return -1;
}
if (mysql_file_close(file, MYF(0)))
return -1;
/* Make sure the real path is not too long. */
if (strlen(real_file_name) > FN_REFLEN)
return -1;
return mysql_file_open(log_file_key, real_file_name,
open_flags | O_NOFOLLOW,
MYF(MY_WME));
#endif //_WIN32
}
bool File_query_log::open()
{
File file= -1;
my_off_t pos= 0;
const char *log_name= NULL;
char buff[FN_REFLEN];
MY_STAT f_stat;
DBUG_ENTER("File_query_log::open");
if (m_log_type == QUERY_LOG_SLOW)
log_name= opt_slow_logname;
else if (m_log_type == QUERY_LOG_GENERAL)
log_name= opt_general_logname;
else
assert(false);
assert(log_name && log_name[0]);
write_error= false;
if (!(name= my_strdup(key_memory_File_query_log_name, log_name, MYF(MY_WME))))
{
name= const_cast<char *>(log_name); // for the error message
goto err;
}
fn_format(log_file_name, name, mysql_data_home, "", 4);
/* File is regular writable file */
if (my_stat(log_file_name, &f_stat, MYF(0)) && !MY_S_ISREG(f_stat.st_mode))
goto err;
db[0]= 0;
/* First, open the file to make sure it exists. */
if ((file= mysql_file_open(m_log_file_key,
log_file_name,
O_CREAT | O_BINARY | O_WRONLY | O_APPEND,
MYF(MY_WME))) < 0)
goto err;
#ifdef _WIN32
char real_log_file_name[FN_REFLEN];
#else
/* File name must have room for PATH_MAX. Checked against F_REFLEN later. */
char real_log_file_name[PATH_MAX];
#endif // _Win32
/* Reopen and get real path. */
if ((file= mysql_file_real_name_reopen(file,
#ifdef HAVE_PSI_INTERFACE
m_log_file_key,
#endif
O_CREAT | O_BINARY | O_WRONLY | O_APPEND,
log_file_name, real_log_file_name)) < 0)
goto err;
if (!is_valid_log_name(real_log_file_name, strlen(real_log_file_name)))
{
sql_print_error("Invalid log file name after expanding symlinks: '%s'",
real_log_file_name);
goto err;
}
if ((pos= mysql_file_tell(file, MYF(MY_WME))) == MY_FILEPOS_ERROR)
{
if (my_errno() == ESPIPE)
pos= 0;
else
goto err;
}
if (init_io_cache(&log_file, file, IO_SIZE, WRITE_CACHE, pos, 0,
MYF(MY_WME | MY_NABP)))
goto err;
{
char *end;
size_t len=my_snprintf(buff, sizeof(buff), "%s, Version: %s (%s). "
#ifdef EMBEDDED_LIBRARY
"embedded library\n",
my_progname, server_version, MYSQL_COMPILATION_COMMENT
#elif _WIN32
"started with:\nTCP Port: %d, Named Pipe: %s\n",
my_progname, server_version, MYSQL_COMPILATION_COMMENT,
mysqld_port, mysqld_unix_port
#else
"started with:\nTcp port: %d Unix socket: %s\n",
my_progname, server_version, MYSQL_COMPILATION_COMMENT,
mysqld_port, mysqld_unix_port
#endif
);
end= my_stpncpy(buff + len, "Time Id Command Argument\n",
sizeof(buff) - len);
if (my_b_write(&log_file, (uchar*) buff, (uint) (end-buff)) ||
flush_io_cache(&log_file))
goto err;
}
log_open= true;
DBUG_RETURN(false);
err:
char log_open_file_error_message[96]= "";
if (strcmp(opt_slow_logname, name) == 0)
{
strcpy(log_open_file_error_message, "either restart the query logging "
"by using \"SET GLOBAL SLOW_QUERY_LOG=ON\" or");
}
else if (strcmp(opt_general_logname, name) == 0)
{
strcpy(log_open_file_error_message, "either restart the query logging "
"by using \"SET GLOBAL GENERAL_LOG=ON\" or");
}
char errbuf[MYSYS_STRERROR_SIZE];
sql_print_error("Could not use %s for logging (error %d - %s). "
"Turning logging off for the server process. "
"To turn it on again: fix the cause, "
"then %s restart the MySQL server.", name, errno,
my_strerror(errbuf, sizeof(errbuf), errno),
log_open_file_error_message);
if (file >= 0)
mysql_file_close(file, MYF(0));
end_io_cache(&log_file);
my_free(name);
name= NULL;
log_open= false;
DBUG_RETURN(true);
}
void File_query_log::close()
{
DBUG_ENTER("File_query_log::close");
if (!is_open())
DBUG_VOID_RETURN;
end_io_cache(&log_file);
if (mysql_file_sync(log_file.file, MYF(MY_WME)))
check_and_print_write_error();
if (mysql_file_close(log_file.file, MYF(MY_WME)))
check_and_print_write_error();
log_open= false;
my_free(name);
name= NULL;
DBUG_VOID_RETURN;
}
void File_query_log::check_and_print_write_error()
{
if (!write_error)
{
char errbuf[MYSYS_STRERROR_SIZE];
write_error= true;
sql_print_error(ER_DEFAULT(ER_ERROR_ON_WRITE), name, errno,
my_strerror(errbuf, sizeof(errbuf), errno));
}
}
bool File_query_log::write_general(ulonglong event_utime,
const char *user_host,
size_t user_host_len,
my_thread_id thread_id,
const char *command_type,
size_t command_type_len,
const char *sql_text,
size_t sql_text_len)
{
char buff[32];
size_t length= 0;
mysql_mutex_lock(&LOCK_log);
assert(is_open());
/* Note that my_b_write() assumes it knows the length for this */
char local_time_buff[iso8601_size];
int time_buff_len= make_iso8601_timestamp(local_time_buff, event_utime);
if (my_b_write(&log_file, (uchar*) local_time_buff, time_buff_len))
goto err;
if (my_b_write(&log_file, (uchar*) "\t", 1))
goto err;
length= my_snprintf(buff, 32, "%5u ", thread_id);
if (my_b_write(&log_file, (uchar*) buff, length))
goto err;
if (my_b_write(&log_file, (uchar*) command_type, command_type_len))
goto err;
if (my_b_write(&log_file, (uchar*) "\t", 1))
goto err;
/* sql_text */
if (my_b_write(&log_file, (uchar*) sql_text, sql_text_len))
goto err;
if (my_b_write(&log_file, (uchar*) "\n", 1) ||
flush_io_cache(&log_file))
goto err;
mysql_mutex_unlock(&LOCK_log);
return false;
err:
check_and_print_write_error();
mysql_mutex_unlock(&LOCK_log);
return true;
}
bool File_query_log::write_slow(THD *thd, ulonglong current_utime,
ulonglong query_start_arg,
const char *user_host,
size_t user_host_len, ulonglong query_utime,
ulonglong lock_utime, bool is_command,
const char *sql_text, size_t sql_text_len)
{
char buff[80], *end;
char query_time_buff[22+7], lock_time_buff[22+7];
size_t buff_len;
end= buff;
mysql_mutex_lock(&LOCK_log);
assert(is_open());
if (!(specialflag & SPECIAL_SHORT_LOG_FORMAT))
{
char my_timestamp[iso8601_size];
make_iso8601_timestamp(my_timestamp, current_utime);
buff_len= my_snprintf(buff, sizeof buff,
"# Time: %s\n", my_timestamp);
/* Note that my_b_write() assumes it knows the length for this */
if (my_b_write(&log_file, (uchar*) buff, buff_len))
goto err;
buff_len= my_snprintf(buff, 32, "%5u", thd->thread_id());
if (my_b_printf(&log_file, "# User@Host: %s Id: %s\n", user_host, buff)
== (uint) -1)
goto err;
}
/* For slow query log */
sprintf(query_time_buff, "%.6f", ulonglong2double(query_utime)/1000000.0);
sprintf(lock_time_buff, "%.6f", ulonglong2double(lock_utime)/1000000.0);
if (my_b_printf(&log_file,
"# Query_time: %s Lock_time: %s"
" Rows_sent: %lu Rows_examined: %lu\n",
query_time_buff, lock_time_buff,
(ulong) thd->get_sent_row_count(),
(ulong) thd->get_examined_row_count()) == (uint) -1)
goto err;
if (thd->db().str && strcmp(thd->db().str, db))
{ // Database changed
if (my_b_printf(&log_file,"use %s;\n",thd->db().str) == (uint) -1)
goto err;
my_stpcpy(db,thd->db().str);
}
if (thd->stmt_depends_on_first_successful_insert_id_in_prev_stmt)
{
end=my_stpcpy(end, ",last_insert_id=");
end=longlong10_to_str((longlong)
thd->first_successful_insert_id_in_prev_stmt_for_binlog,
end, -10);
}
// Save value if we do an insert.
if (thd->auto_inc_intervals_in_cur_stmt_for_binlog.nb_elements() > 0)
{
if (!(specialflag & SPECIAL_SHORT_LOG_FORMAT))
{
end=my_stpcpy(end,",insert_id=");
end=longlong10_to_str((longlong)
thd->auto_inc_intervals_in_cur_stmt_for_binlog.minimum(),
end, -10);
}
}
/*
This info used to show up randomly, depending on whether the query
checked the query start time or not. now we always write current
timestamp to the slow log
*/
end= my_stpcpy(end, ",timestamp=");
end= int10_to_str((long) (current_utime / 1000000), end, 10);
if (end != buff)
{
*end++=';';
*end='\n';
if (my_b_write(&log_file, (uchar*) "SET ", 4) ||
my_b_write(&log_file, (uchar*) buff + 1, (uint) (end-buff)))
goto err;
}
if (is_command)
{
end= strxmov(buff, "# administrator command: ", NullS);
buff_len= (ulong) (end - buff);
DBUG_EXECUTE_IF("simulate_slow_log_write_error",
{DBUG_SET("+d,simulate_file_write_error");});
if (my_b_write(&log_file, (uchar*) buff, buff_len))
goto err;
}
if (my_b_write(&log_file, (uchar*) sql_text, sql_text_len) ||
my_b_write(&log_file, (uchar*) ";\n",2) ||
flush_io_cache(&log_file))
goto err;
mysql_mutex_unlock(&LOCK_log);
return false;
err:
check_and_print_write_error();
mysql_mutex_unlock(&LOCK_log);
return true;
}
bool Log_to_csv_event_handler::log_general(THD *thd, ulonglong event_utime,
const char *user_host,
size_t user_host_len,
my_thread_id thread_id,
const char *command_type,
size_t command_type_len,
const char *sql_text,
size_t sql_text_len,
const CHARSET_INFO *client_cs)
{
TABLE *table= NULL;
bool result= true;
bool need_close= false;
bool need_rnd_end= false;
uint field_index;
struct timeval tv;
/*
CSV uses TIME_to_timestamp() internally if table needs to be repaired
which will set thd->time_zone_used
*/
bool save_time_zone_used= thd->time_zone_used;
ulonglong save_thd_options= thd->variables.option_bits;
thd->variables.option_bits&= ~OPTION_BIN_LOG;
TABLE_LIST table_list;
table_list.init_one_table(MYSQL_SCHEMA_NAME.str, MYSQL_SCHEMA_NAME.length,
GENERAL_LOG_NAME.str, GENERAL_LOG_NAME.length,
GENERAL_LOG_NAME.str,
TL_WRITE_CONCURRENT_INSERT);
/*
1) open_log_table generates an error if the
table can not be opened or is corrupted.
2) "INSERT INTO general_log" can generate warning sometimes.
Suppress these warnings and errors, they can't be dealt with
properly anyway.
QQ: this problem needs to be studied in more detail.
Comment this 2 lines and run "cast.test" to see what's happening.
*/
Silence_log_table_errors error_handler;
thd->push_internal_handler(&error_handler);
Open_tables_backup open_tables_backup;
if (!(table= open_log_table(thd, &table_list, &open_tables_backup)))
goto err;
need_close= true;