Skip to content
This repository has been archived by the owner on Mar 22, 2023. It is now read-only.

add logging #854

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ option(COVERAGE "run coverage test" OFF)

option(DEVELOPER_MODE "enable developer's checks" OFF)
option(CHECK_CPP_STYLE "check code style of C++ sources" OFF)
option(USE_CCACHE "Use ccache if it is available in the system" ON)
option(USE_CCACHE "use ccache if it is available in the system" ON)
option(LOGGING "enable logging of debug information in library" OFF)

# Each engine can be enabled separately.
# By default all experimental engines are turned off.
Expand Down Expand Up @@ -76,6 +77,8 @@ set(SOURCE_FILES
src/engines/blackhole.h
src/out.cc
src/out.h
src/logging.c
src/logging.h
)
# Add each engine source separately
if(ENGINE_CMAP)
Expand Down Expand Up @@ -247,6 +250,10 @@ if(ENGINE_VCMAP)
target_link_libraries(pmemkv PRIVATE ${TBB_LIBRARIES})
endif()

if(LOGGING)
add_definitions(-DPMEMKV_USE_LOGGING)
endif()

## Configure make install/uninstall and packages
configure_file(libpmemkv.pc.in libpmemkv.pc @ONLY)
install(FILES ${CMAKE_BINARY_DIR}/libpmemkv.pc
Expand Down
18 changes: 9 additions & 9 deletions examples/pmemkv_basic_c/pmemkv_basic.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
assert(expr); \
} while (0)

#define LOG(msg) puts(msg)
#define LOG_EX(msg) puts(msg)
#define MAX_VAL_LEN 64

static const uint64_t SIZE = 1024UL * 1024UL * 1024UL;
Expand All @@ -39,7 +39,7 @@ int main(int argc, char *argv[])
}

/* See libpmemkv_config(3) for more detailed example of config creation */
LOG("Creating config");
LOG_EX("Creating config");
pmemkv_config *cfg = pmemkv_config_new();
ASSERT(cfg != NULL);

Expand All @@ -50,13 +50,13 @@ int main(int argc, char *argv[])
s = pmemkv_config_put_force_create(cfg, true);
ASSERT(s == PMEMKV_STATUS_OK);

LOG("Opening pmemkv database with 'cmap' engine");
LOG_EX("Opening pmemkv database with 'cmap' engine");
pmemkv_db *db = NULL;
s = pmemkv_open("cmap", cfg, &db);
ASSERT(s == PMEMKV_STATUS_OK);
ASSERT(db != NULL);

LOG("Putting new key");
LOG_EX("Putting new key");
const char *key1 = "key1";
const char *value1 = "value1";
s = pmemkv_put(db, key1, strlen(key1), value1, strlen(value1));
Expand All @@ -67,13 +67,13 @@ int main(int argc, char *argv[])
ASSERT(s == PMEMKV_STATUS_OK);
ASSERT(cnt == 1);

LOG("Reading key back");
LOG_EX("Reading key back");
char val[MAX_VAL_LEN];
s = pmemkv_get_copy(db, key1, strlen(key1), val, MAX_VAL_LEN, NULL);
ASSERT(s == PMEMKV_STATUS_OK);
ASSERT(!strcmp(val, "value1"));

LOG("Iterating existing keys");
LOG_EX("Iterating existing keys");
const char *key2 = "key2";
const char *value2 = "value2";
const char *key3 = "key3";
Expand All @@ -82,16 +82,16 @@ int main(int argc, char *argv[])
pmemkv_put(db, key3, strlen(key3), value3, strlen(value3));
pmemkv_get_all(db, &get_kv_callback, NULL);

LOG("Removing existing key");
LOG_EX("Removing existing key");
s = pmemkv_remove(db, key1, strlen(key1));
ASSERT(s == PMEMKV_STATUS_OK);
ASSERT(pmemkv_exists(db, key1, strlen(key1)) == PMEMKV_STATUS_NOT_FOUND);

LOG("Defragmenting the database");
LOG_EX("Defragmenting the database");
s = pmemkv_defrag(db, 0, 100);
ASSERT(s == PMEMKV_STATUS_OK);

LOG("Closing database");
LOG_EX("Closing database");
pmemkv_close(db);

return 0;
Expand Down
23 changes: 13 additions & 10 deletions examples/pmemkv_basic_cpp/pmemkv_basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@
#include <libpmemkv.hpp>
#include <sstream>

