Skip to content

Commit c8af3ac

Browse files
authored
Merge pull request #6 from vthiery/feature/improve-organization
Feature/improve organization
2 parents 43dfa9c + fe5567e commit c8af3ac

File tree

5 files changed

+198
-173
lines changed

5 files changed

+198
-173
lines changed

.clang-format

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
AccessModifierOffset: -4
2+
AllowAllParametersOfDeclarationOnNextLine: false
3+
AllowShortFunctionsOnASingleLine: Empty
4+
BinPackArguments: false
5+
BinPackParameters: false
6+
ColumnLimit: 120
7+
IndentCaseLabels: false
8+
IndentWidth: 4
9+
10+
---
11+
Language: Cpp
12+
# BasedOnStyle: Google
13+
#AccessModifierOffset: -1
14+
AlignAfterOpenBracket: Align
15+
AlignConsecutiveAssignments: false
16+
AlignConsecutiveDeclarations: false
17+
AlignEscapedNewlinesLeft: true
18+
AlignOperands: true
19+
AlignTrailingComments: true
20+
#AllowAllParametersOfDeclarationOnNextLine: true
21+
AllowShortBlocksOnASingleLine: false
22+
AllowShortCaseLabelsOnASingleLine: false
23+
#AllowShortFunctionsOnASingleLine: All
24+
AllowShortIfStatementsOnASingleLine: true
25+
AllowShortLoopsOnASingleLine: true
26+
AlwaysBreakAfterDefinitionReturnType: None
27+
AlwaysBreakAfterReturnType: None
28+
AlwaysBreakBeforeMultilineStrings: true
29+
AlwaysBreakTemplateDeclarations: true
30+
#BinPackArguments: true
31+
#BinPackParameters: true
32+
BraceWrapping:
33+
AfterClass: false
34+
AfterControlStatement: false
35+
AfterEnum: false
36+
AfterFunction: false
37+
AfterNamespace: false
38+
AfterObjCDeclaration: false
39+
AfterStruct: false
40+
AfterUnion: false
41+
BeforeCatch: false
42+
BeforeElse: false
43+
IndentBraces: false
44+
BreakBeforeBinaryOperators: None
45+
BreakBeforeBraces: Attach
46+
BreakBeforeTernaryOperators: true
47+
BreakConstructorInitializersBeforeComma: false
48+
BreakAfterJavaFieldAnnotations: false
49+
BreakStringLiterals: true
50+
#ColumnLimit: 80
51+
CommentPragmas: '^ IWYU pragma:'
52+
ConstructorInitializerAllOnOneLineOrOnePerLine: true
53+
ConstructorInitializerIndentWidth: 4
54+
ContinuationIndentWidth: 4
55+
Cpp11BracedListStyle: true
56+
DerivePointerAlignment: true
57+
DisableFormat: false
58+
ExperimentalAutoDetectBinPacking: false
59+
ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ]
60+
IncludeCategories:
61+
- Regex: '^<.*\.h>'
62+
Priority: 1
63+
- Regex: '^<.*'
64+
Priority: 2
65+
- Regex: '.*'
66+
Priority: 3
67+
IncludeIsMainRegex: '([-_](test|unittest))?$'
68+
#IndentCaseLabels: true
69+
#IndentWidth: 2
70+
IndentWrappedFunctionNames: false
71+
JavaScriptQuotes: Leave
72+
JavaScriptWrapImports: true
73+
KeepEmptyLinesAtTheStartOfBlocks: false
74+
MacroBlockBegin: ''
75+
MacroBlockEnd: ''
76+
MaxEmptyLinesToKeep: 1
77+
NamespaceIndentation: None
78+
ObjCBlockIndentWidth: 2
79+
ObjCSpaceAfterProperty: false
80+
ObjCSpaceBeforeProtocolList: false
81+
PenaltyBreakBeforeFirstCallParameter: 1
82+
PenaltyBreakComment: 300
83+
PenaltyBreakFirstLessLess: 120
84+
PenaltyBreakString: 1000
85+
PenaltyExcessCharacter: 1000000
86+
PenaltyReturnTypeOnItsOwnLine: 200
87+
PointerAlignment: Left
88+
ReflowComments: true
89+
SortIncludes: true
90+
SpaceAfterCStyleCast: false
91+
SpaceAfterTemplateKeyword: true
92+
SpaceBeforeAssignmentOperators: true
93+
SpaceBeforeParens: ControlStatements
94+
SpaceInEmptyParentheses: false
95+
SpacesBeforeTrailingComments: 2
96+
SpacesInAngles: false
97+
SpacesInContainerLiterals: true
98+
SpacesInCStyleCastParentheses: false
99+
SpacesInParentheses: false
100+
SpacesInSquareBrackets: false
101+
Standard: Auto
102+
TabWidth: 8
103+
UseTab: Never
104+
...
105+

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ cmake_minimum_required(VERSION 3.2)
22

