mirror of
https://github.com/KjellKod/g3log.git
synced 2024-12-13 10:42:56 +01:00
compile error: namespace usage or lack thereof
This commit is contained in:
parent
81b6b91038
commit
a69286399d
@ -5,20 +5,19 @@
|
||||
* Created on October 26, 2013, 3:45 PM
|
||||
*/
|
||||
|
||||
//#include "g2log.hpp"
|
||||
#include "g2LogMessageBuilder.hpp"
|
||||
#include "g2logmessageimpl.hpp"
|
||||
#include "g2log.hpp"
|
||||
#include "g2logmessage.hpp"
|
||||
#include <iostream>
|
||||
#include <csignal>
|
||||
|
||||
namespace g2 {
|
||||
|
||||
namespace g2 {
|
||||
using namespace internal;
|
||||
LogMessageBuilder::LogMessageBuilder(const std::string& file, const int line,
|
||||
const std::string& function, const LEVELS& level)
|
||||
: _message(std::make_shared<LogMessageImpl>(file, line, function, level)) {}
|
||||
//: _message(new LogMessageImpl(file, line, function, level)) //
|
||||
//{}
|
||||
: _message(std::make_shared<LogMessageImpl>(file, line, function, level)) {
|
||||
}
|
||||
|
||||
LogMessageBuilder::~LogMessageBuilder() {
|
||||
LogMessage log_entry(_message);
|
||||
@ -26,7 +25,7 @@ namespace g2 {
|
||||
FatalMessageBuilder trigger({log_entry.toString(), SIGABRT});
|
||||
return; // FatalMessageBuilder will send to worker at scope exit
|
||||
}
|
||||
internal::saveMessage(log_entry); // message saved to g2LogWorker
|
||||
saveMessage(log_entry); // message saved to g2LogWorker
|
||||
}
|
||||
|
||||
LogMessageBuilder& LogMessageBuilder::setExpression(const std::string& boolean_expression) {
|
||||
@ -40,17 +39,18 @@ namespace g2 {
|
||||
|
||||
|
||||
/// FatalMessageBuilder
|
||||
|
||||
FatalMessageBuilder::FatalMessageBuilder(const std::string& exit_message, int fatal_signal)
|
||||
:_exit_message(exit_message), _fatal_signal(fatal_signal) { }
|
||||
: _exit_message(exit_message), _fatal_signal(fatal_signal) {
|
||||
}
|
||||
|
||||
FatalMessageBuilder::~FatalMessageBuilder() {
|
||||
// At destruction, flushes fatal message to g2LogWorker
|
||||
// either we will stay here until the background worker has received the fatal
|
||||
// message, flushed the crash message to the sinks and exits with the same fatal signal
|
||||
//..... OR it's in unit-test mode then we throw a std::runtime_error (and never hit sleep)
|
||||
internal::fatalCall({_exit_message, _fatal_signal});
|
||||
FatalMessage msg(_exit_message, _fatal_signal);
|
||||
//internal::fatalCall({_exit_message, _fatal_signal});
|
||||
internal::fatalCall(msg);
|
||||
|
||||
}
|
||||
|
||||
|
||||
} // g2
|
@ -13,36 +13,36 @@
|
||||
#include "g2loglevels.hpp"
|
||||
|
||||
|
||||
namespace g2 {struct LogMessageImpl;
|
||||
namespace g2 {
|
||||
|
||||
// At RAII scope end this struct will trigger a FatalMessage sending
|
||||
struct FatalMessageBuilder {
|
||||
//explicit FatalMessageBuilder(const FatalMessage& exit_message);
|
||||
FatalMessageBuilder(const std::string& exit_message, int fatal_signal);
|
||||
~FatalMessageBuilder();
|
||||
|
||||
std::string _exit_message;
|
||||
int _fatal_signal;
|
||||
};
|
||||
struct LogMessageImpl;
|
||||
|
||||
// At RAII scope end this struct will trigger a FatalMessage sending
|
||||
struct FatalMessageBuilder {
|
||||
//explicit FatalMessageBuilder(const FatalMessage& exit_message);
|
||||
FatalMessageBuilder(const std::string& exit_message, int fatal_signal);
|
||||
virtual ~FatalMessageBuilder();
|
||||
|
||||
struct LogMessageBuilder {
|
||||
LogMessageBuilder(const std::string& file, const int line, const std::string& function, const LEVELS& level);
|
||||
virtual ~LogMessageBuilder();
|
||||
std::string _exit_message;
|
||||
int _fatal_signal;
|
||||
};
|
||||
|
||||
LogMessageBuilder& setExpression(const std::string& boolean_expression);
|
||||
std::ostringstream& stream();
|
||||
struct LogMessageBuilder {
|
||||
LogMessageBuilder(const std::string& file, const int line, const std::string& function, const LEVELS& level);
|
||||
virtual ~LogMessageBuilder();
|
||||
|
||||
// Use "-Wall" to generate warnings in case of illegal printf format.
|
||||
// Ref: http://www.unixwiz.net/techtips/gnu-c-attributes.html
|
||||
LogMessageBuilder& setExpression(const std::string& boolean_expression);
|
||||
std::ostringstream& stream();
|
||||
|
||||
// Use "-Wall" to generate warnings in case of illegal printf format.
|
||||
// Ref: http://www.unixwiz.net/techtips/gnu-c-attributes.html
|
||||
#ifndef __GNUC__
|
||||
#define __attribute__(x) // Disable 'attributes' if compiler does not support 'em
|
||||
#endif
|
||||
void messageSave(const char *printf_like_message, ...)
|
||||
__attribute__((format(printf, 2, 3))); // ref: http://www.codemaestro.com/reviews/18
|
||||
|
||||
private:
|
||||
std::shared_ptr<LogMessageImpl> _message;
|
||||
};
|
||||
void messageSave(const char *printf_like_message, ...)
|
||||
__attribute__((format(printf, 2, 3))); // ref: http://www.codemaestro.com/reviews/18
|
||||
|
||||
private:
|
||||
std::shared_ptr<LogMessageImpl> _message;
|
||||
};
|
||||
} // g2
|
@ -44,43 +44,41 @@ class g2LogWorker;
|
||||
* --- Thanks for a great 2011 and good luck with 'g2' --- KjellKod
|
||||
*/
|
||||
namespace g2 {
|
||||
struct LogMessage;
|
||||
struct FatalMessage;
|
||||
struct LogMessage;
|
||||
struct FatalMessage;
|
||||
|
||||
|
||||
/** Should be called at very first startup of the software with \ref g2LogWorker
|
||||
* pointer. Ownership of the \ref g2LogWorker is the responsibilkity of the caller */
|
||||
void initializeLogging(g2LogWorker *logger);
|
||||
/** Should be called at very first startup of the software with \ref g2LogWorker
|
||||
* pointer. Ownership of the \ref g2LogWorker is the responsibilkity of the caller */
|
||||
void initializeLogging(g2LogWorker *logger);
|
||||
|
||||
namespace internal {
|
||||
|
||||
// Save the created LogMessage to any existing sinks
|
||||
void saveMessage(g2::LogMessage log_entry);
|
||||
namespace internal {
|
||||
|
||||
bool isLoggingInitialized();
|
||||
|
||||
|
||||
// Save the created FatalMessage to any existing sinks and exit with
|
||||
// the originating fatal signal,. or SIGABRT if it originated from a broken contract
|
||||
void fatalCall(FatalMessage message);
|
||||
// Save the created LogMessage to any existing sinks
|
||||
void saveMessage(LogMessage log_entry);
|
||||
|
||||
// Save the created FatalMessage to any existing sinks and exit with
|
||||
// the originating fatal signal,. or SIGABRT if it originated from a broken contract
|
||||
void fatalCall(FatalMessage message);
|
||||
|
||||
/** FOR TESTING PURPOSES
|
||||
* Shutdown the logging by making the pointer to the background logger to nullptr
|
||||
* The \ref pointer to the g2LogWorker is owned by the instantniater \ref initializeLogging
|
||||
* and is not deleted. By restoring the ptr to nullptr we can re-initialize it later again.
|
||||
*
|
||||
* This is kept for test reasons and should normally not be used */
|
||||
g2LogWorker* shutDownLogging();
|
||||
/** FOR TESTING PURPOSES
|
||||
* Shutdown the logging by making the pointer to the background logger to nullptr
|
||||
* The \ref pointer to the g2LogWorker is owned by the instantniater \ref initializeLogging
|
||||
* and is not deleted. By restoring the ptr to nullptr we can re-initialize it later again.
|
||||
*
|
||||
* This is kept for test reasons and should normally not be used */
|
||||
g2LogWorker* shutDownLogging();
|
||||
|
||||
bool isLoggingInitialized();
|
||||
|
||||
|
||||
/** FOR TESTING PURPOSES: @ref g2log.ipp
|
||||
* By default the g2log will call g2LogWorker::fatal(...) which will
|
||||
* abort() the system after flushing the logs to file. This makes unit
|
||||
* test of FATAL level cumbersome. A work around is to change the
|
||||
* fatal call' which can be done here */
|
||||
void changeFatalInitHandlerForUnitTesting();
|
||||
} // g2::internal
|
||||
/** FOR TESTING PURPOSES: @ref g2log.ipp
|
||||
* By default the g2log will call g2LogWorker::fatal(...) which will
|
||||
* abort() the system after flushing the logs to file. This makes unit
|
||||
* test of FATAL level cumbersome. A work around is to change the
|
||||
* fatal call' which can be done here */
|
||||
void changeFatalInitHandlerForUnitTesting();
|
||||
} // g2::internal
|
||||
} // g2
|
||||
|
||||
|
||||
|
@ -20,76 +20,75 @@
|
||||
#include <chrono>
|
||||
|
||||
namespace g2 {
|
||||
namespace internal {
|
||||
g2LogWorker* g_logger_instance = nullptr; // instantiated and OWNED somewhere else (main)
|
||||
std::string g_once_error;
|
||||
std::once_flag g_error_flag;
|
||||
std::once_flag g_retrieve_error_flag;
|
||||
}
|
||||
namespace internal {
|
||||
g2LogWorker* g_logger_instance = nullptr; // instantiated and OWNED somewhere else (main)
|
||||
std::string g_once_error;
|
||||
std::once_flag g_error_flag;
|
||||
std::once_flag g_retrieve_error_flag;
|
||||
|
||||
/**
|
||||
* saveMesage to the LogWorker which will pass them to the sinks.
|
||||
*
|
||||
* In the messup of calling LOG before instantiating the logger the first
|
||||
* message will be saved,.. to be given later with an initialization warning
|
||||
*/
|
||||
void saveMessage(g2::LogMessage log_entry) {
|
||||
using namespace internal;
|
||||
|
||||
/**
|
||||
* saveMesage to the LogWorker which will pass them to the sinks.
|
||||
*
|
||||
* In the messup of calling LOG before instantiating the logger the first
|
||||
* message will be saved,.. to be given later with an initialization warning
|
||||
*/
|
||||
void saveMessage(g2::LogMessage log_entry) {
|
||||
using namespace internal;
|
||||
|
||||
if (false == isLoggingInitialized()) {
|
||||
std::call_once(g_error_flag, [&]() {
|
||||
g_once_error = {"\n\nWARNING LOGGER NOT INSTANTIATED WHEN CALLING IT"
|
||||
"\nAt least once tried to call the logger before instantiating it\n\n"};
|
||||
g_once_error.append("\nThe first message was: \n").append(log_entry.toString());
|
||||
});
|
||||
std::cerr << g_once_error << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
std::call_once(g_retrieve_error_flag, [&]() {
|
||||
if (false == g_once_error.empty()) {
|
||||
std::string empty = {""};
|
||||
g2::LogMessage error{std::make_shared<g2::LogMessageImpl>(empty, 0, empty, WARNING)};
|
||||
error.stream() << g_once_error;
|
||||
g_logger_instance->save(error);
|
||||
if (false == isLoggingInitialized()) {
|
||||
std::call_once(g_error_flag, [&]() {
|
||||
g_once_error = {"\n\nWARNING LOGGER NOT INSTANTIATED WHEN CALLING IT"
|
||||
"\nAt least once tried to call the logger before instantiating it\n\n"};
|
||||
g_once_error.append("\nThe first message was: \n").append(log_entry.toString());
|
||||
});
|
||||
std::cerr << g_once_error << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
std::call_once(g_retrieve_error_flag, [&]() {
|
||||
if (false == g_once_error.empty()) {
|
||||
std::string empty = {""};
|
||||
g2::LogMessage error{std::make_shared<g2::LogMessageImpl>(empty, 0, empty, WARNING)};
|
||||
error.stream() << g_once_error;
|
||||
g_logger_instance->save(error);
|
||||
}
|
||||
});
|
||||
|
||||
g_logger_instance->save(log_entry);
|
||||
}
|
||||
});
|
||||
|
||||
g_logger_instance->save(log_entry);
|
||||
}
|
||||
|
||||
void fatalCallToLogger(FatalMessage message) {
|
||||
// real fatal call to loggr
|
||||
internal::g_logger_instance->fatal(message);
|
||||
}
|
||||
void fatalCallToLogger(FatalMessage message) {
|
||||
// real fatal call to loggr
|
||||
internal::g_logger_instance->fatal(message);
|
||||
}
|
||||
|
||||
// By default this function pointer goes to \ref fatalCall;
|
||||
void (*g_fatal_to_g2logworker_function_ptr)(FatalMessage) = fatalCallToLogger;
|
||||
// By default this function pointer goes to \ref fatalCall;
|
||||
void (*g_fatal_to_g2logworker_function_ptr)(FatalMessage) = fatalCallToLogger;
|
||||
|
||||
void fatalCallForUnitTest(FatalMessage fatal_message) {
|
||||
// mock fatal call, not to logger: used by unit test
|
||||
assert(internal::g_logger_instance != nullptr);
|
||||
internal::g_logger_instance->save(fatal_message.copyToLogMessage()); // calling 'save' instead of 'fatal'
|
||||
throw std::runtime_error(fatal_message.toString());
|
||||
}
|
||||
void fatalCallForUnitTest(FatalMessage fatal_message) {
|
||||
// mock fatal call, not to logger: used by unit test
|
||||
assert(internal::g_logger_instance != nullptr);
|
||||
internal::g_logger_instance->save(fatal_message.copyToLogMessage()); // calling 'save' instead of 'fatal'
|
||||
throw std::runtime_error(fatal_message.toString());
|
||||
}
|
||||
|
||||
/** The default, initial, handling to send a 'fatal' event to g2logworker
|
||||
* he caller will stay here, eternally, until the software is aborted
|
||||
* During unit testing the sleep-loop will be interrupted by exception from
|
||||
* @ref fatalCallForUnitTest */
|
||||
void fatalCall(FatalMessage message) {
|
||||
g_fatal_to_g2logworker_function_ptr(message);
|
||||
while (true) {
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
}
|
||||
}
|
||||
/** The default, initial, handling to send a 'fatal' event to g2logworker
|
||||
* he caller will stay here, eternally, until the software is aborted
|
||||
* During unit testing the sleep-loop will be interrupted by exception from
|
||||
* @ref fatalCallForUnitTest */
|
||||
void fatalCall(FatalMessage message) {
|
||||
g_fatal_to_g2logworker_function_ptr(message);
|
||||
while (true) {
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
}
|
||||
}
|
||||
|
||||
/** Used to REPLACE fatalCallToLogger for fatalCallForUnitTest
|
||||
* This function switches the function pointer so that only
|
||||
* 'unitTest' mock-fatal calls are made. */
|
||||
void changeFatalInitHandlerForUnitTesting() {
|
||||
g_fatal_to_g2logworker_function_ptr = fatalCallForUnitTest;
|
||||
}
|
||||
/** Used to REPLACE fatalCallToLogger for fatalCallForUnitTest
|
||||
* This function switches the function pointer so that only
|
||||
* 'unitTest' mock-fatal calls are made. */
|
||||
void changeFatalInitHandlerForUnitTesting() {
|
||||
g_fatal_to_g2logworker_function_ptr = fatalCallForUnitTest;
|
||||
}
|
||||
} // internal
|
||||
} // g2
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user