Move string_printf to maxbase
Can be used in tests.
This commit is contained in:
@ -137,15 +137,6 @@ inline std::vector<std::string> strtok(std::string str, const char* delim)
|
|||||||
return rval;
|
return rval;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Format parameters to a string. Uses printf-formatting.
|
|
||||||
*
|
|
||||||
* @param format Format string
|
|
||||||
* @param ... Items to convert according to format string
|
|
||||||
* @return The result string
|
|
||||||
*/
|
|
||||||
std::string string_printf(const char* format, ...) mxb_attribute((format (printf, 1, 2)));
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @class CloserTraits utils.hh <maxscale/utils.hh>
|
* @class CloserTraits utils.hh <maxscale/utils.hh>
|
||||||
*
|
*
|
||||||
|
@ -29,4 +29,14 @@ namespace maxbase
|
|||||||
* @return Value as a human readable size e.g. 5.01MiB
|
* @return Value as a human readable size e.g. 5.01MiB
|
||||||
*/
|
*/
|
||||||
std::string to_binary_size(int64_t size);
|
std::string to_binary_size(int64_t size);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Format parameters to a string. Uses printf-formatting.
|
||||||
|
*
|
||||||
|
* @param format Format string
|
||||||
|
* @param ... Items to convert according to format string
|
||||||
|
* @return The result string
|
||||||
|
*/
|
||||||
|
std::string string_printf(const char* format, ...) mxb_attribute((format (printf, 1, 2)));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,8 @@
|
|||||||
|
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
#include <maxbase/assert.h>
|
||||||
|
#include <maxbase/log.hh>
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
@ -65,4 +67,33 @@ std::string to_binary_size(int64_t size)
|
|||||||
snprintf(buf, sizeof(buf), "%.2lf%s", num, get_binary_size_suffix(idx));
|
snprintf(buf, sizeof(buf), "%.2lf%s", num, get_binary_size_suffix(idx));
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string string_printf(const char* format, ...)
|
||||||
|
{
|
||||||
|
/* Use 'vsnprintf' for the formatted printing. It outputs the optimal buffer length - 1. */
|
||||||
|
va_list args;
|
||||||
|
va_start(args, format);
|
||||||
|
int characters = vsnprintf(NULL, 0, format, args);
|
||||||
|
va_end(args);
|
||||||
|
std::string rval;
|
||||||
|
if (characters < 0)
|
||||||
|
{
|
||||||
|
// Encoding (programmer) error.
|
||||||
|
mxb_assert(!true);
|
||||||
|
MXB_ERROR("Could not format the string %s.", format);
|
||||||
|
}
|
||||||
|
else if (characters > 0)
|
||||||
|
{
|
||||||
|
// 'characters' does not include the \0-byte.
|
||||||
|
int total_size = characters + 1;
|
||||||
|
rval.reserve(total_size);
|
||||||
|
rval.resize(characters); // The final "length" of the string
|
||||||
|
va_start(args, format);
|
||||||
|
// Write directly to the string internal array, avoiding any temporary arrays.
|
||||||
|
vsnprintf(&rval[0], total_size, format, args);
|
||||||
|
va_end(args);
|
||||||
|
}
|
||||||
|
return rval;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1229,34 +1229,6 @@ uint8_t* set_byteN(uint8_t* ptr, uint64_t value, int bytes)
|
|||||||
return ptr + bytes;
|
return ptr + bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string string_printf(const char* format, ...)
|
|
||||||
{
|
|
||||||
/* Use 'vsnprintf' for the formatted printing. It outputs the optimal buffer length - 1. */
|
|
||||||
va_list args;
|
|
||||||
va_start(args, format);
|
|
||||||
int characters = vsnprintf(NULL, 0, format, args);
|
|
||||||
va_end(args);
|
|
||||||
std::string rval;
|
|
||||||
if (characters < 0)
|
|
||||||
{
|
|
||||||
// Encoding (programmer) error.
|
|
||||||
mxb_assert(!true);
|
|
||||||
MXS_ERROR("Could not format the string %s.", format);
|
|
||||||
}
|
|
||||||
else if (characters > 0)
|
|
||||||
{
|
|
||||||
// 'characters' does not include the \0-byte.
|
|
||||||
int total_size = characters + 1;
|
|
||||||
rval.reserve(total_size);
|
|
||||||
rval.resize(characters); // The final "length" of the string
|
|
||||||
va_start(args, format);
|
|
||||||
// Write directly to the string internal array, avoiding any temporary arrays.
|
|
||||||
vsnprintf(&rval[0], total_size, format, args);
|
|
||||||
va_end(args);
|
|
||||||
}
|
|
||||||
return rval;
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
|
|
||||||
|
@ -12,16 +12,16 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "mariadbmon.hh"
|
#include "mariadbmon.hh"
|
||||||
|
#include <algorithm>
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <queue>
|
#include <queue>
|
||||||
|
#include <maxbase/format.hh>
|
||||||
#include <maxscale/modutil.h>
|
#include <maxscale/modutil.h>
|
||||||
#include <maxscale/mysql_utils.h>
|
#include <maxscale/mysql_utils.h>
|
||||||
#include <maxscale/utils.hh>
|
|
||||||
|
|
||||||
using std::string;
|
using std::string;
|
||||||
using maxscale::string_printf;
|
using maxbase::string_printf;
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
|
@ -12,18 +12,18 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "mariadbmon.hh"
|
#include "mariadbmon.hh"
|
||||||
|
#include <algorithm>
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
#include <set>
|
#include <set>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <maxbase/stopwatch.hh>
|
#include <maxbase/stopwatch.hh>
|
||||||
|
#include <maxbase/format.hh>
|
||||||
#include <maxscale/clock.h>
|
#include <maxscale/clock.h>
|
||||||
#include <maxscale/mysql_utils.h>
|
#include <maxscale/mysql_utils.h>
|
||||||
#include <maxscale/utils.hh>
|
|
||||||
|
|
||||||
using std::string;
|
using std::string;
|
||||||
using std::unique_ptr;
|
using std::unique_ptr;
|
||||||
using maxscale::string_printf;
|
using maxbase::string_printf;
|
||||||
using maxbase::StopWatch;
|
using maxbase::StopWatch;
|
||||||
using maxbase::Duration;
|
using maxbase::Duration;
|
||||||
|
|
||||||
|
@ -19,18 +19,19 @@
|
|||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <maxbase/assert.h>
|
#include <maxbase/assert.h>
|
||||||
|
#include <maxbase/format.hh>
|
||||||
#include <maxscale/alloc.h>
|
#include <maxscale/alloc.h>
|
||||||
#include <maxscale/dcb.h>
|
#include <maxscale/dcb.h>
|
||||||
#include <maxscale/modulecmd.h>
|
#include <maxscale/modulecmd.h>
|
||||||
#include <maxscale/mysql_utils.h>
|
#include <maxscale/mysql_utils.h>
|
||||||
#include <maxscale/routingworker.h>
|
#include <maxscale/routingworker.h>
|
||||||
#include <maxscale/secrets.h>
|
#include <maxscale/secrets.h>
|
||||||
#include <maxscale/utils.hh>
|
#include <maxscale/utils.h>
|
||||||
// TODO: For monitor_add_parameters
|
// TODO: For monitor_add_parameters
|
||||||
#include "../../../core/internal/monitor.h"
|
#include "../../../core/internal/monitor.h"
|
||||||
|
|
||||||
using std::string;
|
using std::string;
|
||||||
using maxscale::string_printf;
|
using maxbase::string_printf;
|
||||||
|
|
||||||
// Config parameter names
|
// Config parameter names
|
||||||
const char* const CN_AUTO_FAILOVER = "auto_failover";
|
const char* const CN_AUTO_FAILOVER = "auto_failover";
|
||||||
@ -278,7 +279,6 @@ void MariaDBMonitor::diagnostics(DCB* dcb) const
|
|||||||
|
|
||||||
string MariaDBMonitor::diagnostics_to_string() const
|
string MariaDBMonitor::diagnostics_to_string() const
|
||||||
{
|
{
|
||||||
using maxscale::string_printf;
|
|
||||||
string rval;
|
string rval;
|
||||||
rval += string_printf("Automatic failover: %s\n", m_auto_failover ? "Enabled" : "Disabled");
|
rval += string_printf("Automatic failover: %s\n", m_auto_failover ? "Enabled" : "Disabled");
|
||||||
rval += string_printf("Failcount: %d\n", m_failcount);
|
rval += string_printf("Failcount: %d\n", m_failcount);
|
||||||
|
@ -17,12 +17,13 @@
|
|||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <maxscale/mysql_utils.h>
|
|
||||||
#include <maxscale/utils.hh>
|
|
||||||
#include <set>
|
#include <set>
|
||||||
|
#include <maxbase/format.hh>
|
||||||
|
#include <maxscale/mysql_utils.h>
|
||||||
|
|
||||||
|
|
||||||
using std::string;
|
using std::string;
|
||||||
using maxscale::string_printf;
|
using maxbase::string_printf;
|
||||||
using maxbase::Duration;
|
using maxbase::Duration;
|
||||||
using maxbase::StopWatch;
|
using maxbase::StopWatch;
|
||||||
|
|
||||||
|
@ -14,11 +14,12 @@
|
|||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
|
#include <maxbase/format.hh>
|
||||||
#include <maxbase/assert.h>
|
#include <maxbase/assert.h>
|
||||||
#include <maxscale/utils.hh>
|
|
||||||
|
|
||||||
using std::string;
|
using std::string;
|
||||||
using maxscale::string_printf;
|
using maxbase::string_printf;
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user