Skip to content

Commit

Permalink
log: added log handler
Browse files Browse the repository at this point in the history
  • Loading branch information
rxdu committed Dec 17, 2024
1 parent c977332 commit 7b358e7
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 23 deletions.
2 changes: 0 additions & 2 deletions src/app/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
if (BUILD_QUICKVIZ_APP)
message(STATUS "Build quickviz application")
add_executable(quickviz main.cpp
# components
quickviz_application.cpp
component/log_processor.cpp
# panels
panels/menu_bar.cpp
panels/main_docking_panel.cpp
Expand Down
31 changes: 17 additions & 14 deletions src/app/panels/console_panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "panels/console_panel.hpp"

#include "imview/fonts.hpp"
#include "imview/component/logging/app_log_handler.hpp"

namespace quickviz {
ConsolePanel::ConsolePanel(std::string name) : Panel(name) {
Expand All @@ -17,24 +18,26 @@ ConsolePanel::ConsolePanel(std::string name) : Panel(name) {
this->SetNoMove(true);
this->SetWindowNoMenuButton();

static int counter = 0;
const char* categories[3] = {"info", "warn", "error"};
const char* words[] = {"Bumfuzzled", "Cattywampus", "Snickersnee",
"Abibliophobia", "Absquatulate", "Nincompoop",
"Pauciloquent"};
for (int n = 0; n < 5; n++) {
const char* category = categories[counter % IM_ARRAYSIZE(categories)];
const char* word = words[counter % IM_ARRAYSIZE(words)];
log_.AddLog(
"[%05d] [%s] Hello, current time is %.1f, here's a word: '%s'\n",
ImGui::GetFrameCount(), category, ImGui::GetTime(), word);
counter++;
}
// static int counter = 0;
// const char* categories[3] = {"info", "warn", "error"};
// const char* words[] = {"Bumfuzzled", "Cattywampus", "Snickersnee",
// "Abibliophobia", "Absquatulate", "Nincompoop",
// "Pauciloquent"};
// for (int n = 0; n < 5; n++) {
// const char* category = categories[counter % IM_ARRAYSIZE(categories)];
// const char* word = words[counter % IM_ARRAYSIZE(words)];
// log_.AddLog(
// "[%05d] [%s] Hello, current time is %.1f, here's a word: '%s'\n",
// ImGui::GetFrameCount(), category, ImGui::GetTime(), word);
// counter++;
// }
AppLogHandler::GetInstance().Log(LogLevel::kInfo, "ConsolePanel initialized");
}

void ConsolePanel::Draw() {
Begin();
log_.Draw("Example: Log");
// log_.Draw("Example: Log");
AppLogHandler::GetInstance().Draw();
End();
}
} // namespace quickviz
6 changes: 1 addition & 5 deletions src/app/panels/console_panel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,13 @@
#define QUICKVIZ_CONSOLE_PANEL_HPP

#include "imview/panel.hpp"
#include "component/log_processor.hpp"

namespace quickviz {
class ConsolePanel : public Panel {
public:
ConsolePanel(std::string name = "Debug");
ConsolePanel(std::string name = "Console");

void Draw() override;

private:
LogProcessor log_;
};
} // namespace quickviz

Expand Down
7 changes: 7 additions & 0 deletions src/app/panels/scene_panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>

#include "imview/component/logging/app_log_handler.hpp"

namespace quickviz {
ScenePanel::ScenePanel(const std::string& panel_name) : GlWidget(panel_name) {
this->SetNoMove(true);
Expand All @@ -36,6 +38,11 @@ void ScenePanel::Draw() {
glm::mat4 projection = camera_->GetProjectionMatrix(aspect_ratio);
glm::mat4 view = camera_->GetViewMatrix();

if (ImGui::IsWindowHovered()) {
std::cerr << "ScenePanel is hovered" << std::endl;
AppLogHandler::GetInstance().Log(LogLevel::kInfo, "ScenePanel is hovered");
}

UpdateView(projection, view);

GlWidget::Draw();
Expand Down
2 changes: 2 additions & 0 deletions src/imview/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ add_library(imview
src/component/buffer/scrolling_plot_buffer.cpp
src/component/event/event_dispatcher.cpp
src/component/event/async_event_dispatcher.cpp
src/component/logging/log_processor.cpp
src/component/logging/app_log_handler.cpp
)
target_link_libraries(imview PUBLIC imcore
PkgConfig::Cairo PkgConfig::Fontconfig
Expand Down
49 changes: 49 additions & 0 deletions src/imview/include/imview/component/logging/app_log_handler.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* @file app_log_handler.hpp
* @date 12/17/24
* @brief
*
* @copyright Copyright (c) 2024 Ruixiang Du (rdu)
*/

#ifndef APP_LOG_HANDLER_HPP
#define APP_LOG_HANDLER_HPP

#include "imview/component/logging/log_processor.hpp"

#include <chrono>
#include <unordered_map>

namespace quickviz {
enum class LogLevel { kDebug, kInfo, kWarn, kError, kFatal };

class AppLogHandler {
using Clock = std::chrono::steady_clock;
using TimePoint = Clock::time_point;

AppLogHandler() { t0_ = Clock::now(); }
~AppLogHandler() = default;

public:
static AppLogHandler& GetInstance() {
static AppLogHandler instance;
return instance;
}

void Log(LogLevel level, const char* fmt, ...);
void Draw();

private:
LogProcessor processor_;
TimePoint t0_;

std::unordered_map<LogLevel, const char*> level_str_map_ = {
{LogLevel::kDebug, "DEBUG"},
{LogLevel::kInfo, "INFO "},
{LogLevel::kWarn, "WARN "},
{LogLevel::kError, "ERROR"},
{LogLevel::kFatal, "FATAL"}};
};
} // namespace quickviz

#endif // APP_LOG_HANDLER_HPP
File renamed without changes.
37 changes: 37 additions & 0 deletions src/imview/src/component/logging/app_log_handler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* @file app_log_handler.cpp
* @date 12/17/24
* @brief
*
* @copyright Copyright (c) 2024 Ruixiang Du (rdu)
*/

#include "imview/component/logging/app_log_handler.hpp"

#include <string>
#include <sstream>
#include <iomanip>

namespace quickviz {
void AppLogHandler::Log(LogLevel level, const char* fmt, ...) {
auto duration = Clock::now() - t0_;
// Split into seconds and fractional nanoseconds
auto seconds =
std::chrono::duration_cast<std::chrono::seconds>(duration).count();
auto microseconds =
std::chrono::duration_cast<std::chrono::microseconds>(duration).count() %
1'000'000;

// Format into string
std::ostringstream timestamp;
timestamp << std::setfill('0') << std::setw(10) << seconds
<< "." // Fixed-width seconds
<< std::setfill('0') << std::setw(6)
<< microseconds; // Microseconds

processor_.AddLog("[%s] [%s] %s\n", timestamp.str().c_str(),
level_str_map_[level], fmt);
}

void AppLogHandler::Draw() { processor_.Draw("AppLog"); }
} // namespace quickviz
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
* @file log_processor.cpp
* @date 11/3/24
* @brief
* @brief adapted from imgui_demo.cpp
*
* @copyright Copyright (c) 2024 Ruixiang Du (rdu)
*/

#include "component/log_processor.hpp"
#include "imview/component/logging/log_processor.hpp"

namespace quickviz {
LogProcessor::LogProcessor() {
Expand Down

0 comments on commit 7b358e7

Please sign in to comment.