MySQL 9.1.0
Source Code Documentation
server_logs_helpers.h
Go to the documentation of this file.
1/* Copyright (c) 2023, 2024 Oracle and/or its affiliates.
2
3This program is free software; you can redistribute it and/or modify
4it under the terms of the GNU General Public License, version 2.0,
5as published by the Free Software Foundation.
6
7This program is designed to work with certain software (including
8but not limited to OpenSSL) that is licensed under separate terms,
9as designated in a particular file or component or in included license
10documentation. The authors of MySQL hereby grant you an additional
11permission to link the program and your derivative works with the
12separately licensed software that they have either included with
13the program or referenced in the documentation.
14
15This program is distributed in the hope that it will be useful,
16but WITHOUT ANY WARRANTY; without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18GNU General Public License, version 2.0, for more details.
19
20You should have received a copy of the GNU General Public License
21along with this program; if not, write to the Free Software
22Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef TEST_SERVER_LOGS_HELPERS_INCLUDED
25#define TEST_SERVER_LOGS_HELPERS_INCLUDED
26
27#include <stdarg.h>
28#include <algorithm> // std::min
29#include <cstring>
30#include <string>
31#include "required_services.h"
32
34 public:
35 explicit FileLogger(const char *filename) : m_path(filename) {}
36
37 void write(const char *format, ...)
38#ifdef __GNUC__
39 __attribute__((format(printf, 2, 3)))
40#endif
41 {
42 // to be thread-safe each call opens the file by itself
43 FILE *outfile = fopen(m_path.c_str(), "a+");
44 if (outfile) {
45 char msg[2048];
46
47 va_list args;
48 va_start(args, format);
49 const int len = vsnprintf(msg, sizeof(msg), format, args);
50 va_end(args);
51
52 const int bytes = std::min(len, (int)(sizeof(msg) - 1));
53 auto written [[maybe_unused]] = fwrite(msg, sizeof(char), bytes, outfile);
54 (void)fclose(outfile);
55 }
56 }
57
58 private:
59 std::string m_path;
60};
61
62bool parse_log_level(const char *level, OTELLogLevel &result);
63const char *print_log_level(OTELLogLevel severity);
64
65#endif /* TEST_SERVER_TELEMETRY_HELPERS_INCLUDED */
Definition: server_logs_helpers.h:33
std::string m_path
Definition: server_logs_helpers.h:59
void write(const char *format,...)
Definition: server_logs_helpers.h:37
FileLogger(const char *filename)
Definition: server_logs_helpers.h:35
static char outfile[FN_REFLEN]
Definition: mysql.cc:231
const std::string FILE("FILE")
const char * filename
Definition: pfs_example_component_population.cc:67
OTELLogLevel
Log levels as supported by opentelemetry-cpp (+ "none"), see: api/include/opentelemetry/logs/severity...
Definition: server_telemetry_logs_client_bits.h:43
Definition: result.h:30
bool parse_log_level(const char *level, OTELLogLevel &result)
Definition: server_logs_helpers.cc:26
const char * print_log_level(OTELLogLevel severity)
Definition: server_logs_helpers.cc:40