-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lib: add command line parser, allow setting global timeline buffer size
- Loading branch information
Showing
9 changed files
with
250 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
#include "internal/command_line_parser.h" | ||
#include "internal/global_timeline.h" | ||
|
||
#include <errno.h> | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
typedef HT_ErrorCode(*HT_CommandLineArgumentParser)(int, char**, int); | ||
|
||
typedef struct | ||
{ | ||
const char* argument; | ||
const char* help; | ||
HT_CommandLineArgumentParser parser; | ||
HT_Boolean is_flag; | ||
} HT_CommandLineArgument; | ||
|
||
static HT_ErrorCode print_help(int argc, char** argv, int pos); | ||
static HT_ErrorCode set_global_timeline_buffer_size(int argc, char** argv, int pos); | ||
|
||
HT_CommandLineArgument arguments[] = { | ||
{ | ||
"--ht-global-timeline-buffer-size", | ||
"Set Global Timeline buffer size", | ||
set_global_timeline_buffer_size, | ||
HT_FALSE | ||
}, | ||
{ | ||
"--ht-help", "Print this help and exits the process", | ||
print_help, | ||
HT_TRUE | ||
} | ||
}; | ||
|
||
static HT_ErrorCode | ||
set_global_timeline_buffer_size(int argc, char** argv, int pos) | ||
{ | ||
if (pos + 1 >= argc) | ||
{ | ||
return HT_ERR_MISSING_ARGUMENT; | ||
} | ||
|
||
const char* s = argv[pos + 1]; | ||
char* end; | ||
errno = 0; | ||
unsigned long value = strtoul(s, &end, 10); | ||
|
||
if (errno == ERANGE || | ||
(sizeof(size_t) < sizeof(unsigned long) && (unsigned long)SIZE_MAX < value)) | ||
{ | ||
return HT_ERR_OUT_OF_RANGE; | ||
} | ||
|
||
if (end - s == 0) | ||
{ | ||
return HT_ERR_INVALID_FORMAT; | ||
} | ||
|
||
ht_global_timeline_set_buffer_size((size_t) value); | ||
return HT_ERR_OK; | ||
} | ||
|
||
static HT_ErrorCode | ||
print_help(int argc, char** argv, int pos) | ||
{ | ||
HT_UNUSED(argc); | ||
HT_UNUSED(argv); | ||
HT_UNUSED(pos); | ||
|
||
printf("HawkTracer options:\n"); | ||
|
||
for (unsigned int x = 0; x < sizeof(arguments) / sizeof(arguments[0]); x++) | ||
{ | ||
printf(" %s %s %s\n", | ||
arguments[x].argument, | ||
arguments[x].is_flag ? " " : "VALUE", | ||
arguments[x].help); | ||
} | ||
|
||
exit(0); | ||
|
||
return HT_ERR_OK; | ||
} | ||
|
||
void | ||
ht_command_line_parse_args(int argc, char** argv) | ||
{ | ||
for (int i = 1; i < argc; i++) | ||
{ | ||
for (unsigned int x = 0; x < sizeof(arguments) / sizeof(arguments[0]); x++) | ||
{ | ||
const char* argname = arguments[x].argument; | ||
if (strcmp(argname, argv[i]) != 0) | ||
{ | ||
continue; | ||
} | ||
|
||
HT_ErrorCode error = arguments[x].parser(argc, argv, i); | ||
|
||
if (error != HT_ERR_OK) | ||
{ | ||
printf("Failed to process argument %s. Error code: %d\n", | ||
argname, error); | ||
} | ||
|
||
if (arguments[x].is_flag == HT_FALSE) | ||
{ | ||
i++; | ||
} | ||
break; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#ifndef HAWKTRACER_INTERNAL_COMMAND_LINE_PARSER_H | ||
#define HAWKTRACER_INTERNAL_COMMAND_LINE_PARSER_H | ||
|
||
#include <hawktracer/macros.h> | ||
#include <hawktracer/base_types.h> | ||
|
||
HT_DECLS_BEGIN | ||
|
||
void ht_command_line_parse_args(int argc, char** argv); | ||
|
||
HT_DECLS_END | ||
|
||
#endif /* HAWKTRACER_INTERNAL_COMMAND_LINE_PARSER_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#ifndef HAWKTRACER_INTERNAL_GLOBAL_TIMELINE_H | ||
#define HAWKTRACER_INTERNAL_GLOBAL_TIMELINE_H | ||
|
||
#include <hawktracer/macros.h> | ||
|
||
#include <stddef.h> | ||
|
||
HT_DECLS_BEGIN | ||
|
||
void ht_global_timeline_set_buffer_size(size_t buffer_size); | ||
|
||
size_t ht_global_timeline_get_buffer_size(void); | ||
|
||
HT_DECLS_END | ||
|
||
#endif /* HAWKTRACER_INTERNAL_GLOBAL_TIMELINE_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#include <internal/command_line_parser.h> | ||
#include <internal/global_timeline.h> | ||
|
||
#include "test_common.h" | ||
|
||
class TestCommandLineParserLib : public ::testing::Test | ||
{ | ||
protected: | ||
void SetUp() override | ||
{ | ||
_buff_size = ht_global_timeline_get_buffer_size(); | ||
} | ||
|
||
void TearDown() override | ||
{ | ||
ht_global_timeline_set_buffer_size(_buff_size); | ||
} | ||
|
||
size_t _buff_size; | ||
}; | ||
|
||
TEST_F(TestCommandLineParserLib, SettingBufferSizeShouldPassForValidInput) | ||
{ | ||
// Arrange | ||
const char* args[] = {"app", "--ht-global-timeline-buffer-size", "76"}; | ||
|
||
// Act | ||
ht_command_line_parse_args(3, (char**)args); | ||
|
||
// Assert | ||
ASSERT_EQ(76u, ht_global_timeline_get_buffer_size()); | ||
|
||
// Cleanup | ||
ht_global_timeline_set_buffer_size(_buff_size); | ||
} | ||
|
||
TEST_F(TestCommandLineParserLib, SettingBufferSizeShouldFailForOutOfRangeValue) | ||
{ | ||
// Arrange | ||
const char* args[] = {"app", "--ht-global-timeline-buffer-size", "9999999999999999999999999999"}; | ||
|
||
// Act | ||
ht_command_line_parse_args(3, (char**)args); | ||
|
||
// Assert | ||
ASSERT_EQ(_buff_size, ht_global_timeline_get_buffer_size()); | ||
} | ||
|
||
TEST_F(TestCommandLineParserLib, SettingBufferSizeShouldFailForInvalidString) | ||
{ | ||
// Arrange | ||
const char* args[] = {"app", "--ht-global-timeline-buffer-size", "test"}; | ||
|
||
// Act | ||
ht_command_line_parse_args(3, (char**)args); | ||
|
||
// Assert | ||
ASSERT_EQ(_buff_size, ht_global_timeline_get_buffer_size()); | ||
} | ||
|
||
TEST_F(TestCommandLineParserLib, SettingBufferSizeShouldFailIfValueIsMissing) | ||
{ | ||
// Arrange | ||
const char* args[] = {"app", "--ht-global-timeline-buffer-size"}; | ||
|
||
// Act | ||
ht_command_line_parse_args(2, (char**)args); | ||
|
||
// Assert | ||
ASSERT_EQ(_buff_size, ht_global_timeline_get_buffer_size()); | ||
} | ||
|
||
TEST_F(TestCommandLineParserLib, PassingInvalidArgumentShouldSkipTheArgument) | ||
{ | ||
// Arrange | ||
const char* args[] = {"app", "--ht-non-existing-parameter"}; | ||
|
||
// Act | ||
ht_command_line_parse_args(2, (char**)args); | ||
} | ||
|