C/Cpp: Add main.cpp example, doxygen config

This commit is contained in:
Jannik Beyerstedt 2021-06-01 13:16:47 +02:00
parent f186badabe
commit fd94cf1b57
4 changed files with 4173 additions and 7 deletions

View File

@ -25,7 +25,7 @@ if (CMAKE_BUILD_TYPE STREQUAL "Release" AND CLANG_TIDY)
)
endif()
# generate a header file with the version number from git tag/ commit
# # generate a header file with the version number from git tag/ commit
# execute_process(
# COMMAND git describe --always --tags --dirty
# OUTPUT_VARIABLE GIT_VERSION_TAG
@ -53,7 +53,7 @@ endif()
# add_library(lib_json INTERFACE)
# target_include_directories(lib_json INTERFACE ${PROJECT_SOURCE_DIR}/thirdparty/nlohmann_json)
# # example for a compiled only library: https://github.com/emilk/loguru
# # example for a compiled library: https://github.com/emilk/loguru
# find_package(Threads)
# add_library(lib_loguru STATIC
# ${PROJECT_SOURCE_DIR}/thirdparty/loguru/loguru.cpp
@ -72,14 +72,14 @@ set(SOURCES
# add additional sources here
)
add_executable(ctestapp ${SOURCES})
add_executable(${PROJECT_NAME} ${SOURCES})
# add libraries
# target_link_libraries(ctestapp PRIVATE lib_json)
# target_link_libraries(ctestapp PRIVATE lib_loguru)
# target_link_libraries(${PROJECT_NAME} PRIVATE lib_json)
# target_link_libraries(${PROJECT_NAME} PRIVATE lib_loguru)
# define which compile target should be installed with `make install`
install(TARGETS ctestapp
install(TARGETS ${PROJECT_NAME}
RUNTIME DESTINATION bin)
@ -102,7 +102,7 @@ install(TARGETS ctestapp
# clang-tidy runner of CMake, which is already enabled on Release builds.
if(CLANG_TIDY)
# get include directories and convert them to compiler flags (prepend "-I")
get_target_property(TIDY_INCLUDE_DIRS ctestapp INCLUDE_DIRECTORIES)
get_target_property(TIDY_INCLUDE_DIRS ${PROJECT_NAME} INCLUDE_DIRECTORIES)
foreach(dir IN ITEMS ${TIDY_INCLUDE_DIRS})
list(APPEND TIDY_INCLUDE_DIRS_ARG "-I${dir}")
endforeach()
@ -118,3 +118,30 @@ if(CLANG_TIDY)
${TIDY_INCLUDE_DIRS_ARG}
)
endif()
## CONFIGURE DOXYGEN
find_package(Doxygen)
if (DOXYGEN_FOUND)
set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES ${CMAKE_CURRENT_BINARY_DIR}/docs)
# set input and output files
set(DOXYGEN_IN ${CMAKE_CURRENT_SOURCE_DIR}/docs/Doxyfile.in)
set(DOXYGEN_OUT ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile.out)
set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile.out)
# request to configure the file
configure_file(${DOXYGEN_IN} ${DOXYGEN_OUT} @ONLY)
message("Doxygen build started")
# Note: do not put "ALL" - this builds docs together with application EVERY TIME!
add_custom_target(docs
COMMAND ${DOXYGEN_EXECUTABLE} ${DOXYGEN_OUT}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMENT "Generating API documentation with Doxygen"
VERBATIM
)
else (DOXYGEN_FOUND)
message("Doxygen needs to be installed to generate the documentation")
endif (DOXYGEN_FOUND)

2620
cpp/docs/Doxyfile.in Normal file

File diff suppressed because it is too large Load Diff

1367
cpp/docs/doxygen-awesome.css Normal file

File diff suppressed because it is too large Load Diff

152
cpp/src/main.cpp Normal file
View File

