Skip to content

Commit 75bf8dd

Browse files
authored
Merge pull request #57 from urbytes21/dev_branch_3
id 1776177215
2 parents a43595a + 127beb3 commit 75bf8dd

6 files changed

Lines changed: 202 additions & 74 deletions

File tree

.clang-tidy

Lines changed: 138 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,148 @@
1-
# Apply to ALL headers (empty = no restriction)
2-
HeaderFilterRegex: '.*'
3-
4-
# Treat warnings as errors (optional but recommended for CI)
5-
# WarningsAsErrors: '*'
6-
WarningsAsErrors: ''
1+
---
2+
# Configure clang-tidy for this project.
73

4+
# Here is an explanation for why some of the checks are disabled:
5+
#
6+
# -google-readability-namespace-comments: the *_CLIENT_NS is a macro, and
7+
# clang-tidy fails to match it against the initial value.
8+
#
9+
# -modernize-use-trailing-return-type: clang-tidy recommends using
10+
# `auto Foo() -> std::string { return ...; }`, we think the code is less
11+
# readable in this form.
12+
#
13+
# --modernize-concat-nested-namespaces: clang-tidy recommends
14+
# `namespace google::cloud {}` over `namespace google { namespace cloud { } }`
15+
# We need to support C++14, which does not supported nested namespaces.
16+
#
17+
# --modernize-use-nodiscard: clang-tidy recommends adding a nodiscard annotation
18+
# to functions where the return value should not be ignored.
19+
# We need to support C++14, which does not supported the annotation.
20+
#
21+
# -modernize-return-braced-init-list: We think removing typenames and using
22+
# only braced-init can hurt readability.
23+
#
24+
# -modernize-avoid-c-arrays: We only use C arrays when they seem to be the
25+
# right tool for the job, such as `char foo[] = "hello"`. In these cases,
26+
# avoiding C arrays often makes the code less readable, and std::array is
27+
# not a drop-in replacement because it doesn't deduce the size.
28+
#
29+
# -modernize-type-traits: clang-tidy recommands using c++17 style variable
30+
# templates. We will enable this check after we moved to c++17.
31+
#
32+
# -modernize-unary-static-assert: clang-tidy asks removing empty string in
33+
# static_assert(), the check is only applicable for c++17 and later code.
34+
# We will enable this check after we moved to c++17.
35+
#
36+
# -performance-move-const-arg: This warning requires the developer to
37+
# know/care more about the implementation details of types/functions than
38+
# should be necessary. For example, `A a; F(std::move(a));` will trigger a
39+
# warning IFF `A` is a trivial type (and therefore the move is
40+
# meaningless). It would also warn if `F` accepts by `const&`, which is
41+
# another detail that the caller need not care about.
42+
#
43+
# -performance-avoid-endl: we would like to turn this on, but there are too
44+
# many legitimate uses in our samples.
45+
#
46+
# -performance-enum-size: Smaller enums may or not may be faster, it depends on
47+
# the architechture. If data size was a consideration, we might decide to
48+
# enable the warnings.
49+
#
50+
# -readability-redundant-declaration: A friend declaration inside a class
51+
# counts as a declaration, so if we also declare that friend outside the
52+
# class in order to document it as part of the public API, that will
53+
# trigger a redundant declaration warning from this check.
54+
#
55+
# -readability-avoid-return-with-void-value: We believe this is idiomatic
56+
# and saves typing, and the intent is obvious.
57+
#
58+
# -readability-function-cognitive-complexity: too many false positives with
59+
# clang-tidy-12. We need to disable this check in macros, and that setting
60+
# only appears in clang-tidy-13.
61+
#
62+
# -bugprone-narrowing-conversions: too many false positives around
63+
# `std::size_t` vs. `*::difference_type`.
64+
#
65+
# -bugprone-easily-swappable-parameters: too many false positives.
66+
#
67+
# -bugprone-implicit-widening-of-multiplication-result: too many false positives.
68+
# Almost any expression of the form `2 * variable` or `long x = a_int * b_int;`
69+
# generates an error.
70+
#
71+
# -bugprone-unchecked-optional-access: too many false positives in tests.
72+
# Despite what the documentation says, this warning appears after
73+
# `ASSERT_TRUE(variable)` or `ASSERT_TRUE(variable.has_value())`.
74+
#
75+
# TODO(#14162): Enable clang-tidy checks. We initially omitted these checks
76+
# because they require large cleanup efforts or were blocking the clang-tidy
77+
# X update.
878
Checks: >
979
-*,
10-
clang-diagnostic-*,
11-
modernize-*,
12-
-modernize-use-trailing-return-type,
13-
-modernize-use-auto,
14-
cppcoreguidelines-*,
15-
-cppcoreguidelines-owning-memory,
16-
-cppcoreguidelines-pro-type-vararg,
17-
-cppcoreguidelines-avoid-magic-numbers,
80+
abseil-*,
1881
bugprone-*,
82+
google-*,
83+
misc-*,
84+
modernize-*,
1985
performance-*,
86+
portability-*,
2087
readability-*,
21-
-readability-magic-numbers,
88+
-bugprone-exception-escape,
89+
-google-readability-braces-around-statements,
90+
-google-readability-namespace-comments,
91+
-google-runtime-references,
92+
-misc-non-private-member-variables-in-classes,
93+
-misc-const-correctness,
94+
-misc-include-cleaner,
95+
-modernize-return-braced-init-list,
96+
-modernize-use-trailing-return-type,
97+
-modernize-concat-nested-namespaces,
98+
-modernize-use-nodiscard,
99+
-modernize-avoid-c-arrays,
100+
-modernize-type-traits,
101+
-modernize-unary-static-assert,
102+
-performance-move-const-arg,
103+
-performance-avoid-endl,
104+
-performance-enum-size,
105+
-readability-braces-around-statements,
22106
-readability-identifier-length,
23-
misc-*,
24-
-misc-unused-parameters
25-
26-
CheckOptions:
27-
- key: readability-identifier-naming.NamespaceCase
28-
value: lower_case
29-
30-
- key: readability-identifier-naming.ClassCase
31-
value: CamelCase
32-
33-
- key: readability-identifier-naming.StructCase
34-
value: CamelCase
35-
36-
- key: readability-identifier-naming.FunctionCase
37-
value: lower_case
107+
-readability-magic-numbers,
108+
-readability-named-parameter,
109+
-readability-redundant-declaration,
110+
-readability-avoid-return-with-void-value,
111+
-readability-function-cognitive-complexity,
112+
-bugprone-narrowing-conversions,
113+
-bugprone-easily-swappable-parameters,
114+
-bugprone-inc-dec-in-conditions,
115+
-bugprone-implicit-widening-of-multiplication-result,
116+
-bugprone-unchecked-optional-access,
117+
-bugprone-unused-local-non-trivial-variable,
118+
-bugprone-unused-return-value
38119
39-
- key: readability-identifier-naming.VariableCase
40-
value: lower_case
120+
# Turn all the warnings from the checks above into errors.
121+
WarningsAsErrors: "*"
41122

