Move string_printf to maxbase

Can be used in tests.
This commit is contained in:
Esa Korhonen
2018-11-08 16:32:13 +02:00
parent dbe9e0c471
commit a1e1ac0012
9 changed files with 57 additions and 51 deletions

View File

@ -137,15 +137,6 @@ inline std::vector<std::string> strtok(std::string str, const char* delim)
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>
*

View File

@ -29,4 +29,14 @@ namespace maxbase
* @return Value as a human readable size e.g. 5.01MiB
*/
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)));
}

View File

@ -15,6 +15,8 @@
#include <cmath>
#include <cstdio>
#include <maxbase/assert.h>
#include <maxbase/log.hh>
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));
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;
}
}

View File

@ -1229,34 +1229,6 @@ uint8_t* set_byteN(uint8_t* ptr, uint64_t value, int 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
{

View File

@ -12,16 +12,16 @@
*/
#include "mariadbmon.hh"
#include <algorithm>
#include <inttypes.h>
#include <string>
#include <queue>
#include <maxbase/format.hh>
#include <maxscale/modutil.h>
#include <maxscale/mysql_utils.h>
#include <maxscale/utils.hh>
using std::string;
using maxscale::string_printf;
using maxbase::string_printf;
namespace
{

View File

@ -12,18 +12,18 @@
*/
#include "mariadbmon.hh"
#include <algorithm>
#include <inttypes.h>
#include <set>
#include <sstream>
#include <maxbase/stopwatch.hh>
#include <maxbase/format.hh>
#include <maxscale/clock.h>
#include <maxscale/mysql_utils.h>
#include <maxscale/utils.hh>
using std::string;
using std::unique_ptr;
using maxscale::string_printf;
using maxbase::string_printf;
using maxbase::StopWatch;
using maxbase::Duration;

View File

@ -19,18 +19,19 @@
#include <inttypes.h>
#include <sstream>
#include <maxbase/assert.h>
#include <maxbase/format.hh>
#include <maxscale/alloc.h>
#include <maxscale/dcb.h>
#include <maxscale/modulecmd.h>
#include <maxscale/mysql_utils.h>
#include <maxscale/routingworker.h>
#include <maxscale/secrets.h>
#include <maxscale/utils.hh>
#include <maxscale/utils.h>
// TODO: For monitor_add_parameters
#include "../../../core/internal/monitor.h"
using std::string;
using maxscale::string_printf;
using maxbase::string_printf;
// Config parameter names
const char* const CN_AUTO_FAILOVER = "auto_failover";
@ -278,7 +279,6 @@ void MariaDBMonitor::diagnostics(DCB* dcb) const
string MariaDBMonitor::diagnostics_to_string() const
{
using maxscale::string_printf;
string rval;
rval += string_printf("Automatic failover: %s\n", m_auto_failover ? "Enabled" : "Disabled");
rval += string_printf("Failcount: %d\n", m_failcount);

View File

@ -17,12 +17,13 @@
#include <inttypes.h>
#include <iomanip>
#include <thread>
#include <maxscale/mysql_utils.h>
#include <maxscale/utils.hh>
#include <set>
#include <maxbase/format.hh>
#include <maxscale/mysql_utils.h>
using std::string;
using maxscale::string_printf;
using maxbase::string_printf;
using maxbase::Duration;
using maxbase::StopWatch;

View File

@ -14,11 +14,12 @@
#include <algorithm>
#include <inttypes.h>
#include <maxbase/format.hh>
#include <maxbase/assert.h>
#include <maxscale/utils.hh>
using std::string;
using maxscale::string_printf;
using maxbase::string_printf;
namespace
{