diff --git a/.clang-format b/.clang-format
new file mode 100644
index 0000000..f5b2f89
--- /dev/null
+++ b/.clang-format
@@ -0,0 +1,83 @@
+# Google C/C++ Code Style settings
+# https://clang.llvm.org/docs/ClangFormatStyleOptions.html
+# Author: Kehan Xue, kehan.xue (at) gmail.com
+Language: Cpp
+BasedOnStyle: Google
+AccessModifierOffset: -1
+AlignAfterOpenBracket: Align
+AlignConsecutiveAssignments: None
+AlignOperands: Align
+AllowAllArgumentsOnNextLine: true
+AllowAllConstructorInitializersOnNextLine: true
+AllowAllParametersOfDeclarationOnNextLine: false
+AllowShortBlocksOnASingleLine: Empty
+AllowShortCaseLabelsOnASingleLine: false
+AllowShortFunctionsOnASingleLine: Inline
+AllowShortIfStatementsOnASingleLine: Never # To avoid conflict, set this "Never" and each "if statement" should include brace when coding
+AllowShortLambdasOnASingleLine: Inline
+AllowShortLoopsOnASingleLine: false
+AlwaysBreakAfterReturnType: None
+AlwaysBreakTemplateDeclarations: Yes
+BinPackArguments: true
+PackConstructorInitializers: Never
+BreakBeforeBraces: Attach
+BraceWrapping:
+ AfterCaseLabel: false
+ AfterClass: false
+ AfterStruct: false
+ AfterControlStatement: Never
+ AfterEnum: false
+ AfterFunction: false
+ AfterNamespace: false
+ AfterUnion: false
+ AfterExternBlock: false
+ BeforeCatch: false
+ BeforeElse: false
+ BeforeLambdaBody: false
+ IndentBraces: false
+ SplitEmptyFunction: false
+ SplitEmptyRecord: false
+ SplitEmptyNamespace: false
+BreakBeforeBinaryOperators: None
+BreakBeforeTernaryOperators: true
+BreakConstructorInitializers: AfterColon
+BreakInheritanceList: BeforeColon
+ColumnLimit: 0
+CompactNamespaces: false
+ContinuationIndentWidth: 3
+Cpp11BracedListStyle: true
+DerivePointerAlignment: false # Make sure the * or & align on the left
+EmptyLineBeforeAccessModifier: LogicalBlock
+FixNamespaceComments: true
+IncludeBlocks: Preserve
+IndentCaseLabels: false
+IndentPPDirectives: None
+IndentWidth: 3
+KeepEmptyLinesAtTheStartOfBlocks: true
+MaxEmptyLinesToKeep: 1
+NamespaceIndentation: All
+ObjCSpaceAfterProperty: false
+ObjCSpaceBeforeProtocolList: true
+PointerAlignment: Left
+ReflowComments: false
+# SeparateDefinitionBlocks: Always # Only support since clang-format 14
+SpaceAfterCStyleCast: false
+SpaceAfterLogicalNot: false
+SpaceAfterTemplateKeyword: true
+SpaceBeforeAssignmentOperators: true
+SpaceBeforeCpp11BracedList: false
+SpaceBeforeCtorInitializerColon: true
+SpaceBeforeInheritanceColon: true
+SpaceBeforeParens: ControlStatements
+SpaceBeforeRangeBasedForLoopColon: true
+SpaceBeforeSquareBrackets: false
+SpaceInEmptyParentheses: false
+SpacesBeforeTrailingComments: 2
+SpacesInAngles: false
+SpacesInCStyleCastParentheses: false
+SpacesInContainerLiterals: false
+SpacesInParentheses: false
+SpacesInSquareBrackets: false
+Standard: c++11
+TabWidth: 3
+UseTab: Never
\ No newline at end of file
diff --git a/.vscode/settings.json b/.vscode/settings.json
new file mode 100644
index 0000000..09894bb
--- /dev/null
+++ b/.vscode/settings.json
@@ -0,0 +1,4 @@
+{
+ "cmake.configureOnOpen": false,
+ "editor.formatOnSave": true
+}
diff --git a/PULL_REQUEST_TEMPLATE.md b/PULL_REQUEST_TEMPLATE.md
index a95fcdb..1232346 100644
--- a/PULL_REQUEST_TEMPLATE.md
+++ b/PULL_REQUEST_TEMPLATE.md
@@ -3,16 +3,18 @@
`ADD CONTENT HERE TO DESCRIBE THE PURPOSE OF THE PULL REQUEST`
+
+# Formatting
+- [ ] I am following the formatting style of the existing codebase.
+
+_a clang-format configuration file is available in the root of g3log_
+
# Testing
- [ ] This new/modified code was covered by unit tests.
-
- [ ] (insight) Was all tests written using TDD (Test Driven Development) style?
-
- [ ] The CI (Windows, Linux, OSX) are working without issues.
-
- [ ] Was new functionality documented?
-
- [ ] The testing steps 1 - 2 below were followed
_step 1_
diff --git a/docs/building.md b/docs/building.md
index cae52f5..0883b7b 100644
--- a/docs/building.md
+++ b/docs/building.md
@@ -213,7 +213,7 @@ will install the g3log library to `CPACK_PACKAGING_INSTALL_PREFIX`.
## Testing
-By default, tests will not be built. To enable unit testing, you should turn on `ADD_G3LOG_UNIT_TEST`.
+By default, tests will be built. To disable unit testing, you should turn off `ADD_G3LOG_UNIT_TEST`.
Suppose the build process has completed, then you can run the tests with:
```
diff --git a/example/main_contract.cpp b/example/main_contract.cpp
index e921cfd..950e1dd 100644
--- a/example/main_contract.cpp
+++ b/example/main_contract.cpp
@@ -9,34 +9,29 @@
#include
#include
#include
-#include
#include
+#include
-
-namespace
-{
+namespace {
#if (defined(WIN32) || defined(_WIN32) || defined(__WIN32__))
const std::string path_to_log_file = "./";
#else
const std::string path_to_log_file = "/tmp/";
#endif
-}
+} // namespace
-namespace example_fatal
-{
- void killWithContractIfNonEqual(int first, int second)
- {
+namespace example_fatal {
+ void killWithContractIfNonEqual(int first, int second) {
CHECK(first == second) << "Test to see if contract works: onetwothree: " << 123 << ". This should be at the end of the log, and will exit this example";
}
-} // example fatal
+} // namespace example_fatal
-int main(int argc, char **argv)
-{
+int main(int argc, char** argv) {
double pi_d = 3.1415926535897932384626433832795;
float pi_f = 3.1415926535897932384626433832795f;
auto worker = g3::LogWorker::createLogWorker();
- auto handle= worker->addDefaultLogger(argv[0], path_to_log_file);
+ auto handle = worker->addDefaultLogger(argv[0], path_to_log_file);
g3::initializeLogging(worker.get());
std::future log_file_name = handle->call(&g3::FileSink::fileName);
@@ -49,11 +44,11 @@ int main(int argc, char **argv)
changeFormatting.wait();
changeHeader.wait();
-
std::cout << "* This is an example of g3log. It WILL exit by a failed CHECK(...)" << std::endl;
std::cout << "* that acts as a FATAL trigger. Please see the generated log and " << std::endl;
std::cout << "* compare to the code at:\n* \t g3log/test_example/main_contract.cpp" << std::endl;
- std::cout << "*\n* Log file: [" << log_file_name.get() << "]\n\n" << std::endl;
+ std::cout << "*\n* Log file: [" << log_file_name.get() << "]\n\n"
+ << std::endl;
LOGF(INFO, "Hi log %d", 123);
LOG(INFO) << "Test SLOG INFO";
@@ -72,4 +67,3 @@ int main(int argc, char **argv)
int larger = 2;
example_fatal::killWithContractIfNonEqual(smaller, larger);
}
-
diff --git a/example/main_fatal_choice.cpp b/example/main_fatal_choice.cpp
index 9fe5c75..50c3359 100644
--- a/example/main_fatal_choice.cpp
+++ b/example/main_fatal_choice.cpp
@@ -9,14 +9,14 @@
#include
#include
-#include
#include
-#include
-#include
-#include
#include
-#include
#include
+#include
+#include
+#include
+#include
+#include
#ifndef _MSC_VER
#define NOEXCEPT noexcept
@@ -24,17 +24,15 @@
#define NOEXCEPT throw()
#endif
-namespace
-{
+namespace {
#if (defined(WIN32) || defined(_WIN32) || defined(__WIN32__))
const std::string path_to_log_file = "./";
#else
const std::string path_to_log_file = "/tmp/";
#endif
- void ToLower(std::string &str)
- {
- for (auto &character : str) {
+ void ToLower(std::string& str) {
+ for (auto& character : str) {
character = std::tolower(character);
}
}
@@ -75,7 +73,7 @@ namespace
int gShouldBeZero = 1;
void DivisionByZero() {
- LOG(G3LOG_DEBUG) << " trigger exit Executing DivisionByZero: gShouldBeZero: " << gShouldBeZero;
+ LOG(G3LOG_DEBUG) << " trigger exit Executing DivisionByZero: gShouldBeZero: " << gShouldBeZero;
LOG(INFO) << "Division by zero is a big no-no";
int value = 3;
auto test = value / gShouldBeZero;
@@ -96,10 +94,9 @@ namespace
LOG(WARNING) << "Expected to have died by now...";
}
-
void AccessViolation() {
LOG(G3LOG_DEBUG) << " trigger exit";
- char *ptr = 0;
+ char* ptr = 0;
LOG(INFO) << "Death by access violation is imminent";
*ptr = 0;
LOG(WARNING) << "Expected to have died by now...";
@@ -119,8 +116,7 @@ namespace
f2.wait();
}
-
- using deathfunc = void (*) (void);
+ using deathfunc = void (*)(void);
void Death_x10000(deathfunc func, std::string funcname) NOEXCEPT {
LOG(G3LOG_DEBUG) << " trigger exit";
std::vector> asyncs;
@@ -136,19 +132,20 @@ namespace
std::cout << __FUNCTION__ << " unexpected result. Death by " << funcname << " did not crash and exit the system" << std::endl;
}
-
void Throw() NOEXCEPT {
LOG(G3LOG_DEBUG) << " trigger exit";
std::future empty;
- empty.get();
+ empty.get();
// --> thows future_error http://en.cppreference.com/w/cpp/thread/future_error
// example of std::exceptions can be found here: http://en.cppreference.com/w/cpp/error/exception
}
-
void SegFaultAttempt_x10000() NOEXCEPT {
-
- deathfunc f = []{char* ptr = 0; *ptr = 1; };
+
+ deathfunc f = [] {
+ char* ptr = 0;
+ *ptr = 1;
+ };
Death_x10000(f, "throw uncaught exception... and then some sigsegv calls");
}
@@ -169,28 +166,57 @@ namespace
CallActualExitFunction(fatal_function);
}
-
-
void ExecuteDeathFunction(const bool runInNewThread, int fatalChoice) {
LOG(G3LOG_DEBUG) << "trigger exit";
auto exitFunction = &NoExitFunction;
switch (fatalChoice) {
- case 1: exitFunction = &RaiseSIGABRT; break;
- case 2: exitFunction = &RaiseSIGFPE; break;
- case 3: exitFunction = &RaiseSIGSEGV; break;
- case 4: exitFunction = &RaiseSIGILL; break;
- case 5: exitFunction = &RAiseSIGTERM; break;
- case 6: exitFunction = &DivisionByZero; gShouldBeZero = 0; DivisionByZero(); break;
- case 7: exitFunction = &IllegalPrintf; break;
- case 8: exitFunction = &OutOfBoundsArrayIndexing; break;
- case 9: exitFunction = &AccessViolation; break;
- case 10: exitFunction = &RaiseSIGABRTAndAccessViolation; break;
- case 11: exitFunction = &Throw; break;
- case 12: exitFunction = &FailedCHECK; break;
- case 13: exitFunction = &AccessViolation_x10000; break;
- case 14: exitFunction = &SegFaultAttempt_x10000; break;
- default: break;
+ case 1:
+ exitFunction = &RaiseSIGABRT;
+ break;
+ case 2:
+ exitFunction = &RaiseSIGFPE;
+ break;
+ case 3:
+ exitFunction = &RaiseSIGSEGV;
+ break;
+ case 4:
+ exitFunction = &RaiseSIGILL;
+ break;
+ case 5:
+ exitFunction = &RAiseSIGTERM;
+ break;
+ case 6:
+ exitFunction = &DivisionByZero;
+ gShouldBeZero = 0;
+ DivisionByZero();
+ break;
+ case 7:
+ exitFunction = &IllegalPrintf;
+ break;
+ case 8:
+ exitFunction = &OutOfBoundsArrayIndexing;
+ break;
+ case 9:
+ exitFunction = &AccessViolation;
+ break;
+ case 10:
+ exitFunction = &RaiseSIGABRTAndAccessViolation;
+ break;
+ case 11:
+ exitFunction = &Throw;
+ break;
+ case 12:
+ exitFunction = &FailedCHECK;
+ break;
+ case 13:
+ exitFunction = &AccessViolation_x10000;
+ break;
+ case 14:
+ exitFunction = &SegFaultAttempt_x10000;
+ break;
+ default:
+ break;
}
if (runInNewThread) {
auto dieInNearFuture = std::async(std::launch::async, CallExitFunction, exitFunction);
@@ -200,12 +226,10 @@ namespace
}
std::string unexpected = "Expected to exit by FATAL event. That did not happen (printf choice in Windows?).";
- unexpected.append("Choice was: ").append(std::to_string(fatalChoice)).append(", async?: ")
- .append(std::to_string(runInNewThread)).append("\n\n***** TEST WILL RUN AGAIN *****\n\n");
+ unexpected.append("Choice was: ").append(std::to_string(fatalChoice)).append(", async?: ").append(std::to_string(runInNewThread)).append("\n\n***** TEST WILL RUN AGAIN *****\n\n");
- std::cerr << unexpected << std::endl;
+ std::cerr << unexpected << std::endl;
LOG(WARNING) << unexpected;
-
}
bool AskForAsyncDeath() {
@@ -224,8 +248,6 @@ namespace
return ("yes" == option);
}
-
-
int ChoiceOfFatalExit() {
std::string option;
int choice = {0};
@@ -257,7 +279,7 @@ namespace
choice = std::stoi(option);
if (choice <= 0 || choice > 14) {
std::cout << "Invalid choice: [" << option << "\n\n";
- } else {
+ } else {
return choice;
}
} catch (...) {
@@ -275,11 +297,11 @@ namespace
const int exitChoice = ChoiceOfFatalExit();
ForwardChoiceForFatalExit(runInNewThread, exitChoice);
}
-} // namespace
+} // namespace
void breakHere() {
std::ostringstream oss;
- oss << "Fatal hook function: " << __FUNCTION__ << ":" << __LINE__ << " was called";
+ oss << "Fatal hook function: " << __FUNCTION__ << ":" << __LINE__ << " was called";
oss << " through g3::setFatalPreLoggingHook(). setFatalPreLoggingHook should be called AFTER g3::initializeLogging()" << std::endl;
LOG(G3LOG_DEBUG) << oss.str();
#if (defined(WIN32) || defined(_WIN32) || defined(__WIN32__))
@@ -287,10 +309,9 @@ void breakHere() {
#endif
}
-int main(int argc, char **argv)
-{
+int main(int argc, char** argv) {
auto worker = g3::LogWorker::createLogWorker();
- auto handle= worker->addDefaultLogger(argv[0], path_to_log_file);
+ auto handle = worker->addDefaultLogger(argv[0], path_to_log_file);
g3::initializeLogging(worker.get());
g3::setFatalPreLoggingHook(&breakHere);
std::future log_file_name = handle->call(&g3::FileSink::fileName);
@@ -298,8 +319,8 @@ int main(int argc, char **argv)
std::cout << "**** G3LOG FATAL EXAMPLE ***\n\n"
<< "Choose your type of fatal exit, then "
<< " read the generated log and backtrace.\n"
- << "The logfile is generated at: [" << log_file_name.get() << "]\n\n" << std::endl;
-
+ << "The logfile is generated at: [" << log_file_name.get() << "]\n\n"
+ << std::endl;
LOGF(G3LOG_DEBUG, "Fatal exit example starts now, it's as easy as %d", 123);
LOG(INFO) << "Feel free to read the source code also in g3log/example/main_fatal_choice.cpp";
@@ -311,6 +332,4 @@ int main(int argc, char **argv)
LOG(WARNING) << "Expected to exit by fatal event, this code line should never be reached";
CHECK(false) << "Forced death";
return 0;
-
}
-
diff --git a/example/main_sigsegv.cpp b/example/main_sigsegv.cpp
index 8000a11..044b747 100644
--- a/example/main_sigsegv.cpp
+++ b/example/main_sigsegv.cpp
@@ -10,62 +10,54 @@
#include
#include
-#include
#include
#include
-namespace
-{
+#include
+namespace {
#if (defined(WIN32) || defined(_WIN32) || defined(__WIN32__))
const std::string path_to_log_file = "./";
#else
const std::string path_to_log_file = "/tmp/";
#endif
-}
+} // namespace
-namespace example_fatal
-{
+namespace example_fatal {
// on Ubunti this caused get a compiler warning with gcc4.6
// from gcc 4.7.2 (at least) it causes a crash (as expected)
// On windows it'll probably crash too.
- void tryToKillWithIllegalPrintout()
- {
- std::cout << "\n\n***** Be ready this last example may 'abort' if on Windows/Linux_gcc4.7 " << std::endl << std::flush;
- std::cout << "************************************************************\n\n" << std::endl << std::flush;
+ void tryToKillWithIllegalPrintout() {
+ std::cout << "\n\n***** Be ready this last example may 'abort' if on Windows/Linux_gcc4.7 " << std::endl
+ << std::flush;
+ std::cout << "************************************************************\n\n"
+ << std::endl
+ << std::flush;
std::this_thread::sleep_for(std::chrono::seconds(1));
const std::string logging = "logging";
LOGF(G3LOG_DEBUG, "ILLEGAL PRINTF_SYNTAX EXAMPLE. WILL GENERATE compiler warning.\n\nbadly formatted message:[Printf-type %s is the number 1 for many %s]", logging.c_str());
}
-
// The function above 'tryToKillWithIllegalPrintout' IS system / compiler dependent. Older compilers sometimes did NOT generate a SIGSEGV
// fault as expected by the illegal printf-format usage. just in case we exit by zero division"
- void killByZeroDivision(int value)
- {
- int zero = 0; // trying to fool the compiler to automatically warn
+ void killByZeroDivision(int value) {
+ int zero = 0; // trying to fool the compiler to automatically warn
LOG(INFO) << "This is a bad operation [value/zero] : " << value / zero;
}
-
void tryToKillWithAccessingIllegalPointer(std::unique_ptr badStringPtr) {
auto badPtr = std::move(badStringPtr);
LOG(INFO) << "Function calls through a nullptr object will trigger SIGSEGV";
badStringPtr->append("crashing");
}
+} // namespace example_fatal
-} // example fatal
-
-
-
-int main(int argc, char **argv)
-{
+int main(int argc, char** argv) {
double pi_d = 3.1415926535897932384626433832795;
float pi_f = 3.1415926535897932384626433832795f;
using namespace g3;
-
- std::unique_ptr logworker {LogWorker::createLogWorker()};
+ std::unique_ptr logworker{LogWorker::createLogWorker()};
auto sinkHandle = logworker->addSink(std::make_unique(argv[0], path_to_log_file),
&FileSink::fileWrite);
@@ -74,8 +66,8 @@ int main(int argc, char **argv)
std::cout << "* This is an example of g3log. It WILL exit by a FATAL trigger" << std::endl;
std::cout << "* Please see the generated log and compare to the code at" << std::endl;
std::cout << "* g3log/test_example/main.cpp" << std::endl;
- std::cout << "*\n* Log file: [" << log_file_name.get() << "]\n\n" << std::endl;
-
+ std::cout << "*\n* Log file: [" << log_file_name.get() << "]\n\n"
+ << std::endl;
LOGF(INFO, "Hi log %d", 123);
LOG(INFO) << "Test SLOG INFO";
@@ -111,8 +103,8 @@ int main(int argc, char **argv)
std::unique_ptr badStringPtr;
example_fatal::tryToKillWithAccessingIllegalPointer(std::move(badStringPtr));
- // what happened? OK. let us just exit with SIGFPE
- int value = 1; // system dependent but it SHOULD never reach this line
+ // what happened? OK. let us just exit with SIGFPE
+ int value = 1; // system dependent but it SHOULD never reach this line
example_fatal::killByZeroDivision(value);
return 0;
}
diff --git a/src/crashhandler_unix.cpp b/src/crashhandler_unix.cpp
index df18b23..dacb575 100644
--- a/src/crashhandler_unix.cpp
+++ b/src/crashhandler_unix.cpp
@@ -7,27 +7,26 @@
* ============================================================================*/
#include "g3log/crashhandler.hpp"
-#include "g3log/logmessage.hpp"
#include "g3log/logcapture.hpp"
#include "g3log/loglevels.hpp"
+#include "g3log/logmessage.hpp"
#if (defined(WIN32) || defined(_WIN32) || defined(__WIN32__))
#error "crashhandler_unix.cpp used but it's a windows system"
#endif
-
-#include
-#include
-#include
-#include
#include
-#include
-#include
-#include
-#include
+#include
+#include
#include
+#include
+#include
+#include
+#include
#include