Skip to content

Commit

Permalink
lib: prepare callbacks for mockup callbacks in unittests
Browse files Browse the repository at this point in the history
  • Loading branch information
franku committed Feb 10, 2020
1 parent 8aaf7b2 commit 93cef66
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
18 changes: 10 additions & 8 deletions core/src/lib/message.cc
Expand Up @@ -3,7 +3,7 @@
Copyright (C) 2000-2012 Free Software Foundation Europe e.V.
Copyright (C) 2011-2012 Planets Communications B.V.
Copyright (C) 2013-2019 Bareos GmbH & Co. KG
Copyright (C) 2013-2020 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
Expand Down Expand Up @@ -43,8 +43,6 @@
#include "lib/message_queue_item.h"
#include "lib/thread_specific_data.h"

static db_log_insert_func p_db_log_insert = NULL;

// globals
const char* working_directory = NULL; /* working directory path stored here */
const char* assert_msg = (char*)NULL; /* ASSERT2 error message */
Expand Down Expand Up @@ -597,7 +595,7 @@ static inline bool SetSyslogFacility(JobControlRecord* jcr,
* Split the output for syslog (it converts \n to ' ' and is
* limited to 1024 characters per syslog message
*/
static inline void SendToSyslog(int mode, const char* msg)
static void SendToSyslog_(int mode, const char* msg)
{
int len;
char buf[1024];
Expand All @@ -616,6 +614,12 @@ static inline void SendToSyslog(int mode, const char* msg)
}
}

static SyslogCallback SendToSyslog{SendToSyslog_};
void RegisterSyslogCallback(SyslogCallback c) { SendToSyslog = c; }

static DbLogInsertCallback SendToDbLog = NULL;
void SetDbLogInsertCallback(DbLogInsertCallback f) { SendToDbLog = f; }

/*
* Handle sending the message to the appropriate place
*/
Expand Down Expand Up @@ -737,8 +741,8 @@ void DispatchMessage(JobControlRecord* jcr,
case MessageDestinationCode::kCatalog:
if (!jcr || !jcr->db) { break; }

if (p_db_log_insert) {
if (!p_db_log_insert(jcr, mtime, msg)) {
if (SendToDbLog) {
if (!SendToDbLog(jcr, mtime, msg)) {
DeliveryError(
_("Msg delivery error: Unable to store data in database.\n"));
}
Expand Down Expand Up @@ -1678,5 +1682,3 @@ void SetLogTimestampFormat(const char* format)
{
log_timestamp_format = format;
}

void SetDbLogInsertCallback(db_log_insert_func f) { p_db_log_insert = f; }
11 changes: 7 additions & 4 deletions core/src/lib/message.h
Expand Up @@ -37,6 +37,7 @@
#include "lib/message_destination_info.h"

#include <string>
#include <functional>

class JobControlRecord;

Expand All @@ -50,10 +51,9 @@ bool GetTrace(void);
const char* get_basename(const char* pathname);
void SetLogTimestampFormat(const char* format);

typedef bool (*db_log_insert_func)(JobControlRecord* jcr,
utime_t mtime,
const char* msg);
void SetDbLogInsertCallback(db_log_insert_func f);
using DbLogInsertCallback =
std::function<bool(JobControlRecord* jcr, utime_t mtime, const char* msg)>;
void SetDbLogInsertCallback(DbLogInsertCallback f);

class MessagesResource;

Expand Down Expand Up @@ -92,4 +92,7 @@ bool GetTimestamp(void);
void SetDbType(const char* name);
void RegisterMessageCallback(void msg_callback(int type, const char* msg));

using SyslogCallback = std::function<void(int mode, const char* msg)>;
void RegisterSyslogCallback(SyslogCallback c);

#endif /* BAREOS_LIB_MESSAGE_H_ */

0 comments on commit 93cef66

Please sign in to comment.