Skip to content

Commit

Permalink
add logger-level option
Browse files Browse the repository at this point in the history
  • Loading branch information
acquamarin committed Nov 16, 2022
1 parent 78d938a commit 5f79866
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 15 deletions.
6 changes: 6 additions & 0 deletions src/common/types/include/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <string>
#include <vector>

#include "spdlog/spdlog.h"

using namespace std;

namespace kuzu {
Expand Down Expand Up @@ -123,5 +125,9 @@ string getRelDirectionAsString(RelDirection relDirection);

enum class DBFileType : uint8_t { ORIGINAL = 0, WAL_VERSION = 1 };

spdlog::level::level_enum convertStrToLevelEnum(string loggingLevel);

string convertLevelEnumToStr(spdlog::level::level_enum);

} // namespace common
} // namespace kuzu
42 changes: 42 additions & 0 deletions src/common/types/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "include/value.h"

#include "src/common/include/exception.h"
#include "src/common/include/utils.h"

namespace kuzu {
namespace common {
Expand Down Expand Up @@ -176,5 +177,46 @@ string getRelDirectionAsString(RelDirection direction) {
return (FWD == direction) ? "forward" : "backward";
}

spdlog::level::level_enum convertStrToLevelEnum(string loggingLevel) {
StringUtils::toLower(loggingLevel);
if (loggingLevel == "info") {
return spdlog::level::level_enum::info;
} else if (loggingLevel == "debug") {
return spdlog::level::level_enum::debug;
} else if (loggingLevel == "err") {
return spdlog::level::level_enum::err;
}
throw ConversionException(
StringUtils::string_format("Unsupported logging level: %s.", loggingLevel.c_str()));
}

string convertLevelEnumToStr(spdlog::level::level_enum levelEnum) {
switch (levelEnum) {
case spdlog::level::level_enum::trace: {
return "trace";
}
case spdlog::level::level_enum::debug: {
return "debug";
}
case spdlog::level::level_enum::info: {
return "info";
}
case spdlog::level::level_enum::warn: {
return "warn";
}
case spdlog::level::level_enum::err: {
return "err";
}
case spdlog::level::level_enum::critical: {
return "critical";
}
case spdlog::level::level_enum::off: {
return "off";
}
default:
assert(false);
}
}

} // namespace common
} // namespace kuzu
10 changes: 10 additions & 0 deletions src/main/database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ void Database::initLoggers() {
LoggerUtils::getOrCreateLogger("wal");
}

void Database::setLoggingLevel(spdlog::level::level_enum loggingLevel) {
if (loggingLevel != spdlog::level::level_enum::debug &&
loggingLevel != spdlog::level::level_enum::info &&
loggingLevel != spdlog::level::level_enum::err) {
printf("Unsupported logging level: %s.", convertLevelEnumToStr(loggingLevel).c_str());
return;
}
spdlog::set_level(loggingLevel);
}