42-
- key: readability-identifier-naming.MemberCase
43-
value: lower_case
123+
HeaderFilterRegex: "(google/cloud/|generator/).*\\.h$"
44124

45-
- key: modernize-use-nullptr.NullMacros
46-
value: 'NULL'
125+
CheckOptions:
126+
- { key: readability-identifier-naming.NamespaceCase, value: lower_case }
127+
- { key: readability-identifier-naming.ClassCase, value: CamelCase }
128+
- { key: readability-identifier-naming.StructCase, value: CamelCase }
129+
- { key: readability-identifier-naming.TemplateParameterCase, value: CamelCase }
130+
- { key: readability-identifier-naming.FunctionCase, value: aNy_CasE }
131+
- { key: readability-identifier-naming.VariableCase, value: lower_case }
132+
- { key: readability-identifier-naming.ClassMemberCase, value: lower_case }
133+
- { key: readability-identifier-naming.ClassMemberSuffix, value: _ }
134+
- { key: readability-identifier-naming.PrivateMemberSuffix, value: _ }
135+
- { key: readability-identifier-naming.ProtectedMemberSuffix, value: _ }
136+
- { key: readability-identifier-naming.EnumConstantCase, value: CamelCase }
137+
- { key: readability-identifier-naming.EnumConstantPrefix, value: k }
138+
- { key: readability-identifier-naming.ConstexprVariableCase, value: CamelCase }
139+
- { key: readability-identifier-naming.ConstexprVariablePrefix, value: k }
140+
- { key: readability-identifier-naming.GlobalConstantCase, value: CamelCase }
141+
- { key: readability-identifier-naming.GlobalConstantPrefix, value: k }
142+
- { key: readability-identifier-naming.MemberConstantCase, value: CamelCase }
143+
- { key: readability-identifier-naming.MemberConstantPrefix, value: k }
144+
- { key: readability-identifier-naming.StaticConstantCase, value: CamelCase }
145+
- { key: readability-identifier-naming.StaticConstantPrefix, value: k }
146+
- { key: readability-implicit-bool-conversion.AllowIntegerConditions, value: 1 }
147+
- { key: readability-implicit-bool-conversion.AllowPointerConditions, value: 1 }
148+
- { key: readability-function-cognitive-complexity.IgnoreMacros, value: 1 }

