Skip to content

Commit

Permalink
Alvin/minor fixes and improvements (#389)
Browse files Browse the repository at this point in the history
* Fix typos
* Fix build for MinGW
- MinGW g++ doesn't recoganize `-rdynamic`
  • Loading branch information
alvin-777 authored Nov 18, 2020
1 parent f1eff42 commit c0ae589
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 95 deletions.
114 changes: 57 additions & 57 deletions Build.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,25 @@
# and no restrictions or obligations.
# ===================================================================

# GENERIC STEPS
SET(LOG_SRC ${g3log_SOURCE_DIR}/src)
# GENERIC STEPS
SET(LOG_SRC ${g3log_SOURCE_DIR}/src)

file(GLOB SRC_FILES ${LOG_SRC}/*.cpp ${LOG_SRC}/*.ipp)
file(GLOB HEADER_FILES ${LOG_SRC}/g3log/*.hpp)

list( APPEND HEADER_FILES ${GENERATED_G3_DEFINITIONS} )
list( APPEND SRC_FILES ${GENERATED_G3_DEFINITIONS} )
file(GLOB SRC_FILES ${LOG_SRC}/*.cpp ${LOG_SRC}/*.ipp)
file(GLOB HEADER_FILES ${LOG_SRC}/g3log/*.hpp)

IF (MSVC OR MINGW)
list(REMOVE_ITEM SRC_FILES ${LOG_SRC}/crashhandler_unix.cpp)
ELSE()
list(REMOVE_ITEM SRC_FILES ${LOG_SRC}/crashhandler_windows.cpp ${LOG_SRC}/g3log/stacktrace_windows.hpp ${LOG_SRC}/stacktrace_windows.cpp)
ENDIF (MSVC OR MINGW)
list( APPEND HEADER_FILES ${GENERATED_G3_DEFINITIONS} )
list( APPEND SRC_FILES ${GENERATED_G3_DEFINITIONS} )

set(SRC_FILES ${SRC_FILES} ${SRC_PLATFORM_SPECIFIC})
IF (MSVC OR MINGW)
list(REMOVE_ITEM SRC_FILES ${LOG_SRC}/crashhandler_unix.cpp)
ELSE()
list(REMOVE_ITEM SRC_FILES ${LOG_SRC}/crashhandler_windows.cpp ${LOG_SRC}/g3log/stacktrace_windows.hpp ${LOG_SRC}/stacktrace_windows.cpp)
ENDIF (MSVC OR MINGW)

# Create the g3log library
SET(G3LOG_LIBRARY g3log)
set(SRC_FILES ${SRC_FILES} ${SRC_PLATFORM_SPECIFIC})

# Create the g3log library
SET(G3LOG_LIBRARY g3log)


IF(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
Expand All @@ -40,48 +40,48 @@ IF(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
message("Install rpath location: ${CMAKE_INSTALL_RPATH}")
ENDIF()

IF( G3_SHARED_LIB )
IF( WIN32 )
IF(NOT(${CMAKE_VERSION} VERSION_LESS "3.4"))
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
ELSE()
message( FATAL_ERROR "Need CMake version >=3.4 to build shared windows library!" )
ENDIF()
ENDIF()
ADD_LIBRARY(${G3LOG_LIBRARY} SHARED ${SRC_FILES})
ELSE()
IF(MSVC)
IF(NOT G3_SHARED_RUNTIME)
SET(CompilerFlags
CMAKE_CXX_FLAGS
CMAKE_CXX_FLAGS_DEBUG
CMAKE_CXX_FLAGS_RELEASE
CMAKE_C_FLAGS
CMAKE_C_FLAGS_DEBUG
CMAKE_C_FLAGS_RELEASE
)
foreach(CompilerFlag ${CompilerFlags})
string(REPLACE "/MDd" "/MTd" ${CompilerFlag} "${${CompilerFlag}}")
string(REPLACE "/MD" "/MT" ${CompilerFlag} "${${CompilerFlag}}")
endforeach()
ENDIF()
ENDIF()
ADD_LIBRARY(${G3LOG_LIBRARY} STATIC ${SRC_FILES})
ENDIF()
IF( G3_SHARED_LIB )
IF( WIN32 )
IF(NOT(${CMAKE_VERSION} VERSION_LESS "3.4"))
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
ELSE()
message( FATAL_ERROR "Need CMake version >=3.4 to build shared windows library!" )
ENDIF()
ENDIF()
ADD_LIBRARY(${G3LOG_LIBRARY} SHARED ${SRC_FILES})
ELSE()
IF(MSVC)
IF(NOT G3_SHARED_RUNTIME)
SET(CompilerFlags
CMAKE_CXX_FLAGS
CMAKE_CXX_FLAGS_DEBUG
CMAKE_CXX_FLAGS_RELEASE
CMAKE_C_FLAGS
CMAKE_C_FLAGS_DEBUG
CMAKE_C_FLAGS_RELEASE
)
foreach(CompilerFlag ${CompilerFlags})
string(REPLACE "/MDd" "/MTd" ${CompilerFlag} "${${CompilerFlag}}")
string(REPLACE "/MD" "/MT" ${CompilerFlag} "${${CompilerFlag}}")
endforeach()
ENDIF()
ENDIF()
ADD_LIBRARY(${G3LOG_LIBRARY} STATIC ${SRC_FILES})
ENDIF()

SET(${G3LOG_LIBRARY}_VERSION_STRING ${VERSION})
MESSAGE( STATUS "Creating ${G3LOG_LIBRARY} VERSION: ${VERSION}" )
SET(${G3LOG_LIBRARY}_VERSION_STRING ${VERSION})
MESSAGE( STATUS "Creating ${G3LOG_LIBRARY} VERSION: ${VERSION}" )

SET_TARGET_PROPERTIES(${G3LOG_LIBRARY} PROPERTIES
LINKER_LANGUAGE CXX
OUTPUT_NAME g3log
CLEAN_DIRECT_OUTPUT 1
SOVERSION ${VERSION}
)
SET_TARGET_PROPERTIES(${G3LOG_LIBRARY} PROPERTIES
LINKER_LANGUAGE CXX
OUTPUT_NAME g3log
CLEAN_DIRECT_OUTPUT 1
SOVERSION ${VERSION}
)

IF(APPLE)
SET_TARGET_PROPERTIES(${G3LOG_LIBRARY} PROPERTIES MACOSX_RPATH TRUE)
ENDIF()
IF(APPLE)
SET_TARGET_PROPERTIES(${G3LOG_LIBRARY} PROPERTIES MACOSX_RPATH TRUE)
ENDIF()

# require here some proxy for c++14 standard to avoid problems TARGET_PROPERTY CXX_STANDARD
TARGET_COMPILE_FEATURES(${G3LOG_LIBRARY} PUBLIC cxx_variable_templates)
Expand All @@ -105,7 +105,7 @@ IF(NOT(MSVC OR MINGW))
if(Backtrace_FOUND)
TARGET_INCLUDE_DIRECTORIES(${G3LOG_LIBRARY} PRIVATE ${Backtrace_INCLUDE_DIRS})
TARGET_LINK_LIBRARIES(${G3LOG_LIBRARY} ${Backtrace_LIBRARIES})
else()
else()
message( FATAL_ERROR "Could not find Library to create backtraces")
endif()

Expand Down Expand Up @@ -134,7 +134,7 @@ target_compile_options(${G3LOG_LIBRARY} PRIVATE
# add GCC specific stuff
target_compile_options(${G3LOG_LIBRARY} PRIVATE
# clang/GCC warnings
$<$<CXX_COMPILER_ID:GNU>:-rdynamic>
$<$<AND:$<CXX_COMPILER_ID:GNU>,$<NOT:$<BOOL:${MINGW}>>>:-rdynamic>
)

#cmake -DCMAKE_CXX_COMPILER=clang++ ..
Expand Down Expand Up @@ -164,7 +164,7 @@ IF(MSVC OR MINGW)
message( STATUS "" )
ENDIF()

TARGET_COMPILE_OPTIONS(${G3LOG_LIBRARY} PRIVATE
TARGET_COMPILE_OPTIONS(${G3LOG_LIBRARY} PRIVATE
$<$<CXX_COMPILER_ID:MSVC>:/utf-8> # source code already in utf-8, force it for compilers in non-utf8_windows_locale
$<$<CXX_COMPILER_ID:MSVC>:$<$<EQUAL:4,${CMAKE_SIZEOF_VOID_P}>:/arch:IA32>>
)
3 changes: 1 addition & 2 deletions src/crashhandler_unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ namespace g3 {
std::ostringstream oss;
for (size_t idx = 1; idx < size && messages != nullptr; ++idx) {
char* mangled_name = 0, *offset_begin = 0, *offset_end = 0;
// find parantheses and +address offset surrounding mangled name
// find parentheses and +address offset surrounding mangled name
for (char* p = messages[idx]; *p; ++p) {
if (*p == '(') {
mangled_name = p;
Expand Down Expand Up @@ -299,4 +299,3 @@ namespace g3 {
installSignalHandler();
}
} // end namespace g3

2 changes: 1 addition & 1 deletion src/crashhandler_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ namespace g3 {

#if !(defined(DISABLE_VECTORED_EXCEPTIONHANDLING))
// const size_t kFirstExceptionHandler = 1;
// kFirstExeptionsHandler is kept here for documentation purposes.
// kFirstExceptionsHandler is kept here for documentation purposes.
// The last exception seems more like what we want.
const size_t kLastExceptionHandler = 0;
g_vector_exception_handler = AddVectoredExceptionHandler(kLastExceptionHandler, vectorExceptionHandling);
Expand Down
5 changes: 2 additions & 3 deletions src/filesink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace g3 {
,_log_file_with_path(log_directory)
, _log_prefix_backup(log_prefix)
, _outptr(new std::ofstream)
, _header("\t\tLOG format: [YYYY/MM/DD hh:mm:ss uuu* LEVEL FILE->FUNCTION:LINE] messagen\n\t\t(uuu*: microseconds fractions of the seconds value)\n\n")
, _header("\t\tLOG format: [YYYY/MM/DD hh:mm:ss uuu* LEVEL FILE->FUNCTION:LINE] message\n\n\t\t(uuu*: microseconds fractions of the seconds value)\n\n")
, _firstEntry(true)
{
_log_prefix_backup = prefixSanityFix(log_prefix);
Expand All @@ -38,7 +38,6 @@ namespace g3 {
_outptr = createLogFile(_log_file_with_path);
}
assert(_outptr && "cannot open log file at startup");

}


Expand Down Expand Up @@ -91,6 +90,7 @@ namespace g3 {
filestream() << now_formatted << ss_change.str();
return _log_file_with_path;
}

std::string FileSink::fileName() {
return _log_file_with_path;
}
Expand All @@ -103,7 +103,6 @@ namespace g3 {
_header = change;
}


void FileSink::addLogFileHeader() {
filestream() << header(_header);
}
Expand Down
27 changes: 12 additions & 15 deletions src/g3log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* Filename:g3log.cpp Framework for Logging and Design By Contract
* Created: 2011 by Kjell Hedström
*
* PUBLIC DOMAIN and Not copywrited since it was built on public-domain software and at least in "spirit" influenced
* PUBLIC DOMAIN and Not copyrighted since it was built on public-domain software and at least in "spirit" influenced
* from the following sources
* 1. kjellkod.cc ;)
* 2. Dr.Dobbs, Petru Marginean: http://drdobbs.com/article/printableArticle.jhtml?articleId=201804215&dept_url=/cpp/
Expand Down Expand Up @@ -38,9 +38,9 @@ namespace {
g3::LogWorker* g_logger_instance = nullptr; // instantiated and OWNED somewhere else (main)
std::mutex g_logging_init_mutex;

std::unique_ptr<g3::LogMessage> g_first_unintialized_msg = {nullptr};
std::unique_ptr<g3::LogMessage> g_first_uninitialized_msg = {nullptr};
std::once_flag g_set_first_uninitialized_flag;
std::once_flag g_save_first_unintialized_flag;
std::once_flag g_save_first_uninitialized_flag;
const std::function<void(void)> g_pre_fatal_hook_that_does_nothing = [] { /*does nothing */};
std::function<void(void)> g_fatal_pre_logging_hook;

Expand Down Expand Up @@ -74,17 +74,17 @@ namespace g3 {
}

// Save the first uninitialized message, if any
std::call_once(g_save_first_unintialized_flag, [&bgworker] {
if (g_first_unintialized_msg) {
bgworker->save(LogMessagePtr {std::move(g_first_unintialized_msg)});
std::call_once(g_save_first_uninitialized_flag, [&bgworker] {
if (g_first_uninitialized_msg) {
bgworker->save(LogMessagePtr {std::move(g_first_uninitialized_msg)});
}
});

g_logger_instance = bgworker;
// by default the pre fatal logging hook does nothing
// if it WOULD do something it would happen in
setFatalPreLoggingHook(g_pre_fatal_hook_that_does_nothing);
// recurvise crash counter re-set to zero
// recursive crash counter re-set to zero
g_fatal_hook_recursive_counter.store(0);
}

Expand Down Expand Up @@ -153,7 +153,7 @@ namespace g3 {



/** explicits copy of all input. This is makes it possibly to use g3log across dynamically loaded libraries
/** explicitly copy of all input. This is makes it possibly to use g3log across dynamically loaded libraries
* i.e. (dlopen + dlsym) */
void saveMessage(const char* entry, const char* file, int line, const char* function, const LEVELS& level,
const char* boolean_expression, int fatal_signal, const char* stack_trace) {
Expand Down Expand Up @@ -195,7 +195,7 @@ namespace g3 {

/**
* save the message to the logger. In case of called before the logger is instantiated
* the first message will be saved. Any following subsequent unitnialized log calls
* the first message will be saved. Any following subsequent uninitialized log calls
* will be ignored.
*
* The first initialized log entry will also save the first uninitialized log message, if any
Expand All @@ -205,10 +205,10 @@ namespace g3 {
// Uninitialized messages are ignored but does not CHECK/crash the logger
if (!internal::isLoggingInitialized()) {
std::call_once(g_set_first_uninitialized_flag, [&] {
g_first_unintialized_msg = incoming.release();
g_first_uninitialized_msg = incoming.release();
std::string err = {"LOGGER NOT INITIALIZED:\n\t\t"};
err.append(g_first_unintialized_msg->message());
std::string& str = g_first_unintialized_msg->write();
err.append(g_first_uninitialized_msg->message());
std::string& str = g_first_uninitialized_msg->write();
str.clear();
str.append(err); // replace content
std::cerr << str << std::endl;
Expand Down Expand Up @@ -252,6 +252,3 @@ namespace g3 {

} // internal
} // g3



2 changes: 1 addition & 1 deletion src/g3log/g3log.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ namespace g3 {
*
* Set a function-hook before a fatal message will be sent to the logger
* i.e. this is a great place to put a break point, either in your debugger
* or programatically to catch LOG(FATAL), CHECK(...) or an OS fatal event (exception or signal)
* or programmatically to catch LOG(FATAL), CHECK(...) or an OS fatal event (exception or signal)
* This will be reset to default (does nothing) at initializeLogging(...);
*
* Example usage:
Expand Down
9 changes: 4 additions & 5 deletions src/g3log/loglevels.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ struct LEVELS {

// If you want to add any extra logging level then please add to your own source file the logging level you need
// 1. If the cmake option G3_DYNAMIC_LOGGING is enabled then you must use g3::only_change_at_initialization::addLogLevel(...).
// to give g3log a record of your logging level and if it is an enabled or disbled logging level.
// to give g3log a record of your logging level and if it is an enabled or disbled logging level.
//
// 2. If the cmake dynamic logging option is turned OFF
// then giving g3log a record of your logging level with 'addLogLevel(...) is NOT needed since no "disbled/enabled"
Expand All @@ -81,7 +81,7 @@ struct LEVELS {
//
// ... another example, when G3_DYNAMIC_LOGGING is enabled
// 'addLogLevel' is NOT required
// LOG(MYFATL) << "this will just work, and it will be counted as a FATAL event";
// LOG(MYFATAL) << "this will just work, and it will be counted as a FATAL event";
namespace g3 {
static const int kDebugValue = 100;
static const int kInfoValue = 300;
Expand Down Expand Up @@ -156,7 +156,7 @@ namespace g3 {


namespace log_levels {
/// Enable log level >= log_level.
/// Enable log level >= log_level.
/// log levels below will be disabled
/// log levels equal or higher will be enabled.
void setHighest(LEVELS level);
Expand All @@ -182,12 +182,11 @@ namespace g3 {
std::map<int, g3::LoggingLevel> getAll();

enum class status {Absent, Enabled, Disabled};
status getStatus(LEVELS level);
status getStatus(LEVELS level);
} // log_levels

#endif
/// Enabled status for the given logging level
bool logLevel(LEVELS level);

} // g3

2 changes: 1 addition & 1 deletion src/g3log/logworker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* Filename:g3logworker.h Framework for Logging and Design By Contract
* Created: 2011 by Kjell Hedström
*
* PUBLIC DOMAIN and Not copywrited. First published at KjellKod.cc
* PUBLIC DOMAIN and Not copyrighted. First published at KjellKod.cc
* ********************************************* */
#include "g3log/g3log.hpp"
#include "g3log/sinkwrapper.hpp"
Expand Down
3 changes: 1 addition & 2 deletions src/g3log/sink.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace g3 {
//
// The Sink will wrap either
// a Sink with Message object receiving call
// or a Sink with a LogEntry (string) receving call
// or a Sink with a LogEntry (string) receiving call
//
// The Sink can also be used through the SinkHandler to call Sink specific function calls
// Ref: send(Message) deals with incoming log entries (converted if necessary to string)
Expand Down Expand Up @@ -76,4 +76,3 @@ namespace g3 {
};
} // internal
} // g3

4 changes: 2 additions & 2 deletions src/logworker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ namespace g3 {
std::string exiting = {"Fatal type: "};

uniqueMsg->write().append("). ").append(exiting).append(" ").append(reason)
.append("\nLog content flushed sucessfully to sink\n\n");
.append("\nLog content flushed successfully to sink\n\n");

std::cerr << uniqueMsg->toString() << std::flush;
for (auto& sink : _sinks) {
Expand Down Expand Up @@ -90,7 +90,7 @@ namespace g3 {
// be no thread that manages to add another sink after the call to clear the sinks above.
// i.e. this manages the extremely unlikely case of another thread calling
// addWrappedSink after the sink clear above. Normally adding of sinks should be done in main.cpp
// and be closely coupled with the existance of the LogWorker. Sharing this adding of sinks to
// and be closely coupled with the existence of the LogWorker. Sharing this adding of sinks to
// other threads that do not know the state of LogWorker is considered a bug but it is dealt with
// nonetheless below.
//
Expand Down
Loading

0 comments on commit c0ae589

Please sign in to comment.