Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
sdottaka committed Feb 25, 2025
1 parent e673ce6 commit 77a8fd6
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 7 deletions.
26 changes: 24 additions & 2 deletions Src/Logger.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,37 @@
#include "pch.h"
#include "Logger.h"
#include "unicoder.h"
#include <chrono>

void Logger::Log(LogLevel level, const String& msg)
{
if (m_func)
m_func(level, msg);
m_func(level, std::chrono::system_clock::now(), msg);
}

void Logger::Log(LogLevel level, const std::string& msg)
{
if (m_func)
m_func(level, ucr::toTString(msg));
m_func(level, std::chrono::system_clock::now(), ucr::toTString(msg));
}


String LogMessage::format() const
{
auto timeT = std::chrono::system_clock::to_time_t(tp);
auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(tp.time_since_epoch()) % 1000;

std::tm tm;
localtime_s(&tm, &timeT);

std::basic_ostringstream<tchar_t> oss;
oss << std::put_time(&tm, _T("%Y-%m-%dT%H:%M:%S"))
<< '.' << std::setw(3) << std::setfill(_T('0')) << millis.count();

const tchar_t* levelStr = (level == Logger::LogLevel::ERR) ? _T("ERROR") :
(level == Logger::LogLevel::WARN) ? _T("WARN") : _T("INFO");

oss << _T(" [") << levelStr << _T("] ") << text;

return oss.str();
}
22 changes: 20 additions & 2 deletions Src/Logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

#include "UnicodeString.h"
#include <functional>
#include <chrono>
#include <iomanip>
#include <sstream>
#include <ctime>

class Logger
{
Expand All @@ -14,11 +18,12 @@ class Logger
template<class T> void Error(const T& msg) { if (m_level >= LogLevel::ERR) Log(LogLevel::ERR, msg); }
void Log(LogLevel level, const String& msg);
void Log(LogLevel level, const std::string& msg);
void SetOutputFunction(std::function<void(LogLevel level, const String& msg)> func) { m_func = func; }
void SetLogLevel(LogLevel level) { m_level = level; }
void SetOutputFunction(std::function<void(LogLevel level, const std::chrono::system_clock::time_point& tp, const String& msg)> func) { m_func = func; }

private:
LogLevel m_level;
std::function<void(LogLevel level, const String& msg)> m_func;
std::function<void(LogLevel level, const std::chrono::system_clock::time_point& tp, const String& msg)> m_func;
};

namespace RootLogger
Expand All @@ -27,3 +32,16 @@ namespace RootLogger
template<class T> static void Warn(const T& msg) { Logger::Get().Warn(msg); }
template<class T> static void Error(const T& msg) { Logger::Get().Error(msg); }
}

struct LogMessage
{
LogMessage(Logger::LogLevel level, const std::chrono::system_clock::time_point& tp, const String& text)
: level(level), tp(tp), text(text) { }
LogMessage(LogMessage&& other) noexcept
: level(other.level), tp(other.tp), text(std::move(other.text)) { }
String format() const;
Logger::LogLevel level;
std::chrono::system_clock::time_point tp;
String text;
};

42 changes: 40 additions & 2 deletions Src/MainFrm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
ON_WM_DESTROY()
ON_MESSAGE(WM_COPYDATA, OnCopyData)
ON_MESSAGE(WM_USER+1, OnUser1)
ON_MESSAGE(WM_USER+2, OnUser2)
ON_WM_ACTIVATEAPP()
ON_WM_NCCALCSIZE()
ON_WM_SIZE()
Expand Down Expand Up @@ -381,6 +382,7 @@ CMainFrame::CMainFrame()
, m_lfDiff(Options::Font::Load(GetOptionsMgr(), OPT_FONT_FILECMP))
, m_lfDir(Options::Font::Load(GetOptionsMgr(), OPT_FONT_DIRCMP))
, m_pDirWatcher(new DirWatcher())
, m_pOutputDoc(nullptr)
{
}

Expand Down Expand Up @@ -422,6 +424,15 @@ int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
if (__super::OnCreate(lpCreateStruct) == -1)
return -1;

Logger::Get().SetOutputFunction([this](Logger::LogLevel level, const std::chrono::system_clock::time_point& tp, const String& msg)
{
LogMessage* p = new LogMessage(level, tp, msg);
PostMessage(WM_USER + 2, reinterpret_cast<WPARAM>(p), 0);
});

RootLogger::Error(_T("test"));
RootLogger::Error(_T("test2"));

m_wndMDIClient.SubclassWindow(m_hWndMDIClient);