void Database::resizeBufferManager(uint64_t newSize) {
systemConfig.defaultPageBufferPoolSize = newSize * StorageConfig::DEFAULT_PAGES_BUFFER_RATIO;
systemConfig.largePageBufferPoolSize = newSize * StorageConfig::LARGE_PAGES_BUFFER_RATIO;
Expand Down
4 changes: 3 additions & 1 deletion src/main/include/database.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,12 @@ class Database {

explicit Database(const DatabaseConfig& databaseConfig, const SystemConfig& systemConfig);

~Database() = default;
void setLoggingLevel(spdlog::level::level_enum loggingLevel);

void resizeBufferManager(uint64_t newSize);

~Database() = default;

private:
// TODO(Semih): This is refactored here for now to be able to test transaction behavior
// in absence of the frontend support. Consider moving this code to connection.cpp.
Expand Down
4 changes: 4 additions & 0 deletions tools/python_api/include/py_database.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ class PyDatabase {
friend class PyConnection;

public:
inline void setLoggingLevel(spdlog::level::level_enum logging_level) {
database->setLoggingLevel(logging_level);
}

static void initialize(py::handle& m);

explicit PyDatabase(const string& databasePath, uint64_t bufferPoolSize);
Expand Down
10 changes: 9 additions & 1 deletion tools/python_api/kuzu_binding.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
#include "include/py_connection.h"
#include "include/py_database.h"
#include "include/py_query_result_converter.h"

void bindEnumTypes(py::module& m) {
py::enum_<spdlog::level::level_enum>(m, "loggingLevel")
.value("debug", spdlog::level::level_enum::debug)
.value("info", spdlog::level::level_enum::info)
.value("err", spdlog::level::level_enum::err)
.export_values();
}

void bind(py::module& m) {
bindEnumTypes(m);
PyDatabase::initialize(m);
PyConnection::initialize(m);
PyQueryResult::initialize(m);
Expand Down
3 changes: 2 additions & 1 deletion tools/python_api/py_database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ void PyDatabase::initialize(py::handle& m) {
py::class_<PyDatabase>(m, "database")
.def(py::init<const string&, uint64_t>(), py::arg("database_path"),
py::arg("buffer_pool_size") = 0)
.def("resize_buffer_manager", &PyDatabase::resizeBufferManager, py::arg("new_size"));
.def("resize_buffer_manager", &PyDatabase::resizeBufferManager, py::arg("new_size"))
.def("set_logging_level", &PyDatabase::setLoggingLevel, py::arg("logging_level"));
}

PyDatabase::PyDatabase(const string& databasePath, uint64_t bufferPoolSize) {
Expand Down
10 changes: 5 additions & 5 deletions tools/python_api/test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest

from tools.python_api import _kuzu as gdb
from tools.python_api import _kuzu as kuzu


# Note conftest is the default file name for sharing fixture through multiple test files. Do not change file name.
Expand All @@ -11,8 +11,8 @@ def init_tiny_snb(tmp_path):
if os.path.exists(tmp_path):
os.rmdir(tmp_path)
output_path = str(tmp_path)
db = gdb.database(output_path)
conn = gdb.connection(db)
db = kuzu.database(output_path)
conn = kuzu.connection(db)
conn.execute("CREATE NODE TABLE person (ID INT64, fName STRING, gender INT64, isStudent BOOLEAN, isWorker BOOLEAN, "
"age INT64, eyeSight DOUBLE, birthdate DATE, registerTime TIMESTAMP, lastJobDuration "
"INTERVAL, workedHours INT64[], usedNames STRING[], courseScoresPerTerm INT64[][], PRIMARY "
Expand All @@ -27,6 +27,6 @@ def init_tiny_snb(tmp_path):

@pytest.fixture
def establish_connection(init_tiny_snb):
db = gdb.database(init_tiny_snb, buffer_pool_size=256 * 1024 * 1024)
conn = gdb.connection(db, num_threads=4)
db = kuzu.database(init_tiny_snb, buffer_pool_size=256 * 1024 * 1024)
conn = kuzu.connection(db, num_threads=4)
return conn, db
11 changes: 5 additions & 6 deletions tools/python_api/test/example.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from tools.python_api import _kuzu as gdb
from tools.python_api import _kuzu as kuzu


databaseDir = "path_to_serialized_database"
db = gdb.database(databaseDir)
conn = gdb.connection(db)
databaseDir = "path to database file"
db = kuzu.database(databaseDir)
conn = kuzu.connection(db)
query = "MATCH (a:person) RETURN *;"
result = conn.query(query)
result = conn.execute(query)
while result.hasNext():
print(result.getNext())
result.close()
10 changes: 10 additions & 0 deletions tools/python_api/test/test_df.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import numpy as np

from pandas import Timestamp, Timedelta, isna
from tools.python_api import _kuzu as kuzu


def test_to_df(establish_connection):
Expand Down Expand Up @@ -77,3 +78,12 @@ def _test_to_df(conn):
db.resize_buffer_manager(512 * 1024 * 1024)
conn.set_max_threads_for_exec(4)
_test_to_df(conn)

db.set_logging_level(kuzu.loggingLevel.debug)
_test_to_df(conn)

db.set_logging_level(kuzu.loggingLevel.info)
_test_to_df(conn)

db.set_logging_level(kuzu.loggingLevel.err)
_test_to_df(conn)
14 changes: 13 additions & 1 deletion tools/shell/embedded_shell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ struct ShellCommand {
const string LIST_RELS = ":list_rels";
const string SHOW_NODE = ":show_node";
const string SHOW_REL = ":show_rel";
const string LOGGING_LEVEL = ":logging_level";
const vector<string> commandList = {HELP, CLEAR, QUIT, THREAD, BUFFER_MANAGER_SIZE,
BUFFER_MANAGER_DEBUG_INFO, LIST_NODES, LIST_RELS, SHOW_NODE, SHOW_REL};
BUFFER_MANAGER_DEBUG_INFO, LIST_NODES, LIST_RELS, SHOW_NODE, SHOW_REL, LOGGING_LEVEL};
} shellCommand;

const char* TAB = " ";
Expand Down Expand Up @@ -207,6 +208,14 @@ void EmbeddedShell::run() {
printNodeSchema(lineStr.substr(shellCommand.SHOW_NODE.length()));
} else if (lineStr.rfind(shellCommand.SHOW_REL) == 0) {
printRelSchema(lineStr.substr(shellCommand.SHOW_REL.length()));
} else if (lineStr.rfind(shellCommand.LOGGING_LEVEL) == 0) {
auto logLevelStr = regex_replace(
lineStr.substr(shellCommand.LOGGING_LEVEL.length()), std::regex("^ +"), "");
try {
auto logLevelEnum = convertStrToLevelEnum(logLevelStr);
database->setLoggingLevel(logLevelEnum);
printf("logging level has been set to: %s.\n", logLevelStr.c_str());
} catch (ConversionException& e) { printf(e.what()); }
} else if (!lineStr.empty()) {
ss.clear();
ss.str(lineStr);
Expand Down Expand Up @@ -286,6 +295,9 @@ void EmbeddedShell::printHelp() {
shellCommand.THREAD.c_str(), TAB);
printf("%s%s [bm_size_in_bytes] %sset buffer manager size in bytes\n", TAB,
shellCommand.BUFFER_MANAGER_SIZE.c_str(), TAB);
printf("%s%s [logging_level] %sset logging level of database, available options: debug, info, "
"err\n",
TAB, shellCommand.LOGGING_LEVEL.c_str(), TAB);
printf("%s%s %slist all node tables\n", TAB, shellCommand.LIST_NODES.c_str(), TAB);
printf("%s%s %slist all rel tables\n", TAB, shellCommand.LIST_RELS.c_str(), TAB);
printf("%s%s %s[table_name] show node schema\n", TAB, shellCommand.SHOW_NODE.c_str(), TAB);
Expand Down

0 comments on commit 5f79866

Please sign in to comment.