CMakeLists.txt

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,29 @@
1-
# ROOT CMAKE
2-
#
3-
# ┌─────────────────┴─────────────────┐
4-
# │ │
5-
# src module tests module
6-
# │ │
7-
# ┌────┴────┐ │
8-
# │ │ │
9-
# core socket │
10-
# │ │ │
11-
# └──────┬──┘ │
12-
# │ │
13-
# ▼ ▼
14-
# cpp_lab_project cpp_lab_project_unit_test
15-
# (main executable) (GoogleTest executable)
16-
171
cmake_minimum_required(VERSION 3.14)
182

19-
# ----------------------------------------------------------------------------------------
203
# Project metadata
21-
# ----------------------------------------------------------------------------------------
224
project(cpp_lab_project # ${PROJECT_NAME}
235
VERSION 1.0.0
246
DESCRIPTION "A C/C++ project uses CMake, GoogleTest, gcc, g++, cppcheck, and lcov, integrated with Docker and GitHub Actions for CI/CD."
257
LANGUAGES CXX
268
)
279

28-
# ----------------------------------------------------------------------------------------
2910
# Output directories to build/bin
30-
# ----------------------------------------------------------------------------------------
3111
# Executables
3212
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
3313
# Shared libraries
3414
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
3515
# Static libraries
3616
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
3717

38-
# ----------------------------------------------------------------------------------------
3918
# Compiler and language configuration
40-
# ----------------------------------------------------------------------------------------
4119
# Require at least C++17 for GoogleTest and modern C++ features
4220
set(CMAKE_CXX_STANDARD 20)
4321
set(CMAKE_CXX_STANDARD_REQUIRED ON)
4422

4523
# Export compile_commands.json (useful for clang-tidy, clangd, IDEs)
4624
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
4725

48-
# ----------------------------------------------------------------------------------------
4926
# Build metadata
50-
# ----------------------------------------------------------------------------------------
5127
# Ensure directory exists for generated headers
5228
file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/generated)
5329

@@ -60,24 +36,18 @@ configure_file(
6036
${CMAKE_BINARY_DIR}/generated/version.h
6137
)
6238

63-
# ----------------------------------------------------------------------------------------
6439
# External dependencies (GoogleTest,...)
65-
# ----------------------------------------------------------------------------------------
6640
include(cmake/Dependencies.cmake)
6741

68-
# ----------------------------------------------------------------------------------------
6942
# Compiler warnings (useful for learning/debugging)
70-
# ----------------------------------------------------------------------------------------
7143
add_compile_options(-Wall -Wextra -Wpedantic)
7244

7345
message(STATUS "C Compiler: ${CMAKE_C_COMPILER}")
7446
message(STATUS "C++ Compiler: ${CMAKE_CXX_COMPILER}")
7547
message(STATUS "C++ Compiler ID: ${CMAKE_CXX_COMPILER_ID}")
7648
message(STATUS "C++ Compiler Version: ${CMAKE_CXX_COMPILER_VERSION}")
7749

