00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "pgapplication.h"
00030 #include "pgwindow.h"
00031 #include "pgmultilineedit.h"
00032 #include "pglog.h"
00033
00034 #include <iostream>
00035 #include <string>
00036 #include <list>
00037 #include <cstring>
00038 #include <cstdarg>
00039 #include <ctime>
00040
00041 Uint32 PG_LogMaxMessages = 200;
00042 int PG_LogMethod = PG_LOGMTH_STDOUT;
00043 static PG_LOG_LEVEL PG_LogLevel = PG_LOG_DBG;
00044 static SDLKey PG_LogConsoleKey = SDLK_F12;
00045
00046 class PG_LogMessage_t {
00047 public:
00048 PG_LOG_LEVEL Id;
00049 time_t TimeStamp;
00050 std::string Text;
00051
00052 PG_LogMessage_t(PG_LOG_LEVEL _id, const char* txt)
00053 : Id(_id), TimeStamp(time(0)) {
00054 Text = txt;
00055 }
00056
00057 }
00058 ;
00059
00060 inline std::ostream& operator<<(std::ostream& os, PG_LogMessage_t *msg) {
00061 os << msg->Text;
00062 return os;
00063 }
00064
00065 static std::list<PG_LogMessage_t*> PG_LogMessages;
00066 static PG_Window* PG_LogWindow = NULL;
00067 static PG_MultiLineEdit* PG_LogWindowData = NULL;
00068 static std::string my_title = "ParaGUI Log Console";
00069
00070 void PG_LogConsole::SetLogLevel(PG_LOG_LEVEL newlevel) {
00071 PG_LogLevel = newlevel;
00072 }
00073
00074 void PG_LogConsole::LogVA(PG_LOG_LEVEL id, const char *Text, va_list ap) {
00075 char buffer[1024];
00076 PG_LogMessage_t* NewMsg;
00077
00078 if(id == PG_LOG_NONE || id > PG_LogLevel) {
00079 return;
00080 }
00081
00082 #ifdef HAVE_VSNPRINTF
00083 vsnprintf(buffer, sizeof(buffer), Text, ap);
00084 #else
00085
00086 vsprintf(buffer, Text, ap);
00087 #endif
00088
00089
00090 NewMsg = new PG_LogMessage_t(id, buffer);
00091
00092 PG_LogMessages.push_front(NewMsg);
00093
00094
00095 while (PG_LogMessages.size() >= PG_LogMaxMessages) {
00096 PG_LogMessage_t *tmp = PG_LogMessages.back();
00097 PG_LogMessages.pop_back();
00098
00099 delete tmp;
00100 }
00101
00102
00103 if (PG_LogMethod & PG_LOGMTH_STDOUT) {
00104 switch (NewMsg->Id) {
00105 case PG_LOG_MSG :
00106 std::cout << "MESSAGE [";
00107 break;
00108
00109 case PG_LOG_ERR :
00110 std::cout << "ERROR [";
00111 break;
00112
00113 case PG_LOG_WRN :
00114 std::cout << "WARNING [";
00115 break;
00116
00117 case PG_LOG_DBG :
00118 std::cout << "DEBUG [";
00119 break;
00120
00121 default :
00122 std::cout << "??? [";
00123 }
00124
00125 strftime(buffer, sizeof(buffer), "%m/%d/%Y %X", localtime(&NewMsg->TimeStamp));
00126 std::cout << buffer << "] > " << NewMsg << std::endl;
00127 }
00128
00129
00130 if (PG_LogMethod & PG_LOGMTH_STDERR) {
00131 switch (NewMsg->Id) {
00132 case PG_LOG_MSG :
00133 std::cerr << "MESSAGE [";
00134 break;
00135
00136 case PG_LOG_ERR :
00137 std::cerr << "ERROR [";
00138 break;
00139
00140 case PG_LOG_WRN :
00141 std::cerr << "WARNING [";
00142 break;
00143
00144 case PG_LOG_DBG :
00145 std::cerr << "DEBUG [";
00146 break;
00147
00148 default :
00149 std::cerr << "??? [";
00150 }
00151
00152 strftime(buffer, sizeof(buffer), "%m/%d/%Y %X", localtime(&NewMsg->TimeStamp));
00153 std::cerr << buffer << "] > " << NewMsg << std::endl;
00154 }
00155
00156 return;
00157 }
00158
00159 void PG_LogConsole::Done() {
00160 std::list<PG_LogMessage_t*>::iterator it = PG_LogMessages.begin();
00161
00162 while (it!=PG_LogMessages.end()) {
00163 delete *it;
00164 PG_LogMessages.erase(it);
00165 it = PG_LogMessages.begin();
00166 }
00167 PG_LogMessages.clear();
00168
00169 if (PG_LogWindow) {
00170 delete PG_LogWindow;
00171 PG_LogWindow = NULL;
00172 }
00173 }
00174
00175 void PG_LogConsole::Update() {
00176 if ((PG_LogMethod & PG_LOGMTH_CONSOLE) == 0) {
00177 return;
00178 }
00179
00180
00181 if (PG_LogWindow == NULL) {
00182 PG_Rect r(25,100,PG_Application::GetScreenWidth()-50,300);
00183 PG_LogWindow = new PG_Window(NULL, r, my_title.c_str(), PG_Window::SHOW_CLOSE, "Window", 25);
00184 PG_LogWindowData = new PG_MultiLineEdit(PG_LogWindow, PG_Rect(1,26,r.w-2,r.h-27));
00185 PG_LogWindowData->SetEditable(false);
00186 }
00187
00188 std::string buffer;
00189 for(std::list<PG_LogMessage_t*>::reverse_iterator it = PG_LogMessages.rbegin();
00190 it != PG_LogMessages.rend(); it++) {
00191 PG_LogMessage_t *Msg = *it;
00192 char timebuf[128];
00193 strftime(timebuf, sizeof(timebuf), "%m/%d/%Y %X", localtime(&Msg->TimeStamp));
00194 buffer += timebuf;
00195
00196 switch (Msg->Id) {
00197 case PG_LOG_MSG :
00198 buffer += " MESSAGE >";
00199 break;
00200
00201 case PG_LOG_ERR :
00202 buffer += " ERROR >";
00203 break;
00204
00205 case PG_LOG_WRN :
00206 buffer += " WARNING >";
00207 break;
00208
00209 case PG_LOG_DBG :
00210 buffer += " DEBUG >";
00211 break;
00212
00213 default :
00214 buffer += " ????? >";
00215 }
00216
00217 buffer += Msg->Text;
00218 buffer += "\n";
00219 }
00220 PG_LogWindowData->SetText(buffer);
00221 }
00222
00223 void PG_LogConsole::SetTitle(const char* title, PG_Label::TextAlign alignment) {
00224 my_title = title;
00225
00226 if (PG_LogWindow) {
00227 PG_LogWindow->SetTitle(title, alignment);
00228 }
00229 }
00230
00231 void PG_LogConsole::Show() {
00232 if(PG_LogWindow) {
00233 PG_LogWindow->Show();
00234 }
00235 }
00236
00237 void PG_LogConsole::Hide() {
00238 if(PG_LogWindow) {
00239 PG_LogWindow->Hide();
00240 }
00241 }
00242
00243 void PG_LogConsole::Toggle() {
00244 if(PG_LogWindow == NULL) {
00245 return;
00246 }
00247
00248 if (PG_LogWindow->IsVisible())
00249 PG_LogWindow->Hide();
00250 else {
00251 PG_LogWindow->Show();
00252 }
00253 }
00254
00255 void PG_LogConsole::SetMethod(int method) {
00256 PG_LogMethod = method;
00257 }
00258
00259 int PG_LogConsole::GetMethod() {
00260 return PG_LogMethod;
00261 }
00262
00263 void PG_LogConsole::SetConsoleKey(SDLKey key) {
00264 PG_LogConsoleKey = key;
00265 }
00266
00267 SDLKey PG_LogConsole::GetConsoleKey() {
00268 return PG_LogConsoleKey;
00269 }
00270
00271 void PG_Log(PG_LOG_LEVEL id, const char *Text, ...) {
00272 va_list ap;
00273 va_start(ap, Text);
00274 PG_LogConsole::LogVA(id, Text, ap);
00275 va_end(ap);
00276 }
00277
00278
00279 void PG_LogMSG(const char *fmt, ...) {
00280 va_list ap;
00281 va_start(ap, fmt);
00282 PG_LogConsole::LogVA(PG_LOG_MSG, fmt, ap);
00283 va_end(ap);
00284 }
00285
00286 void PG_LogERR(const char *fmt, ...) {
00287 va_list ap;
00288 va_start(ap, fmt);
00289 PG_LogConsole::LogVA(PG_LOG_ERR, fmt, ap);
00290 va_end(ap);
00291 }
00292
00293 void PG_LogWRN(const char *fmt, ...) {
00294 va_list ap;
00295 va_start(ap, fmt);
00296 PG_LogConsole::LogVA(PG_LOG_WRN, fmt, ap);
00297 va_end(ap);
00298 }
00299
00300 void PG_LogDBG(const char *fmt, ...) {
00301 va_list ap;
00302 va_start(ap, fmt);
00303 PG_LogConsole::LogVA(PG_LOG_DBG, fmt, ap);
00304 va_end(ap);
00305 }
00306
00307 void PG_LogConsole::SetMaxLogLines(Uint32 max) {
00308 PG_LogMaxMessages = max;
00309 }
00310
00311
00312
00313
00314
00315