33
project(StatsdClient)
44

5-
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src)
5+
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include/cpp-statsd-client)
66

7-
install(FILES src/UDPSender.hpp src/StatsdClient.hpp DESTINATION include)
7+
install(FILES UDPSender.hpp StatsdClient.hpp DESTINATION include)
88

99
find_package(Threads)
1010
add_executable(testStatsdClient ${CMAKE_CURRENT_SOURCE_DIR}/tests/testStatsdClient.cpp)

src/StatsdClient.hpp renamed to include/cpp-statsd-client/StatsdClient.hpp

Lines changed: 37 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
#include <string>
77
#include "UDPSender.hpp"
88

9-
namespace Statsd
10-
{
9+
namespace Statsd {
1110
/*!
1211
*
1312
* Statsd client
@@ -20,159 +19,124 @@ namespace Statsd
2019
* set if batching is not desired.
2120
*
2221
*/
23-
class StatsdClient
24-
{
22+
class StatsdClient {
2523
public:
26-
2724
//!@name Constructor and destructor
2825
//!@{
2926

3027
//! Constructor
31-
StatsdClient(
32-
const std::string& host,
33-
const uint16_t port,
34-
const std::string& prefix,
35-
const std::experimental::optional<uint64_t> batchsize = std::experimental::nullopt) noexcept;
28+
StatsdClient(const std::string &host,
29+
const uint16_t port,
30+
const std::string &prefix,
31+
const std::experimental::optional<uint64_t> batchsize = std::experimental::nullopt) noexcept;
3632

3733
//!@}
3834

3935
//!@name Methods
4036
//!@{
4137

4238
//! Sets a configuration { host, port, prefix }
43-
inline void setConfig(const std::string& host, const uint16_t port, const std::string& prefix) noexcept;
39+
inline void setConfig(const std::string &host, const uint16_t port, const std::string &prefix) noexcept;
4440

4541
//! Returns the error message as an optional std::string
4642
inline std::experimental::optional<std::string> errorMessage() const noexcept;
4743

4844
//! Increments the key, at a given frequency rate
49-
inline void increment(const std::string& key, const float frequency = 1.0f) const noexcept;
45+
inline void increment(const std::string &key, const float frequency = 1.0f) const noexcept;
5046

5147
//! Increments the key, at a given frequency rate
52-
inline void decrement(const std::string& key, const float frequency = 1.0f) const noexcept;
48+
inline void decrement(const std::string &key, const float frequency = 1.0f) const noexcept;
5349

5450
//! Adjusts the specified key by a given delta, at a given frequency rate
55-
inline void count(const std::string& key, const int delta, const float frequency = 1.0f) const noexcept;
51+
inline void count(const std::string &key, const int delta, const float frequency = 1.0f) const noexcept;
5652

5753
//! Records a gauge for the key, with a given value, at a given frequency rate
58-
inline void gauge(const std::string& key, const unsigned int value, const float frequency = 1.0f) const noexcept;
54+
inline void gauge(const std::string &key, const unsigned int value, const float frequency = 1.0f) const noexcept;
5955

6056
//! Records a timing for a key, at a given frequency
61-
inline void timing(const std::string& key, const unsigned int ms, const float frequency = 1.0f) const noexcept;
57+
inline void timing(const std::string &key, const unsigned int ms, const float frequency = 1.0f) const noexcept;
6258

6359
//! Send a value for a key, according to its type, at a given frequency
64-
void send(const std::string& key, const int value, const std::string& type, const float frequency = 1.0f) const noexcept;
60+
void send(const std::string &key, const int value, const std::string &type, const float frequency = 1.0f) const
61+
noexcept;
6562

6663
//!@}
6764

6865
private:
69-
7066
//! The prefix to be used for metrics
7167
std::string m_prefix;
7268

7369
//! The UDP sender to be used for actual sending
7470
mutable UDPSender m_sender;
7571
};
7672

77-
StatsdClient::
78-
StatsdClient(
79-
const std::string& host,
80-
const uint16_t port,
81-
const std::string& prefix,
82-
const std::experimental::optional<uint64_t> batchsize) noexcept
83-
: m_prefix(prefix)
84-
, m_sender(host, port, batchsize)
85-
{
73+
StatsdClient::StatsdClient(const std::string &host,
74+
const uint16_t port,
75+
const std::string &prefix,
76+
const std::experimental::optional<uint64_t> batchsize) noexcept
77+
: m_prefix(prefix), m_sender(host, port, batchsize) {
8678
// Initialize the randorm generator to be used for sampling
8779
std::srand(time(nullptr));
8880
}
8981

90-
void
91-
StatsdClient::
92-
setConfig(const std::string& host, const uint16_t port, const std::string& prefix) noexcept
93-
{
82+
void StatsdClient::setConfig(const std::string &host, const uint16_t port, const std::string &prefix) noexcept {
9483
m_prefix = prefix;
9584
m_sender.setConfig(host, port);
9685
}
9786

98-
std::experimental::optional<std::string>
99-
StatsdClient::
100-
errorMessage() const noexcept
101-
{
87+
std::experimental::optional<std::string> StatsdClient::errorMessage() const noexcept {
10288
return m_sender.errorMessage();
10389
}
10490

105-
void
106-
StatsdClient::
107-
decrement(const std::string& key, const float frequency) const noexcept
108-
{
91+
void StatsdClient::decrement(const std::string &key, const float frequency) const noexcept {
10992
return count(key, -1, frequency);
11093
}
11194

112-
void
113-
StatsdClient::
114-
increment(const std::string& key, const float frequency) const noexcept
115-
{
95+
void StatsdClient::increment(const std::string &key, const float frequency) const noexcept {
11696
return count(key, 1, frequency);
11797
}
11898

119-
void
120-
StatsdClient::
121-
count(const std::string& key, const int delta, const float frequency) const noexcept
122-
{
99+
void StatsdClient::count(const std::string &key, const int delta, const float frequency) const noexcept {
123100
return send(key, delta, "c", frequency);
124101
}
125102

126-
void
127-
StatsdClient::
128-
gauge(const std::string& key, const unsigned int value, const float frequency) const noexcept
129-
{
103+
void StatsdClient::gauge(const std::string &key, const unsigned int value, const float frequency) const noexcept {
130104
return send(key, value, "g", frequency);
131105
}
132106

133-
void
134-
StatsdClient::
135-
timing(const std::string& key, const unsigned int ms, const float frequency) const noexcept
136-
{
107+
void StatsdClient::timing(const std::string &key, const unsigned int ms, const float frequency) const noexcept {
137108
return send(key, ms, "ms", frequency);
138109
}
139110

140-
void
141-
StatsdClient::
142-
send(const std::string& key, const int value, const std::string& type, const float frequency) const noexcept
143-
{
144-
const auto isFrequencyOne = [](const float frequency) noexcept
145-
{
146-
constexpr float epsilon{ 0.0001f };
111+
void StatsdClient::send(const std::string &key, const int value, const std::string &type, const float frequency) const
112+
noexcept {
113+
const auto isFrequencyOne = [](const float frequency) noexcept {
114+
constexpr float epsilon{0.0001f};
147115
return std::fabs(frequency - 1.0f) < epsilon;
148116
};
149117

150118
// Test if one should send or not, according to the frequency rate
151-
if (!isFrequencyOne(frequency))
152-
{
153-
if (frequency < static_cast<float>(std::rand()) / RAND_MAX)
154-
{
119+
if (!isFrequencyOne(frequency)) {
120+
if (frequency < static_cast<float>(std::rand()) / RAND_MAX) {
155121
return;
156122
}
157123
}
158124

159125
// Prepare the buffer, with a sampling rate if specified different from 1.0f
160126
char buffer[256];
161-
if (isFrequencyOne(frequency))
162-
{
127+
if (isFrequencyOne(frequency)) {
163128
// Sampling rate is 1.0f, no need to specify it
164129
std::snprintf(buffer, sizeof(buffer), "%s%s:%d|%s", m_prefix.c_str(), key.c_str(), value, type.c_str());
165-
}
166-
else
167-
{
130+
} else {
168131
// Sampling rate is different from 1.0f, hence specify it
169-
std::snprintf(buffer, sizeof(buffer), "%s%s:%d|%s|@%.2f", m_prefix.c_str(), key.c_str(), value, type.c_str(), frequency);
132+
std::snprintf(
133+
buffer, sizeof(buffer), "%s%s:%d|%s|@%.2f", m_prefix.c_str(), key.c_str(), value, type.c_str(), frequency);
170134
}
171135

172136
// Send the message via the UDP sender
173137
m_sender.send(buffer);
174138
}
175139

176-
}
140+
} // namespace Statsd
177141

178142
#endif

0 commit comments

Comments
 (0)