Skip to content

Commit

Permalink
adopts google c++ style guide (#9)
Browse files Browse the repository at this point in the history
* done refactoring zj-basics

* doing log

* utility to do

* timer done

* all changed

* all done

* tests examples updated

* up

* up

* up
  • Loading branch information
zongyaojin authored Nov 23, 2023
1 parent e01840d commit f9f3c67
Show file tree
Hide file tree
Showing 61 changed files with 1,264 additions and 1,292 deletions.
8 changes: 7 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
"*.prototxt": "proto",
"*.hqt": "cpp",
"*.primitives": "xml",
"*.lr": "jinja-html",
"*.lektorproject": "ini",
"*.ini": "ini",
"*.html": "html",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
Expand Down Expand Up @@ -76,9 +80,11 @@
"fstream": "cpp",
"forward_list": "cpp",
"csignal": "cpp",
"codecvt": "cpp"
"codecvt": "cpp",
"dense": "cpp"
},
"cSpell.words": [
"chrono",
"cppcheck",
"cpplint",
"Eigen",
Expand Down
25 changes: 9 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,12 @@
[![C++ Build](https://github.com/zongyaojin/zj-base/actions/workflows/cpp-build.yml/badge.svg)](https://github.com/zongyaojin/zj-base/actions/workflows/cpp-build.yml)
[![GitHub license](https://img.shields.io/badge/license-Apache--2.0-blue.svg)](https://github.com/zongyaojin/zj-base/blob/main/LICENSE)

This package contains some basic tools such as logging and debugging for C++ projects.

- `ZjChrono` provides some time-related functions, definitions, and aliases
- `ZjSingleton` provides a singleton base class using the Curiously Recurring Template Pattern

- `ZjDebug.hpp` provides macros for print, try-catch, and throw mechanisms that can report error with call site and stack trace information
- `ZjVerifyNumerics.hpp` provides a macro to check Eigen or std variable singularity
- `ZjLogMacroExtensions.hpp` provides macros for assertion and regular, condition-based, and periodic message with built-in log support

- `ZjCsvLog` provides a singleton class that can create as many files as needed and log Eigen vectors with time stamp to a given file in a thread-safe fashion
- `ZjProgramSwitch` provides a singleton class that can be used as a global program switch and run user-specified functions when it turns off
- `ZjTimer` provides a timer class that can be used to guard a periodic loop of a certain frequency
- `ZjUtility.hpp` provides some functions for map access and pointer clean up

- `[./examples/sigint-sigsegv-handling/]` shows how to handle a Ctrl-C or a Segmentation Fault terminated program
- `[./examples/compile-definitions/]` shows the usage of some compile definitions provided by the [zj-cmake](https://github.com/zongyaojin/zj-cmake/tree/main) submodule
This package provides some basic tools for C++ projects. The package mostly follows the [Google C++ Style Guide](https://google.github.io/styleguide/cppguide.html). But there are a few exceptions:

- headers use the `#pragma once` include guard;
- regular header and source files use the `.hpp` and `.cpp` extensions;
- some global functions start with an underscore, such as `_ZjMessage()`, indicating that they are not intended for client code to use;
- Macro functions start with an underscore, such as `_ZJ_TRY()`
- Macro variables defined within the package do not have leading or trailing underscores, such as `ZJ_PURPLE`;
- Macro variables defined through CMake follow the two underscores pattern, such as `__FOO_BAR__`;
- filenames follow the `foo-bar.xyz` pattern to make their style consistent with packages such as [zj-cmake](https://github.com/zongyaojin/zj-cmake/tree/main) and [zj-common-scripts](https://github.com/zongyaojin/zj-common-scripts).
33 changes: 17 additions & 16 deletions client-project-example/main.cpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
#include "zj-base/zj-log.hpp"
#include "zj-base/zj-utility.hpp"
#include "zj-base/zj-logging.hpp"
#include "zj-base/zj-tools.hpp"

#include <iostream>
#include <eigen3/Eigen/Dense>

#include "Eigen/Dense"
#include "spdlog/spdlog.h"

void foo()
void Foo()
{
_ZJ_DEBUG("\n\nin foo\n\n");
_ZJ_THROW("intentional fault");
}

void bar()
void Bar()
{
_ZJ_DEBUG("\n\nin bar\n\n");
_ZJ_TRY(foo());
_ZJ_TRY(Foo());
}

int main()
{
ZjLog::getInstance().init(std::string {__EXAMPLE_PKG_BUILD_PATH__}, std::string {__EXAMPLE_PKG_BUILD_PATH__});
ZjLogger::GetInstance().Init(std::string {__EXAMPLE_PKG_BUILD_PATH__}, std::string {__EXAMPLE_PKG_BUILD_PATH__});

_ZJ_INFO("hello info");
_ZJ_WARN("hello warn");
Expand All @@ -32,10 +33,10 @@ int main()
spdlog::critical("Consider selecting PUBLIC, INTERFACE, or PRIVATE");
spdlog::critical("=======================================================================");

_ZJ_INFO("time iso: {}", ZjChrono::getTimeIso());
_ZJ_INFO("time iso: {}", ZjGetTimeIso());

try {
bar();
Bar();
} catch (const std::exception& e) {
_ZJ_DEBUG("got intentional exception [{}]", e.what());
}
Expand All @@ -47,15 +48,15 @@ int main()
v2.resize(6);
v2 << 3.14, 3.14, 3.14, 3.14, 3.14, 3.14;

ZjCsvLog::getInstance().log("csv-example-1", v);
ZjCsvLog::getInstance().log("csv-example-2", v2);
ZjCsvLogger::GetInstance().Log("csv-example-1", v);
ZjCsvLogger::GetInstance().Log("csv-example-2", v2);

ZjCsvLog::getInstance().log("csv-example-1", v);
ZjCsvLog::getInstance().log("csv-example-2", v2);
ZjCsvLogger::GetInstance().Log("csv-example-1", v);
ZjCsvLogger::GetInstance().Log("csv-example-2", v2);

ZjCsvLog::getInstance().log("csv-example-1", v);
ZjCsvLog::getInstance().log("csv-example-2", v2);
ZjCsvLog::getInstance().log("csv-example-2", v2);
ZjCsvLogger::GetInstance().Log("csv-example-1", v);
ZjCsvLogger::GetInstance().Log("csv-example-2", v2);
ZjCsvLogger::GetInstance().Log("csv-example-2", v2);

_ZJ_DEBUG("");
_ZJ_INFO("===================================================");
Expand Down
2 changes: 1 addition & 1 deletion cmake/zj-cmake
Submodule zj-cmake updated 1 files
+16 −3 README.md
2 changes: 1 addition & 1 deletion examples/compile-definitions/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "ZjLogMacroExtensions.hpp"
#include "zj-logging-macros-simplified.hpp"

int main()
{
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

#include <eigen3/Eigen/Dense>

void foo();
void Foo();
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include "foo.hpp"
#include "ZjLogMacroExtensions.hpp"
#include "zj-logging-macros-simplified.hpp"

int main()
{
_ZJ_INFO("in main");
foo();
Foo();
return 0;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "foo.hpp"
#include "ZjLogMacroExtensions.hpp"
#include "zj-logging-macros-simplified.hpp"

void foo()
void Foo()
{
Eigen::Vector2d v {3.14, 5.28};
_ZJ_DEBUG("v: {}, {}", v(0), v(1));
Expand Down
34 changes: 17 additions & 17 deletions examples/sigint-sigsegv-handling/main.cpp
Original file line number Diff line number Diff line change
@@ -1,54 +1,54 @@
#include "zj-log.hpp"
#include "zj-utility.hpp"
#include "zj-logging.hpp"
#include "zj-tools.hpp"

#include <csignal>

// Signal handler function for SIGINT (Ctrl-C)
void sigintHandler(int signal)
void SigintHandler(int signal)
{
_ZJ_WARN("Program terminated by Ctrl-C (SIGINT).");

ZjLog::getInstance().shutdown();
ZjProgramSwitch::getInstance().turnOff();
ZjLogger::GetInstance().Shutdown();
ZjProgramSwitch::GetInstance().TurnOff();

exit(signal);
}

// Signal handler function for SIGSEGV (Segmentation fault)
void sigsegvHandler(int signal)
void SigsegvHandler(int signal)
{
_ZJ_WARN("Segmentation fault (SIGSEGV) occurred.");

ZjLog::getInstance().shutdown();
ZjProgramSwitch::getInstance().turnOff();
ZjLogger::GetInstance().Shutdown();
ZjProgramSwitch::GetInstance().TurnOff();

exit(signal);
}

void turnOffRoutine()
void TurnOffRoutine()
{
_ZJ_DEBUG("turn off routine says bye...");
}

int main()
{
ZjProgramSwitch::getInstance().turnOn();
ZjProgramSwitch::getInstance().registerTurnOffRoutine(&turnOffRoutine);
ZjProgramSwitch::GetInstance().TurnOn();
ZjProgramSwitch::GetInstance().RegisterTurnOffRoutine(&TurnOffRoutine);

/// @see <signum_generic.h> for more signals
std::signal(SIGINT, sigintHandler);
std::signal(SIGSEGV, sigsegvHandler);
std::signal(SIGINT, SigintHandler);
std::signal(SIGSEGV, SigsegvHandler);

_ZJ_INFO("hello");

std::vector<int>* ptr = nullptr;

auto startTime = ZjChrono::Clock::now();
auto start_time = ZjChronoClock::now();

while (true) {
auto timeSpent = std::chrono::duration_cast<ZjChrono::Ms>(ZjChrono::Clock::now() - startTime);
_ZJ_INFO_T(1, "Ctrl-C to terminate, or wait 5 sec to trigger segmentation fault, current count: [{}]", timeSpent.count());
if (timeSpent.count() > 5000) {
auto time_spent = std::chrono::duration_cast<ZjChronoMs>(ZjChronoClock::now() - start_time);
_ZJ_INFO_T(1, "Ctrl-C to terminate, or wait 5 sec to trigger segmentation fault, current count: [{}]", time_spent.count());
if (time_spent.count() > 5000) {
_ZJ_WARN("triggering segmentation fault now");
ptr->push_back(1); // cppcheck-suppress nullPointer
}
Expand Down
10 changes: 5 additions & 5 deletions examples/std-any/main.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include "ZjLogMacroExtensions.hpp"
#include "zj-logging-macros-simplified.hpp"
#include <any>
#include "eigen3/Eigen/Dense"
#include <iostream>

void func(const std::any input)
void Func(const std::any input)
{
try {
auto d = std::any_cast<double>(input);
Expand All @@ -16,7 +16,7 @@ void func(const std::any input)
}


void func2(std::any input)
void Func2(std::any input)
{
try {
double* d = std::any_cast<double*>(input);
Expand All @@ -36,9 +36,9 @@ int main()
// std::string a = "2";
// int a = 123;
double a = 1.23;
func(a);
Func(a);

func2(&a);
Func2(&a);

std::cout << a << std::endl;
return 0;
Expand Down
28 changes: 14 additions & 14 deletions examples/throw-and-try/main.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include "ZjDebug.hpp"
#include "ZjLogMacroExtensions.hpp"
#include "zj-debug.hpp"
#include "zj-logging-macros-simplified.hpp"

void f1(int i)
void F1(int i)
{
spdlog::info("in f1");
spdlog::info("in F1");

if (i == 2) {
throw std::invalid_argument("throwing std exception");
Expand All @@ -12,24 +12,24 @@ void f1(int i)
_ZJ_THROW_IF(i != 2, "throwing ZjFault");
}

void f2(int i)
void F2(int i)
{
spdlog::info("in f2");
_ZJ_TRY(f1(i));
spdlog::info("in F2");
_ZJ_TRY(F1(i));
}

void f3(int i)
void F3(int i)
{
spdlog::info("in f3");
_ZJ_TRY(f2(i));
spdlog::info("in F3");
_ZJ_TRY(F2(i));
}

int main()
{
try {
_ZJ_TRY(f3(2));
_ZJ_TRY(F3(2));
} catch (const std::exception& e) {
spdlog::info("got f3(2) exception: {}", e.what());
spdlog::info("got F3(2) exception: {}", e.what());
}

_ZJ_DEBUG("");
Expand All @@ -38,9 +38,9 @@ int main()
_ZJ_DEBUG("");

try {
_ZJ_TRY(f3(1));
_ZJ_TRY(F3(1));
} catch (const std::exception& e) {
spdlog::info("got f3(1) exception: {}", e.what());
spdlog::info("got F3(1) exception: {}", e.what());
}

return 0;
Expand Down
2 changes: 1 addition & 1 deletion examples/timed-msg/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "ZjLogMacroExtensions.hpp"
#include "zj-logging-macros-simplified.hpp"

int main()
{
Expand Down
Loading

0 comments on commit f9f3c67

Please sign in to comment.