MXS-2008 Provide single entrypoint for initializing maxbase

Everything of maxbase can now be initialized by a call to

    maxbase_init();

(from a C-program) or

    maxbase::init();

from a C++-program and finalized with calls to either
maxbase_finish() or maxbase::finish(). Creating an instance
maxbase::MaxBase will take care of both operations.
This commit is contained in:
Johan Wikman
2018-08-20 14:29:22 +03:00
parent 88f1795412
commit e852dcacdd
9 changed files with 324 additions and 36 deletions

View File

@ -115,6 +115,12 @@ bool mxb_log_init(const char* ident,
*/
void mxb_log_finish(void);
/**
* @brief Has the log been initialized.
*
* @return True if the log has been initialized, false otherwise.
*/
bool mxb_log_inited();
/**
* Rotate the log
*

View File

@ -0,0 +1,34 @@
#pragma once
/*
* Copyright (c) 2016 MariaDB Corporation Ab
*
* Use of this software is governed by the Business Source License included
* in the LICENSE.TXT file and at www.mariadb.com/bsl11.
*
* Change Date: 2022-01-01
*
* On the date above, in accordance with the Business Source License, use
* of this software will be governed by version 2 or later of the General
* Public License.
*/
#include <maxbase/cdefs.h>
/**
* @brief Initializes the maxbase library
*
* Initializes the maxbase library, except for the log that must
* be initialized separately, before or after this function is
* called.
*
* @return True, if maxbase could be initialized, false otherwise.
*/
bool maxbase_init();
/**
* @brief Finalizes the maxbase library
*
* This function should be called before program exit, if @c maxbase_init()
* returned true.
*/
void maxbase_finish();

View File

@ -0,0 +1,109 @@
#pragma once
/*
* Copyright (c) 2016 MariaDB Corporation Ab
*
* Use of this software is governed by the Business Source License included
* in the LICENSE.TXT file and at www.mariadb.com/bsl11.
*
* Change Date: 2022-01-01
*
* On the date above, in accordance with the Business Source License, use
* of this software will be governed by version 2 or later of the General
* Public License.
*/
#include <maxbase/ccdefs.hh>
#include <stdexcept>
#include <maxbase/log.h>
#include <maxbase/maxbase.h>
namespace maxbase
{
/**
* @brief Initializes the maxbase library
*
* This function should be called before any other functionality of
* maxbase is used. A notable exception is the maxbase log that can
* be initialized and used independently.
*
* Note that if an instance of @c MaxBase is created, it will call
* both @init and @finish.
*
* @return True, if maxbase could be initialized, false otherwise.
*/
bool init();
/**
* @brief Finalizes the maxbase library
*
* This function should be called before program exit, if @c init()
* returned true.
*/
void finish();
/**
* @class MaxBase
*
* A simple utility RAII class where the constructor initializes maxbase
* (and optionally the log) and the destructor finalizes it.
*/
class MaxBase
{
MaxBase(const MaxBase&) = delete;
MaxBase& operator=(const MaxBase&) = delete;
public:
/**
* @brief Initializes MaxBase but not the MaxBase log.
*/
MaxBase()
: m_log_inited(false)
{
if (!maxbase_init())
{
throw std::runtime_error("Initialization of maxbase failed.");
}
}
/**
* @brief Initializes MaxBase and the MaxBase log.
*
* @see mxb_log_init
*
* @throws std::runtime_error if the initialization failed.
*/
MaxBase(const char* zIdent,
const char* zLogdir,
const char* zFilename,
mxb_log_target_t target,
mxb_log_context_provider_t context_provider);
/**
* @brief Initializes MaxBase and the MaxBase log.
*
* @see mxb_log_init
*
* @throws std::runtime_error if the initialization failed.
*/
MaxBase(mxb_log_target_t target)
: MaxBase(nullptr, ".", nullptr, target, nullptr)
{
}
~MaxBase()
{
if (m_log_inited)
{
mxb_log_finish();
}
maxbase::finish();
}
private:
bool m_log_inited;
};
}

View File

@ -18,6 +18,7 @@
namespace maxbase
{
class MaxBase;
class MessageQueue;
class Worker;
@ -116,21 +117,6 @@ public:
typedef MessageQueueHandler Handler;
typedef MessageQueueMessage Message;
/**
* Initializes the message queue mechanism. To be called once at
* process startup.
*
* @return True if the initialization succeeded, false otherwise.
*/
static bool init();
/**
* Finalizes the message queue mechanism. To be called once at
* process shutdown, if the initialization succeeded.
*/
static void finish();
/**
* Creates a @c MessageQueue with the provided handler.
*
@ -190,6 +176,12 @@ public:
*/
Worker* remove_from_worker();
public:
// TODO: Make private once all callers have been modified.
friend class MaxBase;
static bool init();
static void finish();
private:
MessageQueue(Handler* pHandler, int read_fd, int write_fd);

View File

@ -559,22 +559,6 @@ public:
};
};
/**
* Initialize the worker mechanism.
*
* To be called once at process startup.
*
* @return True if the initialization succeeded, false otherwise.
*/
static bool init();
/**
* Finalize the worker mechanism.
*
* To be called once at process shutdown.
*/
static void finish();
enum
{
MAX_EVENTS = 1000
@ -990,6 +974,11 @@ protected:
*/
static void resolve_poll_error(int fd, int err, int op);
public:
// TODO: Make private once all callers have beed modified.
static bool init();
static void finish();
private:
class DelayedCall;
friend class DelayedCall;