1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2016 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Affero General Public License as
9 // published by the Free Software Foundation, either version 3 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Affero General Public License for more details.
17 // You should have received a copy of the GNU Affero General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "nel/misc/path.h"
23 #include "nel/misc/command.h"
24 #include "nel/misc/thread.h"
26 #include "nel/misc/window_displayer.h"
36 class CUpdateThread
: public IRunnable
38 CWindowDisplayer
*Disp
;
48 CUpdateThread (CWindowDisplayer
*disp
, string windowNameEx
, bool iconified
, sint x
, sint y
, sint w
, sint h
, sint hs
, sint fs
, const std::string
&fn
, bool ww
, CLog
*log
) :
49 Disp(disp
), WindowNameEx(windowNameEx
), X(x
), Y(y
), W(w
), H(h
), HS(hs
), Iconified(iconified
), FS(fs
), FN(fn
), WW(ww
), Log(log
)
55 Disp
->open (WindowNameEx
, Iconified
, X
, Y
, W
, H
, HS
, FS
, FN
, WW
, Log
);
56 Disp
->display_main ();
60 CWindowDisplayer::~CWindowDisplayer ()
62 // we have to wait the exit of the thread
64 nlassert (_Thread
!= NULL
);
69 bool CWindowDisplayer::update ()
73 CSynchronized
<std::vector
<std::string
> >::CAccessor
access (&_CommandsToExecute
);
74 copy
= access
.value();
75 access
.value().clear ();
78 // execute all commands in the main thread
79 for (uint i
= 0; i
< copy
.size(); i
++)
81 nlassert (Log
!= NULL
);
82 ICommand::execute (copy
[i
], *Log
);
88 uint
CWindowDisplayer::createLabel (const char *value
)
92 CSynchronized
<std::vector
<CLabelEntry
> >::CAccessor
access (&_Labels
);
93 access
.value().push_back (CLabelEntry(value
));
94 pos
= (uint
)access
.value().size()-1;
99 void CWindowDisplayer::setLabel (uint label
, const string
&value
)
102 CSynchronized
<std::vector
<CLabelEntry
> >::CAccessor
access (&_Labels
);
103 nlassert (label
< access
.value().size());
104 if (access
.value()[label
].Value
!= value
)
106 access
.value()[label
].Value
= value
;
107 access
.value()[label
].NeedUpdate
= true;
112 void CWindowDisplayer::create (string windowNameEx
, bool iconified
, sint x
, sint y
, sint w
, sint h
, sint hs
, sint fs
, const std::string
&fn
, bool ww
, CLog
*log
)
114 nlassert (_Thread
== NULL
);
115 _Thread
= IThread::create (new CUpdateThread(this, windowNameEx
, iconified
, x
, y
, w
, h
, hs
, fs
, fn
, ww
, log
));
122 std::string
CWindowDisplayer::stringifyMessage(const NLMISC::CLog::TDisplayInfo
&args
, const char *message
, bool needSlashR
)
124 bool needSpace
= false;
128 if (args
.LogType
!= CLog::LOG_NO
)
130 str
+= CWindowDisplayer::logTypeToString(args
.LogType
);
134 // Write thread identifier
135 if (args
.ThreadId
!= 0)
137 if (needSpace
) { str
+= " "; needSpace
= false; }
139 str
+= NLMISC::toString("%4x", args
.ThreadId
);
141 str
+= NLMISC::toString("%08x", args
.ThreadId
);
146 if (args
.FileName
!= NULL
)
148 if (needSpace
) { str
+= " "; needSpace
= false; }
149 str
+= NLMISC::toString("%20s", CFile::getFilename(args
.FileName
).c_str());
155 if (needSpace
) { str
+= " "; needSpace
= false; }
156 str
+= NLMISC::toString("%4u", args
.Line
);
157 //ss << setw(4) << args.Line;
161 if (args
.FuncName
!= NULL
)
163 if (needSpace
) { str
+= " "; needSpace
= false; }
164 str
+= NLMISC::toString("%20s", args
.FuncName
);
168 if (needSpace
) { str
+= ": "; needSpace
= false; }
172 char *npos
, *pos
= const_cast<char *>(message
);
173 while ((npos
= strchr(pos
, '\n')))
186 pos
= const_cast<char *>(args
.CallstackAndLog
.c_str());
187 while ((npos
= strchr(pos
, '\n')))
203 void CWindowDisplayer::doDisplay (const NLMISC::CLog::TDisplayInfo
&args
, const char *message
)
205 uint32 color
= 0xFF000000;
207 if (args
.LogType
!= CLog::LOG_NO
)
209 if (args
.LogType
== CLog::LOG_ERROR
|| args
.LogType
== CLog::LOG_ASSERT
) color
= 0x00FF0000;
210 else if (args
.LogType
== CLog::LOG_WARNING
) color
= 0x00800000;
211 else if (args
.LogType
== CLog::LOG_DEBUG
) color
= 0x00808080;
215 std::string str
= stringifyMessage(args
, message
, needSlashR
);
218 CSynchronized
<std::list
<std::pair
<uint32
, std::string
> > >::CAccessor
access (&_Buffer
);
219 if (_HistorySize
> 0 && access
.value().size() >= (uint
)_HistorySize
)
221 access
.value().erase (access
.value().begin());
223 access
.value().push_back (make_pair (color
, str
));