@ -0,0 +1,152 @@
/**
* @file main.cpp
* @brief Main event loop
*
* Here typically goes a more extensive explanation of what the header defines.
*
* @author Name <email@example.com>
* @copyright (c) Company Name/ Author, YEAR
* @copyright Licensed under the Apache License, Version 2.0 <http://www.apache.org/licenses/LICENSE-2.0>
* @copyright Licensed under the GNU GPLv3 License <http://www.gnu.org/licenses/gpl-3.0.txt>
*/
#include <algorithm>
#include <iostream>
#include <thread>
#include <vector>
#include "version.h"
// static configuration
#define LOGFILE_PATH "ctestapp.log"
#define MAINLOOP_SLEEP_US 1
// other default values
#define LOG_VERBOSE 1
#define LOG_DEBUG 2
#define LOG_MAX 9
/**
* @brief Helper class to parse CLI arguments
*
* CLI arguments can be specified with an equals sign between the parameter name and the
* value, so for `-o=foobar.txt` the option would be `-o`.
*/
struct AppOptions {
int verbosity;
std::string configstring{};
AppOptions(int argc, char* argv[] /* NOLINT(modernize-avoid-c-arrays,cppcoreguidelines-avoid-c-arrays) */)
: verbosity{0}, applicationName{argv[0]} {
auto cliArgs = std::vector<std::string>{};
for (int i = 1; i < argc; i++) {
cliArgs.emplace_back(argv[i]);
}
while (!cliArgs.empty()) {
auto arg = *cliArgs.begin();
// find flags (but only at the beginning)
if (0 == arg.find("-h")) {
printUsage();
exit(EXIT_SUCCESS);
break;
}
if (0 == arg.find("-v")) {
this->verbosity = static_cast<int>(std::count(arg.begin(), arg.end(), 'v'));
} else if (0 == arg.find("-d=")) {
this->configstring = arg.substr(arg.find('=') + 1);
} else {
printUsageShort();
std::cerr << "error: unrecognized arguments: " << arg << "\n";
exit(EXIT_FAILURE);
break;
}
// remove the parsed argument from the vector
cliArgs.erase(cliArgs.begin());
}
}
void printUsageShort() {
std::cout << "usage: " << this->applicationName << " ";
std::cout << "[-h] ";
std::cout << "[-v] ";
std::cout << "[-d=SOME_CONFIG]\n";
}
void printUsage() {
printUsageShort();
std::cout << "\n";
std::cout << "optional arguments:\n";
std::cout << " -h, show this help message and exit\n";
std::cout << " -v, increase output verbosity (-vv for VERBOSE)\n";
std::cout << " -d=SOME_CONFIG, TODO config parameter\n";
}
private:
std::string applicationName;
};
int main(int argc, char* argv[]) {
printf("Template C++ App: " GIT_VERSION_TAG "\n\n");
// parse CLI options
AppOptions options(argc, argv);
// start logging
loguru::g_preamble_date = false; // The date field
loguru::g_preamble_time = false; // The time of the current day
loguru::g_preamble_uptime = true; // The time since init call
loguru::g_preamble_thread = false; // The logging thread
loguru::g_preamble_file = true; // The file from which the log originates from
loguru::g_preamble_verbose = true; // The verbosity field
loguru::g_preamble_pipe = true; // The pipe symbol right before the message
loguru::add_file(LOGFILE_PATH, loguru::Truncate, loguru::Verbosity_8);
LOG_F(INFO, "Version " GIT_VERSION_TAG "");
// set stdout verbosity level (0 -> WARNING, 1 -> INFO, 2 -> 1, 3 -> 2, 4 -> MAX)
switch (options.verbosity) {
case 0: // NOLINT(bugprone-branch-clone)
loguru::g_stderr_verbosity = loguru::Verbosity_WARNING; // custom default
break;
case 1: // NOLINT(bugprone-branch-clone)
loguru::g_stderr_verbosity = loguru::Verbosity_INFO; // INFO with -v
break;
case 2: // NOLINT(bugprone-branch-clone)
loguru::g_stderr_verbosity = LOG_VERBOSE; // "verbose" with -vv
break;
case 3: // NOLINT(bugprone-branch-clone)
loguru::g_stderr_verbosity = LOG_DEBUG; // "debug" with -vvv
break;
case 4: // NOLINT(bugprone-branch-clone)
loguru::g_stderr_verbosity = loguru::Verbosity_MAX; // everything with -vvvv
break;
default:
LOG_F(WARNING, "Unrecognised verbosity level %i!", options.verbosity);
break;
}
//
// SETUP
//
// TODO: do some setup here
//
// MAIN LOOP
//
while (true) {
// TODO: do some work here
// very simple ratelimit
std::this_thread::sleep_for(std::chrono::milliseconds(MAINLOOP_SLEEP_US));
}
return EXIT_SUCCESS;
}