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
This commit is contained in:
kohait 2011-03-31 08:32:17 +00:00
parent 6ecf478516
commit 539991f470
8 changed files with 265 additions and 0 deletions

View file

@ -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);
}

89
bazaar/LogCtrl/LogCtrl.h Normal file
View file

@ -0,0 +1,89 @@
#ifndef _LogCtrl_LogCtrl_h
#define _LogCtrl_LogCtrl_h
#include <CtrlLib/CtrlLib.h>
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<LOGLEV>(); }
inline void LogLev(int l) { Single<LOGLEV>() = (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

View file

@ -0,0 +1,7 @@
uses
CtrlLib;
file
LogCtrl.h,
LogCtrl.cpp;

5
bazaar/LogCtrl/init Normal file
View file

@ -0,0 +1,5 @@
#ifndef _LogCtrl_icpp_init_stub
#define _LogCtrl_icpp_init_stub
#include "CtrlLib/init"
#include "EscapeCh/init"
#endif

View file

@ -0,0 +1,27 @@
#ifndef _LogCtrlTest_LogCtrlTest_h
#define _LogCtrlTest_LogCtrlTest_h
#include <CtrlLib/CtrlLib.h>
using namespace Upp;
#include <LogCtrl/LogCtrl.h>
#define LAYOUTFILE <LogCtrlTest/LogCtrlTest.lay>
#include <CtrlCore/lay.h>
class LogCtrlTest : public WithLogCtrlTestLayout<TopWindow> {
public:
typedef LogCtrlTest CLASSNAME;
LogCtrlTest();
void Clear() { log.Clear(); }
void Save() { log.Save(); }
LogCtrl log;
};
#endif

View file

@ -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

View file

@ -0,0 +1,12 @@
uses
CtrlLib,
LogCtrl;
file
LogCtrlTest.h,
main.cpp,
LogCtrlTest.lay;
mainconfig
"" = "GUI MT";

View file

@ -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();
}