#include "../../src/logging.h"
#include "../../src/out.h"

#define ASSERT(expr) \
do { \
if (!(expr)) \
std::cout << pmemkv_errormsg() << std::endl; \
assert(expr); \
} while (0)
#define LOG(msg) std::cout << msg << std::endl
#define LOG_EX(msg) std::cout << msg << std::endl

using namespace pmem::kv;

Expand All @@ -31,7 +34,7 @@ int main(int argc, char *argv[])
}

/* See libpmemkv_config(3) for more detailed example of config creation */
LOG("Creating config");
LOG_EX("Creating config");
config cfg;

status s = cfg.put_path(argv[1]);
Expand All @@ -41,40 +44,40 @@ int main(int argc, char *argv[])
s = cfg.put_force_create(true);
ASSERT(s == status::OK);

LOG("Opening pmemkv database with 'cmap' engine");
LOG_EX("Opening pmemkv database with 'cmap' engine");
db *kv = new db();
ASSERT(kv != nullptr);
s = kv->open("cmap", std::move(cfg));
ASSERT(s == status::OK);

LOG("Putting new key");
LOG_EX("Putting new key");
s = kv->put("key1", "value1");
ASSERT(s == status::OK);

size_t cnt;
s = kv->count_all(cnt);
ASSERT(s == status::OK && cnt == 1);

LOG("Reading key back");
LOG_EX("Reading key back");
std::string value;
s = kv->get("key1", &value);
ASSERT(s == status::OK && value == "value1");

LOG("Iterating existing keys");
LOG_EX("Iterating existing keys");
s = kv->put("key2", "value2");
ASSERT(s == status::OK);
s = kv->put("key3", "value3");
ASSERT(s == status::OK);
kv->get_all([](string_view k, string_view v) {
LOG(" visited: " << k.data());
LOG_EX(" visited: " << k.data());
return 0;
});

LOG("Defragmenting the database");
LOG_EX("Defragmenting the database");
s = kv->defrag(0, 100);
ASSERT(s == status::OK);

LOG("Removing existing key");
LOG_EX("Removing existing key");
s = kv->remove("key1");
ASSERT(s == status::OK);
s = kv->exists("key1");
Expand All @@ -90,7 +93,7 @@ int main(int argc, char *argv[])
oss << s;
assert(oss.str() == "NOT_FOUND (2)");

LOG("Closing database");
LOG_EX("Closing database");
delete kv;

return 0;
Expand Down
12 changes: 6 additions & 6 deletions examples/pmemkv_comparator_c/pmemkv_comparator.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
} while (0)

#define MIN(x, y) (((x) < (y)) ? (x) : (y))
#define LOG(msg) puts(msg)
#define LOG_EX(msg) puts(msg)

static const uint64_t SIZE = 1024UL * 1024UL * 1024UL;

Expand Down Expand Up @@ -57,7 +57,7 @@ int main(int argc, char *argv[])
}

/* See libpmemkv_config(3) for more detailed example of config creation */
LOG("Creating config");
LOG_EX("Creating config");
pmemkv_config *cfg = pmemkv_config_new();
ASSERT(cfg != NULL);

Expand All @@ -74,13 +74,13 @@ int main(int argc, char *argv[])
s = pmemkv_config_put_comparator(cfg, cmp);
ASSERT(s == PMEMKV_STATUS_OK);

LOG("Opening pmemkv database with 'csmap' engine");
LOG_EX("Opening pmemkv database with 'csmap' engine");
pmemkv_db *db = NULL;
s = pmemkv_open("csmap", cfg, &db);
ASSERT(s == PMEMKV_STATUS_OK);
ASSERT(db != NULL);

LOG("Putting new keys");
LOG_EX("Putting new keys");
const char *key1 = "key1";
const char *value1 = "value1";
const char *key2 = "key2";
Expand All @@ -94,11 +94,11 @@ int main(int argc, char *argv[])
s = pmemkv_put(db, key3, strlen(key3), value3, strlen(value3));
ASSERT(s == PMEMKV_STATUS_OK);

LOG("Iterating over existing keys in order specified by the comparator");
LOG_EX("Iterating over existing keys in order specified by the comparator");
s = pmemkv_get_all(db, &get_kv_callback, NULL);
ASSERT(s == PMEMKV_STATUS_OK);

