From 539991f4702e8c074dc3c37e84dfeaac6f5e709f Mon Sep 17 00:00:00 2001 From: kohait Date: Thu, 31 Mar 2011 08:32:17 +0000 Subject: [PATCH] bazaar: LogCtrl: redirect entire Logging facility of Upp to a Ctrl git-svn-id: svn://ultimatepp.org/upp/trunk@3320 f0d560ea-af0d-0410-9eb7-867de7ffcac7 --- bazaar/LogCtrl/LogCtrl.cpp | 74 +++++++++++++++++++++++++ bazaar/LogCtrl/LogCtrl.h | 89 ++++++++++++++++++++++++++++++ bazaar/LogCtrl/LogCtrl.upp | 7 +++ bazaar/LogCtrl/init | 5 ++ bazaar/LogCtrlTest/LogCtrlTest.h | 27 +++++++++ bazaar/LogCtrlTest/LogCtrlTest.lay | 5 ++ bazaar/LogCtrlTest/LogCtrlTest.upp | 12 ++++ bazaar/LogCtrlTest/main.cpp | 46 +++++++++++++++ 8 files changed, 265 insertions(+) create mode 100644 bazaar/LogCtrl/LogCtrl.cpp create mode 100644 bazaar/LogCtrl/LogCtrl.h create mode 100644 bazaar/LogCtrl/LogCtrl.upp create mode 100644 bazaar/LogCtrl/init create mode 100644 bazaar/LogCtrlTest/LogCtrlTest.h create mode 100644 bazaar/LogCtrlTest/LogCtrlTest.lay create mode 100644 bazaar/LogCtrlTest/LogCtrlTest.upp create mode 100644 bazaar/LogCtrlTest/main.cpp diff --git a/bazaar/LogCtrl/LogCtrl.cpp b/bazaar/LogCtrl/LogCtrl.cpp new file mode 100644 index 000000000..452663549 --- /dev/null +++ b/bazaar/LogCtrl/LogCtrl.cpp @@ -0,0 +1,74 @@ +#include "LogCtrl.h" + +LoggerCtrl::LoggerCtrl() : maxlines(1000), ignore(false) +{ + NoInitFocus(); + NoWantFocus(); + SetReadOnly(); + //Reserve(maxlines*64); +} + +void LoggerCtrl::Save() +{ + FileSel fs; + fs.Type("Text file", "*.txt"); + if(!fs.ExecuteSaveAs("Save Log to file")) return; + String fn = fs.Get(); + if(fn.IsEmpty()) return; + FileOut out(fn); + Flush(); + DocEdit::Save(out); +} + +void LoggerCtrl::_Put(int w) +{ + if(ignore) return; + String d; + d.Cat(w); + //String s = EscapeCh((unsigned)w); + GuiLock _; + Insert(GetLength(), d); + SetCursor(GetLength()); +} + +void LoggerCtrl::_Put(const void *data, dword size) +{ + if(ignore) return; + String d; + d.Cat((const char*)data, size); + //String s = EscapeCh(d); + GuiLock _; + Insert(GetLength(), d); + SetCursor(GetLength()); +} + +LogCtrl::LogCtrl() : ps(NULL) +{ + +} + +void LogCtrl::Log(bool b) +{ + if(ps == this) return; + if(b) + { + ps = &VppLog(); + SetVppLog(*this); + } + else if(ps) + { + SetVppLog(*ps); + ps = NULL; + } +} + +void LogCtrl::Reset() +{ + SetVppLog(StdLog()); + ps = NULL; +} + +INITBLOCK +{ + LogLev(LDBG); +} diff --git a/bazaar/LogCtrl/LogCtrl.h b/bazaar/LogCtrl/LogCtrl.h new file mode 100644 index 000000000..db3a06896 --- /dev/null +++ b/bazaar/LogCtrl/LogCtrl.h @@ -0,0 +1,89 @@ +#ifndef _LogCtrl_LogCtrl_h +#define _LogCtrl_LogCtrl_h + +#include +using namespace Upp; + +enum LOGLEV +{ + LNONE = 0, + LMUTE = LNONE, + + LEMRG = 1, + LALRT = 2, + LCRIT = 3, + LERR = 4, + LWARN = 5, + LNOTE = 6, + LINFO = LNOTE, + LDBG = 7, +}; + +inline int LogLev() { return Single(); } +inline void LogLev(int l) { Single() = (LOGLEV)l;} + +#define ONLL(l) if(LogLev()>=l) +#define LL(l,x) ONLL(l) RLOG(x) + +#define EMRG(x) LL(LEMRG, x) +#define ALRT(x) LL(LALRT, x) +#define CRIT(x) LL(LCRIT, x) +#define ERR(x) LL(LERR, x) +#define WARN(x) LL(LWARN, x) +#define NOTE(x) LL(LNOTE, x) +#define INFO(x) LL(LINFO, x) +#define DBG(x) LL(LDBG, x) + +#define L1(x) LL(1, x) +#define L2(x) LL(2, x) +#define L3(x) LL(3, x) +#define L4(x) LL(4, x) +#define L5(x) LL(5, x) +#define L6(x) LL(6, x) +#define L7(x) LL(7, x) + +/// + +class LoggerCtrl : public DocEdit, public Stream +{ +public: + typedef LoggerCtrl CLASSNAME; + LoggerCtrl(); + + void Save(); + void Ignore(bool b = true) { ignore = b; } + bool IsIgnore() const { return ignore; } + + void MaxLines(int c = 1000) { maxlines = c; Update(); } + int GetMaxLines() const { return maxlines; } + void Updated() { if(GetLineCount() > maxlines) RemoveLines(0, maxlines>>1); } + + virtual bool IsOpen() const { return true; } + +protected: + virtual void _Put(int w); + virtual void _Put(const void *data, dword size); + + int maxlines; + bool ignore; +#if 0 + DocEdit::Style st; +#endif +}; + +class LogCtrl : public LoggerCtrl +{ +public: + typedef LogCtrl CLASSNAME; + LogCtrl(); + + void Log(bool b = true); + bool IsLogging() const { return ps == this && !IsIgnore(); } + void Reset(); + +protected: + Stream* ps; +}; + +#endif + diff --git a/bazaar/LogCtrl/LogCtrl.upp b/bazaar/LogCtrl/LogCtrl.upp new file mode 100644 index 000000000..1b7fbd285 --- /dev/null +++ b/bazaar/LogCtrl/LogCtrl.upp @@ -0,0 +1,7 @@ +uses + CtrlLib; + +file + LogCtrl.h, + LogCtrl.cpp; + diff --git a/bazaar/LogCtrl/init b/bazaar/LogCtrl/init new file mode 100644 index 000000000..82514a5e4 --- /dev/null +++ b/bazaar/LogCtrl/init @@ -0,0 +1,5 @@ +#ifndef _LogCtrl_icpp_init_stub +#define _LogCtrl_icpp_init_stub +#include "CtrlLib/init" +#include "EscapeCh/init" +#endif diff --git a/bazaar/LogCtrlTest/LogCtrlTest.h b/bazaar/LogCtrlTest/LogCtrlTest.h new file mode 100644 index 000000000..a34845f15 --- /dev/null +++ b/bazaar/LogCtrlTest/LogCtrlTest.h @@ -0,0 +1,27 @@ +#ifndef _LogCtrlTest_LogCtrlTest_h +#define _LogCtrlTest_LogCtrlTest_h + +#include + +using namespace Upp; + +#include + +#define LAYOUTFILE +#include + + + +class LogCtrlTest : public WithLogCtrlTestLayout { +public: + typedef LogCtrlTest CLASSNAME; + LogCtrlTest(); + + void Clear() { log.Clear(); } + void Save() { log.Save(); } + + LogCtrl log; +}; + +#endif + diff --git a/bazaar/LogCtrlTest/LogCtrlTest.lay b/bazaar/LogCtrlTest/LogCtrlTest.lay new file mode 100644 index 000000000..83301372a --- /dev/null +++ b/bazaar/LogCtrlTest/LogCtrlTest.lay @@ -0,0 +1,5 @@ +LAYOUT(LogCtrlTestLayout, 400, 200) + ITEM(Button, clear, SetLabel(t_("Clear")).LeftPosZ(0, 56).BottomPosZ(0, 20)) + ITEM(Button, save, SetLabel(t_("Save")).LeftPosZ(56, 56).BottomPosZ(0, 20)) +END_LAYOUT + diff --git a/bazaar/LogCtrlTest/LogCtrlTest.upp b/bazaar/LogCtrlTest/LogCtrlTest.upp new file mode 100644 index 000000000..6169bcf85 --- /dev/null +++ b/bazaar/LogCtrlTest/LogCtrlTest.upp @@ -0,0 +1,12 @@ +uses + CtrlLib, + LogCtrl; + +file + LogCtrlTest.h, + main.cpp, + LogCtrlTest.lay; + +mainconfig + "" = "GUI MT"; + diff --git a/bazaar/LogCtrlTest/main.cpp b/bazaar/LogCtrlTest/main.cpp new file mode 100644 index 000000000..7a691618f --- /dev/null +++ b/bazaar/LogCtrlTest/main.cpp @@ -0,0 +1,46 @@ +#include "LogCtrlTest.h" + +LogCtrlTest::LogCtrlTest() +{ + CtrlLayout(*this, "Window title"); + + Add(log.HSizePos().VSizePos(0,20)); + clear <<= THISBACK(Clear); + save <<= THISBACK(Save); + + //SetTimeCallback(-500, callback(&log, &LogCtrl::Flush)); + + log.Log(); + LOG("This text comes from the LOG macro"); + + log.Log(false); + LOG("This should go to the previous Log stream, whatever it is"); + + log.Reset(); + LOG("And this should go to the StdLog in any case"); + + /// + log.Log(); + RLOG("Using the log levels:"); + + ERR("Error message"); + INFO("Hallo, info"); + WARN("A Warning"); + + LogLev(LERR); + + ERR("Another Error Message"); + INFO("Another info"); + WARN("Another Warning"); + + ONLL(1) + { + int a = 0; //do log level dependant stuff + } +} + +GUI_APP_MAIN +{ + LogCtrlTest().Run(); +} +