Skip to content

Commit

Permalink
lib: add overload Mmsg() for std::vector<char>
Browse files Browse the repository at this point in the history
Previously Mmsg() would only write into a POOLMEM or PoolMem. This patch
adds another overload that allows Mmsg() to write to a std::vector<char>
and to automatically resize that vector as needed.
  • Loading branch information
arogge committed Feb 11, 2020
1 parent 8d92e8f commit ffc074b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
1 change: 1 addition & 0 deletions core/src/include/baconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ class PoolMem;
int Mmsg(POOLMEM*& msgbuf, const char* fmt, ...);
int Mmsg(PoolMem& msgbuf, const char* fmt, ...);
int Mmsg(PoolMem*& msgbuf, const char* fmt, ...);
int Mmsg(std::vector<char>& msgbuf, const char* fmt, ...);

class JobControlRecord;
void d_msg(const char* file, int line, int level, const char* fmt, ...);
Expand Down
31 changes: 31 additions & 0 deletions core/src/lib/message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
* Kern Sibbald, April 2000
*/

#include <vector>

#include "include/bareos.h"
#include "include/jcr.h"
#include "lib/berrno.h"
Expand Down Expand Up @@ -1564,6 +1566,35 @@ int Mmsg(PoolMem*& pool_buf, const char* fmt, ...)
return len;
}

int Mmsg(std::vector<char>& msgbuf, const char* fmt, ...)
{
va_list ap;

size_t maxlen = msgbuf.size();
size_t len = strlen(fmt);

/* resize msgbuf so at least fmt fits in there.
* this makes sure the rest of the code works with a zero-sized vector
*/
if (maxlen < len) {
msgbuf.resize(len);
maxlen = len;
}

while (1) {
va_start(ap, fmt);
len = Bvsnprintf(msgbuf.data(), maxlen, fmt, ap);
va_end(ap);

if (len < 0 || len >= (maxlen - 5)) {
maxlen += maxlen / 2;
msgbuf.resize(maxlen);
continue;
}
return len;
}
}

/*
* We queue messages rather than print them directly. This
* is generally used in low level routines (msg handler, bnet)
Expand Down
3 changes: 2 additions & 1 deletion core/src/lib/message.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@
#include "lib/rwlock.h"
#include "lib/message_destination_info.h"

#include <string>
#include <functional>
#include <string>
#include <vector>

class JobControlRecord;

Expand Down

0 comments on commit ffc074b

Please sign in to comment.