Skip to content

Commit

Permalink
add hex dump for db values
Browse files Browse the repository at this point in the history
  • Loading branch information
ggeorgiev committed Jun 10, 2016
1 parent d0d4cc2 commit 167c251
Show file tree
Hide file tree
Showing 13 changed files with 161 additions and 102 deletions.
9 changes: 7 additions & 2 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ clang++ -D NDEBUG -I src/ -O3 -c src/err/err.cpp \
-isystem boost/include/ -isystem fmt/include/ -isystem spdlog/include/ \
-isystem src/system/ -o build/release/src/err/err.cpp.o -std=c++14 &
if [ ! -e build/release/src/log/ ]; then mkdir build/release/src/log/; fi
echo Compile src/log/hex.cpp
clang++ -D NDEBUG -I src/ -O3 -c src/log/hex.cpp -isystem boost/include/ \
-isystem fmt/include/ -isystem spdlog/include/ -isystem src/system/ \
-o build/release/src/log/hex.cpp.o -std=c++14 &
echo Compile src/log/log.cpp
clang++ -D NDEBUG -I src/ -O3 -c src/log/log.cpp -isystem boost/include/ \
-isystem fmt/include/ -isystem spdlog/include/ -isystem src/system/ \
Expand Down Expand Up @@ -382,8 +386,9 @@ clang++ -L boost/lib/ -L fmt/lib/ -L protobuf/lib/ -L rocksdb/lib/ \
build/release/src/dom/cxx/cxx_library.cpp.o \
build/release/src/dom/cxx/cxx_program.cpp.o \
build/release/src/engine/cxx_engine.cpp.o build/release/src/err/err.cpp.o \
build/release/src/log/log.cpp.o build/release/src/logex/log.cpp.o \
build/release/src/main.cpp.o build/release/src/option/verbose.cpp.o \
build/release/src/log/hex.cpp.o build/release/src/log/log.cpp.o \
build/release/src/logex/log.cpp.o build/release/src/main.cpp.o \
build/release/src/option/verbose.cpp.o \
build/release/src/parser/cxx/cxx_parser.cpp.o \
build/release/src/parser/dbs/dbs_config_parser.cpp.o \
build/release/src/parser/dbs/dbs_parser.cpp.o \
Expand Down
8 changes: 7 additions & 1 deletion src/config.dbs
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
verbose: -crc -sys -load -db -parse +task;
verbose:
-crc
-sys
+db load
-db
-parse
+task;
24 changes: 16 additions & 8 deletions src/db/database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ namespace db
{
doim::TagSetSPtr Database::gDbLoadSet =
doim::TagSet::global({&doim::gDbTag, &doim::gLoadTag}, gDbLoadSet);
doim::TagSetSPtr Database::gDbSaveSet =
doim::TagSet::global({&doim::gDbTag, &doim::gSaveTag}, gDbSaveSet);

DatabaseSPtr gDatabase = im::InitializationManager::subscribe(gDatabase);

Expand Down Expand Up @@ -57,19 +59,17 @@ ECode Database::get(const string_view& key,
&value);
if (!status.ok())
{
if (status.code() == rocksdb::Status::kNotFound)
if (status.code() != rocksdb::Status::kNotFound)
{
value = defaultValue.to_string();
EHEnd;
const auto& msg = status.ToString();
EHBan(kDatabase, msg);
}

const auto& msg = status.ToString();
EHBan(kDatabase, msg);
value = defaultValue.to_string();
}
LOGEX(gDbLoadSet,
"Read key: \"{}\", value:\n{}",
key.to_string(),
dbslog::dump(value));
dbslog::hexdump(value));
EHEnd;
}

Expand All @@ -87,7 +87,10 @@ ECode Database::get(const string_view& key, string& value)
EHBan(kDatabase, msg);
}

DLOG(R"(Read key: "{}", value: "{}")", key.to_string(), value);
LOGEX(gDbLoadSet,
"Read key: \"{}\", value:\n{}",
key.to_string(),
dbslog::hexdump(value));
EHEnd;
}

Expand All @@ -101,6 +104,11 @@ ECode Database::put(const string_view& key, const string_view& value)
const auto& msg = status.ToString();
EHBan(kDatabase, msg);
}
LOGEX(gDbSaveSet,
"Put key: \"{}\", value:\n{}",
key.to_string(),
dbslog::hexdump(value));

EHEnd;
}