78-
# ----------------------------------------------------------------------------------------
7950
# Code coverage configuration
80-
# ----------------------------------------------------------------------------------------
8151
option(ENABLE_COVERAGE "Enable coverage reporting" OFF)
8252

8353
if(ENABLE_COVERAGE AND CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
@@ -86,13 +56,9 @@ if(ENABLE_COVERAGE AND CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
8656
add_link_options(--coverage)
8757
endif()
8858

89-
# ----------------------------------------------------------------------------------------
9059
# Enable CTest framework (used by GoogleTest)
91-
# ----------------------------------------------------------------------------------------
9260
enable_testing()
9361

94-
# ----------------------------------------------------------------------------------------
9562
# Add project modules
96-
# ----------------------------------------------------------------------------------------
9763
add_subdirectory(src)
9864
add_subdirectory(tests)

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ Get-ChildItem -Recurse -Include *.cpp, *.h, *.hpp | ForEach-Object { clang-forma
4747
$ sudo apt-get install cppcheck
4848
$ sudo apt-get install -y clang-tidy
4949
$ sudo apt install python3-gcovr
50+
$ sudo apt-get install -y libgtkmm-4.0-dev
5051
```
5152
* Build the application and the tests
5253
```bash

include/Logger.h

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#ifndef LOGGER_H_
2+
#define LOGGER_H_
3+
4+
#include <iostream>
5+
6+
#ifndef NDEBUG
7+
#include <mutex>
8+
#include <source_location>
9+
#include <string_view>
10+
11+
class Logger {
12+
public:
13+
static Logger& instance() {
14+
static Logger logger; // guaranteed single instance, thread-safe in C++11+
15+
return logger;
16+
}
17+
18+
void log(std::string_view msg, std::string_view level = "INFO",
19+
std::source_location loc = std::source_location::current()) {
20+
std::lock_guard<std::mutex> lock(mutex_);
21+
22+
// Extract filename only (no full path)
23+
std::string_view file = loc.file_name();
24+
auto pos = file.find_last_of("/\\");
25+
if (pos != std::string_view::npos) {
26+
file = file.substr(pos + 1);
27+
}
28+
29+
// Get current time
30+
auto now = std::chrono::system_clock::now();
31+
auto t = std::chrono::system_clock::to_time_t(now);
32+
std::tm tm{};
33+
localtime_r(&t, &tm);
34+
35+
char time_buf[9];
36+
std::strftime(time_buf, sizeof(time_buf), "%H:%M:%S", &tm);
37+
38+
std::cout << "[" << time_buf << "]" << "[" << level << "]" << "[" << file
39+
<< ":" << loc.line() << "]" << "[" << loc.function_name() << "] "
40+
<< msg << '\n';
41+
}
42+
43+
// Prevent copies
44+
Logger(const Logger&) = delete;
45+
Logger& operator=(const Logger&) = delete;
46+
47+
private:
48+
Logger() = default;
49+
std::mutex mutex_;
50+
};
51+
52+
#define LOG(msg) Logger::instance().log(msg)
53+
54+
#else
55+
56+
inline void LOG(const std::string& msg) {
57+
std::cout << msg << '\n';
58+
}
59+
60+
#endif
61+
#endif

scripts/run.sh

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
## NOTE:
2-
## This file was initially generated with the assistance of AI.
3-
## The code has been reviewed and may have been modified by the developer
4-
## to ensure correctness, readability, and compliance with project requirements.
51
#!/usr/bin/env bash
62

73
set -e # Exit immediately if a command fails

src/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <vector>
55

66
#include "ExampleRegistry.h"
7+
#include "Logger.h"
78
#include "version.h"
89

910
int readChoice() {
@@ -103,6 +104,7 @@ void runMenu() {
103104
}
104105

105106
int main(int argc, char* argv[]) {
107+
LOG("Logger has been integrated");
106108
std::cout << std::endl;
107109
if (__cplusplus == 202302L)
108110
std::cout << "C++23";

0 commit comments

Comments
 (0)