Skip to content

Commit

Permalink
Adds API function to clean up any internal objects before exiting.
Browse files Browse the repository at this point in the history
Makes channels work with checks they exist before emitting messages.
  • Loading branch information
aliakatas committed Dec 29, 2024
1 parent f54a9e1 commit 064117d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
11 changes: 10 additions & 1 deletion include/applogger/applogger_c_interface.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,16 @@ extern "C" {
* @param message [in] The message to be logged.
* @param err [out] Returns 0 on success, -1 on error and 1 in case of success with messages.
*/
DllExport void send_message_to_applogger(const char* channel, const char* severity, const char* message, int*err);
DllExport void send_message_to_applogger(const char* channel, const char* severity, const char* message, int* err);

/**
* @brief Closes any open log files and destroys the internal objects.
* Any messages from the module are cleared and cannot be recovered.
* It is recommended to retrieve them before calling this function.
*
* @param err [out] Returns 0 on success, -1 on error and 1 in case of success with messages.
*/
DllExport void destroy_apploger(int* err);

#if __cplusplus
}
Expand Down
30 changes: 25 additions & 5 deletions src/applogger/applogger_c_interface.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
#include <vector>
#include <stdexcept>
#include <memory>
// #include <iostream>
#include <set>

namespace applogger
{
std::unique_ptr<AppLogger> applogger;
std::vector<std::string> messages;
std::vector<std::string> channels;
std::set<std::string> channels;
}

void log_message_to_channel(const AppLogger::Severity& severityVal, const std::string& channel, const std::string& message)
Expand Down Expand Up @@ -73,6 +73,7 @@ extern "C" {
memset(buffer, 0, *nchars * sizeof(char));
strncpy(buffer, local_buffer, *nchars - 1);
delete[] local_buffer;
applogger::messages.clear();
}
else
{
Expand Down Expand Up @@ -180,7 +181,7 @@ extern "C" {
else
applogger::applogger->addChannelSink(std::string(channel), std::string(sinkname), severity);

applogger::channels.push_back(channel);
applogger::channels.emplace(channel);
}
catch(const std::exception& e)
{
Expand Down Expand Up @@ -214,9 +215,18 @@ extern "C" {
*err = APPLOGGER_EXIT_WITH_MESSAGES;
}

// TODO: convert channel memory to set so that we can run a quick validity check before proceeding!
if (channel)
log_message_to_channel(severityVal, std::string(channel), std::string(message));
{
if (applogger::channels.find(std::string(channel)) != applogger::channels.end())
log_message_to_channel(severityVal, std::string(channel), std::string(message));
else
{
*err = APPLOGGER_EXIT_WITH_MESSAGES;
applogger::messages.push_back("channel not regognised, logging to all available channels");
for (auto& c : applogger::channels)
log_message_to_channel(severityVal, c, std::string(message));
}
}
else
{
for (auto& c : applogger::channels)
Expand All @@ -231,6 +241,16 @@ extern "C" {
}
}

//====================================================================
DllExport void destroy_apploger(int* err)
{
*err = APPLOGGER_EXIT_SUCCESS;
applogger::messages.clear();
applogger::channels.clear();
// applogger::applogger should be destroyed since it's
// a unique_ptr...
}

#if __cplusplus
}
#endif

0 comments on commit 064117d

Please sign in to comment.