LOG("Closing database");
LOG_EX("Closing database");
pmemkv_close(db);

return 0;
Expand Down
17 changes: 10 additions & 7 deletions examples/pmemkv_comparator_cpp/pmemkv_comparator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@
#include <iostream>
#include <libpmemkv.hpp>

#include "../../src/logging.h"
#include "../../src/out.h"

#define ASSERT(expr) \
do { \
if (!(expr)) \
std::cout << pmemkv_errormsg() << std::endl; \
assert(expr); \
} while (0)
#define LOG(msg) std::cout << msg << std::endl
#define LOG_EX(msg) std::cout << msg << std::endl

using namespace pmem::kv;

Expand Down Expand Up @@ -52,7 +55,7 @@ int main(int argc, char *argv[])
}

/* See libpmemkv_config(3) for more detailed example of config creation */
LOG("Creating config");
LOG_EX("Creating config");
config cfg;

status s = cfg.put_path(argv[1]);
Expand All @@ -64,27 +67,27 @@ int main(int argc, char *argv[])
s = cfg.put_comparator(lexicographical_comparator{});
ASSERT(s == status::OK);

LOG("Opening pmemkv database with 'csmap' engine");
LOG_EX("Opening pmemkv database with 'csmap' engine");
db *kv = new db();
ASSERT(kv != nullptr);
s = kv->open("csmap", std::move(cfg));
ASSERT(s == status::OK);

LOG("Putting new keys");
LOG_EX("Putting new keys");
s = kv->put("key1", "value1");
ASSERT(s == status::OK);
s = kv->put("key2", "value2");
ASSERT(s == status::OK);
s = kv->put("key3", "value3");
ASSERT(s == status::OK);

LOG("Iterating over existing keys in order specified by the comparator");
LOG_EX("Iterating over existing keys in order specified by the comparator");
kv->get_all([](string_view k, string_view v) {
LOG(" visited: " << k.data());
LOG_EX(" visited: " << k.data());
return 0;
});

LOG("Closing database");
LOG_EX("Closing database");
delete kv;

return 0;
Expand Down
21 changes: 12 additions & 9 deletions examples/pmemkv_open_cpp/pmemkv_open.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@
#include <iostream>
#include <libpmemkv.hpp>

#include "../../src/logging.h"
#include "../../src/out.h"

#define ASSERT(expr) \
do { \
if (!(expr)) \
std::cout << pmemkv_errormsg() << std::endl; \
assert(expr); \
} while (0)
#define LOG(msg) std::cout << msg << std::endl
#define LOG_EX(msg) std::cout << msg << std::endl

using namespace pmem::kv;

Expand All @@ -39,48 +42,48 @@ int main(int argc, char *argv[])
}

/* See libpmemkv_config(3) for more detailed example of creating a config */
LOG("Creating config");
LOG_EX("Creating config");
config cfg;

status s = cfg.put_path(argv[1]);
ASSERT(s == status::OK);

LOG("Opening pmemkv database with 'cmap' engine");
LOG_EX("Opening pmemkv database with 'cmap' engine");
db *kv = new db();
ASSERT(kv != nullptr);
s = kv->open("cmap", std::move(cfg));
ASSERT(s == status::OK);

LOG("Putting new key");
LOG_EX("Putting new key");
s = kv->put("key1", "value1");
ASSERT(s == status::OK);

size_t cnt;
s = kv->count_all(cnt);
ASSERT(s == status::OK && cnt == 1);

LOG("Reading key back");
LOG_EX("Reading key back");
std::string value;
s = kv->get("key1", &value);
ASSERT(s == status::OK && value == "value1");

LOG("Iterating existing keys");
LOG_EX("Iterating existing keys");
s = kv->put("key2", "value2");
ASSERT(s == status::OK);
s = kv->put("key3", "value3");
ASSERT(s == status::OK);
kv->get_all([](string_view k, string_view v) {
LOG(" visited: " << k.data());
LOG_EX(" visited: " << k.data());
return 0;
});

LOG("Removing existing key");
LOG_EX("Removing existing key");
s = kv->remove("key1");
ASSERT(s == status::OK);
s = kv->exists("key1");
ASSERT(s == status::NOT_FOUND);

LOG("Closing database");
LOG_EX("Closing database");
delete kv;

return 0;
Expand Down
Loading