Skip to content

Commit 323d82e

Browse files
committed
cleanup flags.h, write options to some channel, stdout in cmdline
Summary: fixes #61 fixes #62 in fb services will call printOptions(LOG(INFO)); {need fb side work/will break there/this is a start/proposal} Closes #63 Reviewed By: @uddipta Differential Revision: D2397161 Pulled By: @ldemailly
1 parent ba71c65 commit 323d82e

8 files changed

+115
-90
lines changed

CMakeLists.txt

+22-5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ cmake_minimum_required(VERSION 3.2)
2222
# There is no C per se in WDT but if you use CXX only here many checks fail
2323
# Version is Major.Minor.YYMMDDX for up to 10 releases per day
2424
# Minor currently is also the protocol version - has to match with Protocol.cpp
25-
project("WDT" LANGUAGES C CXX VERSION 1.16.1508210)
25+
project("WDT" LANGUAGES C CXX VERSION 1.16.1508310)
2626

2727
# On MacOS this requires the latest (master) CMake (and/or CMake 3.1.1/3.2)
2828
set(CMAKE_CXX_STANDARD 11)
@@ -206,7 +206,7 @@ if (BUILD_TESTING)
206206
enable_testing()
207207

208208
# Extra code that we use in tests
209-
add_library(wdt4tests
209+
add_library(wdt4tests_min
210210
"${FOLLY_SOURCE_DIR}/folly/FileUtil.cpp" # used by Random used by tests
211211
"${FOLLY_SOURCE_DIR}/folly/Random.cpp" # used indirectly by tests
212212
)
@@ -238,14 +238,20 @@ if (BUILD_TESTING)
238238
# ${GMOCK_PREFIX}/src/gmock/gmock-all.cc
239239
# ${GMOCK_PREFIX}/src/gmock/gmock_main.cc)
240240

241-
add_dependencies(wdt4tests gmock)
241+
add_dependencies(wdt4tests_min gmock)
242242

243243
# ${BINARY_DIR}/libgmock.a works everywhere except xcode...
244244
# so ugly weird hack generating warnings about unknown dir for now:
245-
target_link_libraries(wdt4tests
245+
target_link_libraries(wdt4tests_min
246246
"-L ${BINARY_DIR} -L ${BINARY_DIR}/Debug -lgmock"
247-
wdtlib
247+
wdtlib_min
248+
)
249+
250+
add_library(wdt4tests
251+
WdtFlags.cpp
248252
)
253+
target_link_libraries(wdt4tests wdt4tests_min)
254+
249255

250256
add_executable(protocol_test ProtocolTest.cpp)
251257
target_link_libraries(protocol_test wdt4tests)
@@ -259,6 +265,15 @@ if (BUILD_TESTING)
259265
target_link_libraries(file_reader_test wdt4tests)
260266
add_test(NAME FileReaderTests COMMAND file_reader_test)
261267

268+
add_executable(option_type_test_long_flags OptionTypeTest.cpp)
269+
target_link_libraries(option_type_test_long_flags wdt4tests)
270+
271+
add_executable(option_type_test_short_flags OptionTypeTest.cpp WdtFlags.cpp)
272+
set_target_properties(option_type_test_short_flags PROPERTIES
273+
COMPILE_DEFINITIONS "STANDALONE_APP")
274+
target_link_libraries(option_type_test_short_flags wdt4tests_min)
275+
276+
262277
add_test(NAME WdtRandGenTest COMMAND
263278
"${CMAKE_CURRENT_SOURCE_DIR}/wdt_rand_gen_test.sh")
264279

@@ -268,5 +283,7 @@ if (BUILD_TESTING)
268283
add_test(NAME WdtBasicE2Exfs COMMAND
269284
"${CMAKE_CURRENT_SOURCE_DIR}/wdt_e2e_xfs_test.sh")
270285

286+
add_test(NAME WdtOptionsTypeTests COMMAND
287+
"${CMAKE_CURRENT_SOURCE_DIR}/wdt_option_type_test.sh")
271288

272289
endif(BUILD_TESTING)

DirectorySourceQueue.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ bool DirectorySourceQueue::setRootDir(const string &newRootDir) {
111111
if (dir.back() != '/') {
112112
dir.push_back('/');
113113
}
114-
if ( dir != rootDir_ ) {
114+
if (dir != rootDir_) {
115115
rootDir_.assign(dir);
116116
LOG(INFO) << "Root dir now " << rootDir_;
117117
}
@@ -202,7 +202,7 @@ string DirectorySourceQueue::resolvePath(const string &path) {
202202
char *resolvedPath = realpath(path.c_str(), nullptr);
203203
if (!resolvedPath) {
204204
PLOG(ERROR) << "Couldn't resolve " << path;
205-
return result; // empty string == error
205+
return result; // empty string == error
206206
}
207207
result.assign(resolvedPath);
208208
free(resolvedPath);

OptionTypeTest.cpp

+5-6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include <glog/logging.h>
1212
#include <gtest/gtest.h>
1313

14+
#include "WdtFlagsMacros.h"
15+
1416
/*
1517
* Tests in this file can not be run in the same process. That is because we
1618
* rely on is_default property of gflags to determine whether a flag has been
@@ -24,12 +26,9 @@
2426
namespace facebook {
2527
namespace wdt {
2628

27-
const std::string NUM_PORTS_FLAG =
28-
WdtFlags::getFlagNameFromOptionName("num_ports");
29-
const std::string BLOCK_SIZE_FLAG =
30-
WdtFlags::getFlagNameFromOptionName("block_size_mbytes");
31-
const std::string OPTION_TYPE_FLAG =
32-
WdtFlags::getFlagNameFromOptionName("option_type");
29+
const std::string NUM_PORTS_FLAG = WDT_FLAG_STR(num_ports);
30+
const std::string BLOCK_SIZE_FLAG = WDT_FLAG_STR(block_size_mbytes);
31+
const std::string OPTION_TYPE_FLAG = WDT_FLAG_STR(option_type);
3332

3433
void overrideTest1(const std::string &optionType) {
3534
const auto &options = WdtOptions::get();

WdtConfig.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
#define WDT_VERSION_MAJOR 1
1010
#define WDT_VERSION_MINOR 16
11-
#define WDT_VERSION_BUILD 1508210
11+
#define WDT_VERSION_BUILD 1508310
1212
// Add -fbcode to version str
13-
#define WDT_VERSION_STR "1.16.1508210-fbcode"
13+
#define WDT_VERSION_STR "1.16.1508310-fbcode"
1414
// Tie minor and proto version
1515
#define WDT_PROTOCOL_VERSION WDT_VERSION_MINOR
1616

WdtFlags.cpp

+34-33
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,30 @@
77
* of patent rights can be found in the PATENTS file in the same directory.
88
*/
99
#include "WdtFlags.h"
10-
#include "WdtFlags.cpp.inc"
10+
#include "WdtOptions.h"
11+
12+
#include <gflags/gflags.h>
1113
#include <glog/logging.h>
14+
#include <iostream>
1215
#include "Protocol.h"
13-
#include <folly/Conv.h>
1416

15-
FLAG_DEFINITION(string, PREFIX(option_type),
16-
facebook::wdt::WdtOptions::FLASH_OPTION_TYPE,
17-
"WDT option type. Options are initialized to different values "
18-
"depending on the type. Individual options can still be "
19-
"changed using specific flags.")
17+
#include "WdtFlags.cpp.inc"
18+
19+
WDT_FLAG_DEFINITION(
20+
string, WDT_FLAG_SYM(option_type),
21+
facebook::wdt::WdtOptions::FLASH_OPTION_TYPE,
22+
"WDT option type. Options are initialized to different values "
23+
"depending on the type. Individual options can still be changed using "
24+
"specific flags. Use -" WDT_FLAG_STR(print_options) " to see values")
2025

2126
namespace facebook {
2227
namespace wdt {
2328

24-
const std::string FLAGS_PREFIX = "wdt_";
29+
const std::string FLAGS_PREFIX = WDT_TOSTR(WDT_LONG_PREFIX);
2530

26-
void WdtFlags::initializeFromFlags() {
27-
LOG(INFO) << "Running WDT " << Protocol::getFullVersion();
28-
#define ASSIGN_OPT
29-
#include "WdtFlags.cpp.inc" //nolint
30-
#undef ASSIGN_OPT
31-
std::set<std::string> userSpecifiedFlags = getUserSpecifiedOptions();
32-
WdtOptions::getMutable().modifyOptions(FLAGS_OPTION_TYPE, userSpecifiedFlags);
33-
}
34-
35-
void WdtFlags::printOptions() {
36-
#define PRINT_OPT
37-
#include "WdtFlags.cpp.inc" //nolint
38-
#undef PRINT_OPT
39-
}
31+
// Internal utilities
4032

41-
std::string WdtFlags::getOptionNameFromFlagName(const std::string &flagName) {
33+
std::string getOptionNameFromFlagName(const std::string &flagName) {
4234
#ifndef STANDALONE_APP
4335
// extra wdt_ prefix is added in this case
4436
if (flagName.compare(0, FLAGS_PREFIX.size(), FLAGS_PREFIX) == 0) {
@@ -49,17 +41,9 @@ std::string WdtFlags::getOptionNameFromFlagName(const std::string &flagName) {
4941
return flagName;
5042
}
5143

52-
std::string WdtFlags::getFlagNameFromOptionName(const std::string &optionName) {
53-
#ifndef STANDALONE_APP
54-
// extra wdt_ prefix has to be added
55-
std::string flagName;
56-
folly::toAppend(FLAGS_PREFIX, optionName, &flagName);
57-
return flagName;
58-
#endif
59-
return optionName;
60-
}
44+
// getFlagNameFromOptionName is WDT_FLAG_STR()
6145

62-
std::set<std::string> WdtFlags::getUserSpecifiedOptions() {
46+
std::set<std::string> getUserSpecifiedOptions() {
6347
std::set<std::string> userSpecifiedFlags;
6448
std::vector<google::CommandLineFlagInfo> allFlags;
6549
google::GetAllFlags(&allFlags);
@@ -75,5 +59,22 @@ std::set<std::string> WdtFlags::getUserSpecifiedOptions() {
7559
}
7660
return userSpecifiedFlags;
7761
}
62+
63+
void WdtFlags::initializeFromFlags() {
64+
LOG(INFO) << "Running WDT " << Protocol::getFullVersion();
65+
#define ASSIGN_OPT
66+
#include "WdtFlags.cpp.inc" //nolint
67+
#undef ASSIGN_OPT
68+
std::set<std::string> userSpecifiedFlags = getUserSpecifiedOptions();
69+
WdtOptions::getMutable().modifyOptions(WDT_FLAG_VAR(option_type),
70+
userSpecifiedFlags);
71+
}
72+
73+
void WdtFlags::printOptions(std::ostream &out) {
74+
out << "Options current value:" << std::endl;
75+
#define PRINT_OPT
76+
#include "WdtFlags.cpp.inc" //nolint
77+
#undef PRINT_OPT
78+
}
7879
}
7980
}

WdtFlags.h

+4-23
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,20 @@
77
* of patent rights can be found in the PATENTS file in the same directory.
88
*/
99
#pragma once
10-
#include <gflags/gflags.h>
11-
#include <iostream>
12-
#include "WdtOptions.h"
13-
#define DECLARE_ONLY
14-
#include "WdtFlags.cpp.inc"
15-
#undef DECLARE_ONLY
16-
17-
#define FLAGS_OPTION_TYPE FLAG_VALUE(option_type)
18-
FLAG_DECLARATION(string, PREFIX(option_type))
10+
#include <ostream>
1911

2012
namespace facebook {
2113
namespace wdt {
2214
class WdtFlags {
2315
public:
2416
/**
2517
* Set the values of options in WdtOptions from corresponding flags
18+
* TODO: return the options (or set the ones passed in)
2619
*/
2720
static void initializeFromFlags();
2821

29-
static void printOptions();
30-
31-
/**
32-
* Returns option name from flag name
33-
*/
34-
static std::string getOptionNameFromFlagName(const std::string &flagName);
35-
36-
/**
37-
* Returns flag name from option name
38-
*/
39-
static std::string getFlagNameFromOptionName(const std::string &optionName);
40-
41-
/// returns list of options specified in the cmd line by the user
42-
static std::set<std::string> getUserSpecifiedOptions();
22+
/// TODO change this to take the options returned above
23+
static void printOptions(std::ostream &out);
4324
};
4425
}
4526
}

WdtFlagsMacros.h

+43-17
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,72 @@
11
/// override-include-guard
2+
3+
// If you make any changes to this - use g++ -E to check the generated code
4+
5+
/// Which options object to use (we can reuse those macros for fbonly options)
26
#ifndef OPTIONS
37
#define OPTIONS WdtOptions
48
#endif
59

10+
// Short symbol A is a field inside the Options struct
11+
// The flag name is either the short one DEFINE_type(A,...) or
12+
// prefixed by wdt_ so we play nice with others when making a
13+
// library (long flag)
14+
#define WDT_READ_OPT(A) facebook::wdt::OPTIONS::get().A
15+
#define WDT_WRITE_OPT(A) facebook::wdt::OPTIONS::getMutable().A
16+
17+
// Generic macros to concat and stringify:
18+
// Turns wdt_ and foo into wdt_foo
19+
#define WDT_CONCAT1(a, b) a##b
20+
#define WDT_CONCAT(a, b) WDT_CONCAT1(a, b)
21+
// Turns a symbol into a string literal ie foo to "foo"
22+
// Needs two steps so WDT_TOSTR(WDT_CONCAT(wdt_,foo)) gives "wdt_foo"
23+
// and not "WDT_CONCAT(wdt_,foo)"
24+
#define WDT_TOSTR1(x) #x
25+
#define WDT_TOSTR(x) WDT_TOSTR1(x)
26+
27+
#define WDT_LONG_PREFIX wdt_
28+
629
#ifndef STANDALONE_APP
7-
#define PREFIX(argument) wdt_##argument
30+
#define WDT_PREFIX(argument) WDT_CONCAT(WDT_LONG_PREFIX, argument)
831
#else
9-
#define PREFIX(argument) argument
32+
#define WDT_PREFIX(argument) argument
1033
#endif
11-
#define VALUE(A) facebook::wdt::OPTIONS::get().A
1234

13-
#define VALUE_X(argument) FLAGS_##argument
14-
#define CAT(argument) VALUE_X(argument)
15-
#define FLAG_VALUE(argument) CAT(PREFIX(argument))
35+
// Symbol. eg wdt_foo
36+
#define WDT_FLAG_SYM(A) WDT_PREFIX(A)
37+
// String version eg "wdt_foo"
38+
#define WDT_FLAG_STR(A) WDT_TOSTR(WDT_FLAG_SYM(A))
39+
// Flag variable eg FLAGS_wdt_foo
40+
#define WDT_FLAG_VAR(A) WDT_CONCAT(FLAGS_, WDT_FLAG_SYM(A))
1641

1742
#ifdef WDT_OPT
1843
#undef WDT_OPT
1944
#endif
2045

46+
/// Setup variants to replace WDT_OPT by the right code depending
47+
/// on the mode/context. Trailing semi colon is expected to be in the .inc
2148
#ifdef ASSIGN_OPT
2249
// Assign option from flags
23-
#define WDT_OPT(argument, type, description) \
24-
facebook::wdt::OPTIONS::getMutable().argument = FLAG_VALUE(argument);
50+
#define WDT_OPT(A, type, description) WDT_WRITE_OPT(A) = WDT_FLAG_VAR(A)
2551
#else
2652
#ifdef PRINT_OPT
2753
// print options
28-
#define WDT_OPT(argument, type, description) \
29-
LOG(INFO) << #argument << " " << facebook::wdt::OPTIONS::get().argument;
54+
#define WDT_OPT(A, type, description) \
55+
out << WDT_TOSTR(A) << " " << WDT_READ_OPT(A) << std::endl
3056
#else
31-
32-
#define FLAG_DECLARATION(type, argument) DECLARE_##type(argument);
33-
#define FLAG_DEFINITION(type, argument, value, description) \
57+
// google flag define or declare:
58+
#define WDT_FLAG_DECLARATION(type, argument) DECLARE_##type(argument);
59+
#define WDT_FLAG_DEFINITION(type, argument, value, description) \
3460
DEFINE_##type(argument, value, description);
3561

3662
#ifdef DECLARE_ONLY
3763
// declare flags
38-
#define WDT_OPT(argument, type, description) \
39-
FLAG_DECLARATION(type, PREFIX(argument))
64+
#define WDT_OPT(A, type, description) \
65+
WDT_FLAG_DECLARATION(type, WDT_FLAG_SYM(A))
4066
#else
4167
// define flags
42-
#define WDT_OPT(argument, type, description) \
43-
FLAG_DEFINITION(type, PREFIX(argument), VALUE(argument), description)
68+
#define WDT_OPT(A, type, description) \
69+
WDT_FLAG_DEFINITION(type, WDT_FLAG_SYM(A), WDT_READ_OPT(A), description)
4470
#endif
4571
#endif
4672
#endif

wdtCmdLine.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#define ADDITIONAL_SENDER_SETUP
2424
#endif
2525

26+
// This can be the fbonly version (extended flags/options)
2627
#ifndef FLAGS
2728
#define FLAGS WdtFlags
2829
#endif
@@ -117,15 +118,15 @@ int main(int argc, char *argv[]) {
117118
usage.append(google::ProgramInvocationShortName());
118119
usage.append(" # for a server/receiver\n\t");
119120
usage.append(google::ProgramInvocationShortName());
120-
usage.append(" -destination host # for a sender");
121+
usage.append(" -connection_url url_produced_by_receiver # for a sender");
121122
google::SetUsageMessage(usage);
122123
google::ParseCommandLineFlags(&argc, &argv, true);
123124
google::InitGoogleLogging(argv[0]);
124125
signal(SIGPIPE, SIG_IGN);
125126

126127
FLAGS::initializeFromFlags();
127128
if (FLAGS_print_options) {
128-
FLAGS::printOptions();
129+
FLAGS::printOptions(std::cout);
129130
return 0;
130131
}
131132

0 commit comments

Comments
 (0)