Expand Down
1 change: 1 addition & 0 deletions src/db/database.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Database
{
public:
static doim::TagSetSPtr gDbLoadSet;
static doim::TagSetSPtr gDbSaveSet;

static constexpr int rank()
{
Expand Down
40 changes: 40 additions & 0 deletions src/log/gtest/hex-utest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright © 2016 George Georgiev. All rights reserved.
//

#include "log/hex.h"
#include "gtest/framework.h"
#include <memory>
#include <str>

TEST(HexTest, simple)
{
struct Test
{
string input;
string dump;
};

const auto zero = string(5, 0);

Test tests[]{
Test{.input = zero, .dump = "0000 : ..... 00 00 00 00 00 \n"},
Test{.input = "dumptest",
.dump = "0000 : dumptest 64 75 6D 70 74 65 73 74 \n"},
Test{.input = "dumptestfullline",
.dump = "0000 : dumptestfullline 64 75 6D 70 74 65 73 74 "
" 66 75 6C 6C 6C 69 6E 65 \n"},
Test{.input = "dumptest2partiallines",
.dump = "0000 : dumptest2partial 64 75 6D 70 74 65 73 74 "
" 32 70 61 72 74 69 61 6C \n"
"0010 : lines 6C 69 6E 65 73 \n"},
Test{.input = "\n\r\t\v\xff",
.dump = "0000 : ..... 0A 0D 09 0B FF \n"},
};

for (const auto& test : tests)
{
SCOPED_TRACE(::testing::Message() << "Input: \"" << test.input << "\"");
const auto& result = dbslog::hexdump(test.input);
EXPECT_EQ(test.dump, result);
}
}
2 changes: 1 addition & 1 deletion src/log/gtest/log-utest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void baz()
bar();
}

TEST(Log, SLOW_LOG_FUNCTION)
TEST(Log, VERYSLOW_LOG_FUNCTION)
{
TLOG_FUNCTION;
baz();
Expand Down
56 changes: 56 additions & 0 deletions src/log/hex.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright © 2016 George Georgiev. All rights reserved.
//

#pragma once

#include <sstream>
#include <string>
#include <string_view>

namespace dbslog
{
std::string hexdump(const string_view& array,
std::size_t group = 8,
std::size_t width = 16)
{
const auto start = array.begin();
const auto end = array.end();

std::stringstream stream;

auto line = start;
while (line != end)
{
stream.width(4);
stream.fill('0');
stream << std::hex << line - start << " : ";
std::size_t lineLength = std::min(width, static_cast<std::size_t>(end - line));
for (auto next = line; next != end && next != line + width; ++next)
stream << (*next < 32 ? '.' : *next);

stream << std::string(width - lineLength, ' ');
stream << " ";

std::size_t split = group;
for (auto next = line; next != end && next != line + width; ++next)
{
if (split-- == 0)
{
split = group;
stream << " ";
}
if (next != line)
stream << " ";
stream.width(2);
stream.fill('0');
int byte = static_cast<unsigned int>(static_cast<unsigned char>(*next));
stream << std::hex << std::uppercase << byte;
stream << " ";
}
stream << std::endl;
line = line + lineLength;
}

return stream.str();
}
}
15 changes: 15 additions & 0 deletions src/log/hex.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright © 2016 George Georgiev. All rights reserved.
//

#pragma once

#include <sstream>
#include <string>
#include <string_view>

namespace dbslog
{
std::string hexdump(const string_view& array,
std::size_t group = 8,
std::size_t width = 16);
}
56 changes: 0 additions & 56 deletions src/log/hex.hpp

This file was deleted.

4 changes: 3 additions & 1 deletion src/log/log.dbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ cxx_library log
@visibility=public
@directory=..:

log/hex.hpp
log/hex.h
log/log.h
log/log_function.hpp
;
Expand All @@ -21,6 +21,7 @@ cxx_library log
;

cxx_file:
hex.cpp
log.cpp
;
;
Expand All @@ -37,6 +38,7 @@ cxx_library log-test
;

cxx_file:
gtest/hex-utest.cpp
gtest/log-utest.cpp
;
;
2 changes: 1 addition & 1 deletion src/log/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#pragma once

#include "im/initialization_manager.hpp"
#include "log/hex.hpp"
#include "log/hex.h"
#include <fmt/format.h> // IWYU pragma: export
#include <spdlog/common.h> // IWYU pragma: export
#include <spdlog/logger.h> // IWYU pragma: export
Expand Down
30 changes: 2 additions & 28 deletions src/logex/gtest/log-utest.cpp
Original file line number Diff line number Diff line change
@@ -1,36 +1,10 @@
// Copyright © 2016 George Georgiev. All rights reserved.
//

#include "log/log.h"
#include "log/log_function.hpp"
#include "logex/log.h"
#include "gtest/framework.h"
#include <memory>

TEST(Log, basic)
TEST(Logex, basic)
{
dbslog::gLogger->info() << "test";
dbslog::gLogger->info("test");
}

void foo()
{
TLOG_FUNCTION;
DLOG("test");
}
void bar()
{
TLOG_FUNCTION;
foo();
}

void baz()
{
TLOG_FUNCTION;
bar();
}

TEST(Log, SLOW_LOG_FUNCTION)
{
TLOG_FUNCTION;
baz();
}
Loading

0 comments on commit 167c251

Please sign in to comment.