Skip to content

Commit

Permalink
Update logging config in init, style-changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
DinoBektesevic committed Feb 28, 2024
1 parent 2f8fca4 commit 1643fd9
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 38 deletions.
11 changes: 5 additions & 6 deletions src/kbmod/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
try:
from ._version import version as __version__ # noqa: F401
except ImportError:
warnings.warn("Unable to determine the package version. "
"This is likely a broken installation.")
warnings.warn("Unable to determine the package version. " "This is likely a broken installation.")

import os
import time
Expand Down Expand Up @@ -35,7 +34,7 @@
"level": os.environ.get("KB_LOG_LEVEL", "WARNING"),
"format": "[%(asctime)s %(levelname)s %(name)s] %(message)s",
"datefmt": "%Y-%m-%dT%H:%M:%SZ",
"converter": "gmtime"
"converter": "gmtime",
}

# Declare our own root logger, so that we don't start printing DEBUG
Expand All @@ -51,16 +50,16 @@
"default": {
"level": _SHARED_LOGGING_CONFIG["level"],
"formatter": "standard",
'class': 'logging.StreamHandler',
'stream': 'ext://sys.stderr',
"class": "logging.StreamHandler",
"stream": "ext://sys.stderr",
}
},
"loggers": {
"kbmod": {
"handlers": ["default"],
"level": _SHARED_LOGGING_CONFIG["level"],
}
}
},
}

# The timezone converter can not be configured via the config submodule for
Expand Down
54 changes: 27 additions & 27 deletions src/kbmod/search/debug_timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,63 +10,63 @@

namespace search {

DebugTimer::DebugTimer(std::string message, std::string name)
: message_(message), logger_(logging::getLogger(name)) {
DebugTimer::DebugTimer(std::string message, std::string name)
: message_(message), logger_(logging::getLogger(name)) {
start();
}
}

DebugTimer::DebugTimer(std::string message, logging::Logger* logger) : message_(message), logger_(logger) {
DebugTimer::DebugTimer(std::string message, logging::Logger* logger) : message_(message), logger_(logger) {
start();
}
}

DebugTimer::DebugTimer(std::string message) : message_(message) {
DebugTimer::DebugTimer(std::string message) : message_(message) {
std::replace(message.begin(), message.end(), ' ', '.');
std::string derived_name = "DebugTimer." + message;
logger_ = logging::getLogger(derived_name);
start();
}
}

void DebugTimer::start() {
void DebugTimer::start() {
running_ = true;
t_start_ = std::chrono::system_clock::now();
logger_->debug("Starting " + message_ + " timer.");
}
}

void DebugTimer::stop() {
void DebugTimer::stop() {
t_end_ = std::chrono::system_clock::now();
running_ = false;
auto t_delta = std::chrono::duration_cast<std::chrono::milliseconds>(t_end_ - t_start_);
logger_->debug("Finished " + message_ + " in " + std::to_string(t_delta.count() / 1000.0) + "seconds.");
}
}

double DebugTimer::read() {
double DebugTimer::read() {
std::chrono::milliseconds t_delta;
if (running_) {
std::chrono::time_point<std::chrono::system_clock> t_current_ = std::chrono::system_clock::now();
t_delta = std::chrono::duration_cast<std::chrono::milliseconds>(t_current_ - t_start_);
std::chrono::time_point<std::chrono::system_clock> t_current_ = std::chrono::system_clock::now();
t_delta = std::chrono::duration_cast<std::chrono::milliseconds>(t_current_ - t_start_);
} else {
t_delta = std::chrono::duration_cast<std::chrono::milliseconds>(t_end_ - t_start_);
t_delta = std::chrono::duration_cast<std::chrono::milliseconds>(t_end_ - t_start_);
}

double result = t_delta.count() / 1000.0;
logger_->debug("Step " + message_ + " is at " + std::to_string(result) + "seconds.");
return result;
}
}

#ifdef Py_PYTHON_H
static void debug_timer_binding(py::module& m) {
static void debug_timer_binding(py::module& m) {
using dbt = search::DebugTimer;
py::class_<dbt>(m, "DebugTimer", pydocs::DOC_DEBUG_TIMER)
.def(py::init<std::string, std::string>())
.def(py::init<std::string>())
.def(py::init([](std::string message, py::object logger) {
std::string name = std::string(py::str(logger.attr("__name__")));
return std::unique_ptr<DebugTimer>(new DebugTimer(message, name));
}))
.def("start", &dbt::start, pydocs::DOC_DEBUG_TIMER_start)
.def("stop", &dbt::stop, pydocs::DOC_DEBUG_TIMER_stop)
.def("read", &dbt::read, pydocs::DOC_DEBUG_TIMER_read);
}
.def(py::init<std::string, std::string>())
.def(py::init<std::string>())
.def(py::init([](std::string message, py::object logger) {
std::string name = std::string(py::str(logger.attr("name")));
return std::unique_ptr<DebugTimer>(new DebugTimer(message, name));
}))
.def("start", &dbt::start, pydocs::DOC_DEBUG_TIMER_start)
.def("stop", &dbt::stop, pydocs::DOC_DEBUG_TIMER_stop)
.def("read", &dbt::read, pydocs::DOC_DEBUG_TIMER_read);
}
#endif /* Py_PYTHON_H */

} /* namespace search */
6 changes: 3 additions & 3 deletions src/kbmod/search/debug_timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
namespace search {
class DebugTimer {
public:
DebugTimer(std::string message, std::string name);
DebugTimer(std::string message, logging::Logger* logger);
DebugTimer(std::string message);
DebugTimer(std::string message, std::string name);
DebugTimer(std::string message, logging::Logger* logger);
DebugTimer(std::string message);

void start();
void stop();
Expand Down
4 changes: 2 additions & 2 deletions src/kbmod/search/stack_search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ void StackSearch::set_start_bounds_y(int y_min, int y_max) {

void StackSearch::prepare_psi_phi() {
if (!psi_phi_generated) {
DebugTimer timer = DebugTimer("preparing Psi and Phi images", rs_logger);
DebugTimer timer = DebugTimer("preparing Psi and Phi images", rs_logger);
fill_psi_phi_array_from_image_stack(psi_phi_array, stack, params.encode_num_bytes, debug_info);
timer.stop();
psi_phi_generated = true;
Expand Down Expand Up @@ -157,7 +157,7 @@ Trajectory StackSearch::search_linear_trajectory(short x, short y, float vx, flo

void StackSearch::search(int ang_steps, int vel_steps, float min_ang, float max_ang, float min_vel,
float mavx, int min_observations) {
DebugTimer core_timer = DebugTimer("core search", rs_logger);
DebugTimer core_timer = DebugTimer("core search", rs_logger);
std::vector<Trajectory> search_list =
create_grid_search_list(ang_steps, vel_steps, min_ang, max_ang, min_vel, mavx);

Expand Down

0 comments on commit 1643fd9

Please sign in to comment.