Skip to content

Commit

Permalink
Show version and commit hash in a more pretty way (#1252)
Browse files Browse the repository at this point in the history
  • Loading branch information
PragmaTwice authored Feb 8, 2023
1 parent 2c15fa4 commit 8cb9cfa
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ execute_process(COMMAND sh -c "cat src/VERSION.txt"
execute_process(COMMAND git rev-parse --short HEAD
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} OUTPUT_VARIABLE GIT_SHA)
string(STRIP "${GIT_SHA}" GIT_SHA)
if ((PROJECT_VERSION STREQUAL "unstable") AND (GIT_SHA STREQUAL ""))
message(WARNING "It is highly recommended to build the unstable branch in a Git repo")
endif ()
configure_file(src/version.h.in ${PROJECT_BINARY_DIR}/version.h)

if ((CMAKE_CXX_COMPILER_ID STREQUAL "GNU") OR (CMAKE_CXX_COMPILER_ID STREQUAL "Clang"))
Expand Down
2 changes: 1 addition & 1 deletion src/VERSION.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
999.999.999
unstable
25 changes: 19 additions & 6 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,26 @@ extern "C" void signalHandler(int sig) {
}
}

std::ostream &printVersion(std::ostream &os) {
os << "kvrocks ";

if (VERSION != "unstable") {
os << "version ";
}

os << VERSION;

if (!GIT_COMMIT.empty()) {
os << " (commit " << GIT_COMMIT << ")";
}

return os;
}

extern "C" void segvHandler(int sig, siginfo_t *info, void *secret) {
void *trace[100];

LOG(ERROR) << "======= Ooops! kvrocks " << VERSION << " @" << GIT_COMMIT << " got signal: " << strsignal(sig) << " ("
<< sig << ") =======";
LOG(ERROR) << "======= Ooops! " << printVersion << " got signal: " << strsignal(sig) << " (" << sig << ") =======";
int trace_size = backtrace(trace, sizeof(trace) / sizeof(void *));
char **messages = backtrace_symbols(trace, trace_size);

Expand Down Expand Up @@ -138,8 +153,6 @@ static void printUsage(const char *program) {
<< "overwrite specific config option <config-key> to <config-value>" << std::endl;
}

static void printVersion(std::ostream &os) { os << "kvrocks version " << VERSION << " @" << GIT_COMMIT << std::endl; }

static CLIOptions parseCommandLineOptions(int argc, char **argv) {
using namespace std::string_view_literals;
CLIOptions opts;
Expand All @@ -148,7 +161,7 @@ static CLIOptions parseCommandLineOptions(int argc, char **argv) {
if ((argv[i] == "-c"sv || argv[i] == "--config"sv) && i + 1 < argc) {
opts.conf_file = argv[++i];
} else if (argv[i] == "-v"sv || argv[i] == "--version"sv) {
printVersion(std::cout);
std::cout << printVersion << std::endl;
std::exit(0);
} else if (argv[i] == "-h"sv || argv[i] == "--help"sv) {
printUsage(*argv);
Expand Down Expand Up @@ -311,7 +324,7 @@ int main(int argc, char *argv[]) {
}

initGoogleLog(&config);
printVersion(LOG(INFO));
LOG(INFO) << printVersion;
// Tricky: We don't expect that different instances running on the same port,
// but the server use REUSE_PORT to support the multi listeners. So we connect
// the listen port to check if the port has already listened or not.
Expand Down
6 changes: 4 additions & 2 deletions src/version.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@

#pragma once

#define VERSION "@PROJECT_VERSION@"
#define GIT_COMMIT "@GIT_SHA@"
#include <string_view>

inline constexpr std::string_view VERSION = "@PROJECT_VERSION@";
inline constexpr std::string_view GIT_COMMIT = "@GIT_SHA@";

0 comments on commit 8cb9cfa

Please sign in to comment.