if (IsWin10_OrGreater())
Expand Down Expand Up @@ -2393,6 +2404,28 @@ LRESULT CMainFrame::OnUser1(WPARAM wParam, LPARAM lParam)
return 0;
}

LRESULT CMainFrame::OnUser2(WPARAM wParam, LPARAM lParam)
{
LogMessage* pLogMessage = reinterpret_cast<LogMessage*>(wParam);
if (pLogMessage->level >= Logger::LogLevel::ERR && m_wndOutputBar.m_hWnd == nullptr)
ShowOutputPane(true);

if (!m_pOutputDoc)
{
m_pOutputDoc = static_cast<COutputDoc*>(theApp.GetOutputTemplate()->CreateNewDocument());
m_pOutputDoc->m_xTextBuffer.InitNew();
}
String text = pLogMessage->format();
CCrystalTextBuffer& buf = m_pOutputDoc->m_xTextBuffer;
int nEndLine, nEndChar;
POSITION pos = m_pOutputDoc->GetFirstViewPosition();
CCrystalTextView* pTextView = static_cast<CCrystalTextView*>(m_pOutputDoc->GetNextView(pos));
buf.InsertText(pTextView, buf.GetLineCount() - 1, 0, text.c_str(), text.length(), nEndLine, nEndChar, 0, false);

delete pLogMessage;
return 0;
}

/**
* @brief Close all open windows.
*
Expand Down Expand Up @@ -3777,10 +3810,15 @@ void CMainFrame::ShowOutputPane(bool bShow)
return;
}
COutputView* pOutputView = new COutputView;
CDocument* pOutputDoc = theApp.GetOutputTemplate()->CreateNewDocument();
if (!m_pOutputDoc)
{
m_pOutputDoc = static_cast<COutputDoc*>(theApp.GetOutputTemplate()->CreateNewDocument());
m_pOutputDoc->m_xTextBuffer.InitNew();
}
const DWORD dwStyle = AFX_WS_DEFAULT_VIEW & ~WS_BORDER;
pOutputView->Create(nullptr, nullptr, dwStyle, CRect(0, 0, 100, 40), &m_wndOutputBar, 200, nullptr);
pOutputDoc->AddView(pOutputView);
m_pOutputDoc->AddView(pOutputView);
pOutputView->AttachToBuffer();

m_wndOutputBar.SetBarStyle(m_wndOutputBar.GetBarStyle() | CBRS_SIZE_DYNAMIC | CBRS_ALIGN_BOTTOM);
m_wndOutputBar.EnableDocking(CBRS_ALIGN_TOP | CBRS_ALIGN_BOTTOM | CBRS_ALIGN_LEFT | CBRS_ALIGN_RIGHT);
Expand Down
3 changes: 3 additions & 0 deletions Src/MainFrm.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class CMainFrame;
class CImgMergeFrame;
class CWebPageDiffFrame;
class DirWatcher;
class COutputDoc;

typedef std::shared_ptr<TempFile> TempFilePtr;

Expand Down Expand Up @@ -345,6 +346,7 @@ class CMainFrame : public CMDIFrameWnd
DropHandler *m_pDropHandler;
std::unique_ptr<DirWatcher> m_pDirWatcher;
std::optional<bool> m_bTabsOnTitleBar;
COutputDoc* m_pOutputDoc;

// Generated message map functions
protected:
Expand Down Expand Up @@ -382,6 +384,7 @@ class CMainFrame : public CMDIFrameWnd
afx_msg void OnFileOpenProject();
afx_msg LRESULT OnCopyData(WPARAM wParam, LPARAM lParam);
afx_msg LRESULT OnUser1(WPARAM wParam, LPARAM lParam);
afx_msg LRESULT OnUser2(WPARAM wParam, LPARAM lParam);
afx_msg void OnWindowCloseAll();
afx_msg void OnUpdateWindowCloseAll(CCmdUI* pCmdUI);
afx_msg void OnSaveProject();
Expand Down
2 changes: 1 addition & 1 deletion Src/OutputView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ COutputDoc* COutputView::GetDocument() // non-debug version is inline

CCrystalTextBuffer* COutputView::LocateTextBuffer()
{
return nullptr;
return &GetDocument()->m_xTextBuffer;
}

void COutputView::OnInitialUpdate()
Expand Down
1 change: 1 addition & 0 deletions Src/StdAfx.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include <functional>
#include <cassert>
#include <ctime>
#include <chrono>
#include <boost/flyweight.hpp>

/**
Expand Down
1 change: 1 addition & 0 deletions Src/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
#include <ctime>
#include <cctype>
#include <array>
#include <chrono>
#include <boost/flyweight.hpp>

0 comments on commit 77a8fd6

Please sign in to comment.