Fawkes API  Fawkes Development Version
startpage_processor.cpp
1 
2 /***************************************************************************
3  * startpage_processor.cpp - Web request processor for the start page
4  *
5  * Created: Thu Feb 12 00:10:53 2009
6  * Copyright 2006-2009 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #include "startpage_processor.h"
24 #include <webview/page_reply.h>
25 
26 #include <logging/cache.h>
27 
28 #include <string>
29 #include <cstring>
30 #include <cstdlib>
31 
32 using namespace fawkes;
33 
34 /** @class WebviewStartPageRequestProcessor "startpage_processor.h"
35  * Web request processor for the start page.
36  * @author Tim Niemueller
37  */
38 
39 /** Constructor.
40  * @param cache_logger cache logger
41  */
43 {
44  __cache_logger = cache_logger;
45 }
46 
47 
48 /** Destructor. */
50 {
51 }
52 
53 
54 WebReply *
56 {
57  if ( strncmp("/", request->url().c_str(), 1) == 0 ) {
58 
59  WebPageReply *r = new WebPageReply("Fawkes", "<h1>Welcome to Fawkes.</h1>\n");
60 
61  std::list<CacheLogger::CacheEntry> & messages = __cache_logger->get_messages();
62  std::list<CacheLogger::CacheEntry>::reverse_iterator i;
63 
64  *r += "<h2>Latest log messages</h2>\n";
65  *r += "<table>\n";
66  for (i = messages.rbegin(); i != messages.rend(); ++i) {
68  const char *color = NULL;
69  switch (e.log_level) {
70  case Logger::LL_DEBUG: color = "#888888"; break;
71  case Logger::LL_WARN: color = "orange"; break;
72  case Logger::LL_ERROR: color = "red"; break;
73  default: ;
74  }
75  if (color) {
76  r->append_body("<tr><td>%s</td><td>%s</td><td><span style=\"color:%s\">%s</span></td></tr>\n",
77  e.timestr.c_str(), e.component.c_str(), color, e.message.c_str());
78  } else {
79  r->append_body("<tr><td>%s</td><td>%s</td><td>%s</td></tr>\n",
80  e.timestr.c_str(), e.component.c_str(), e.message.c_str());
81  }
82  }
83  *r += "</table>\n";
84 
85  return r;
86  } else {
87  return NULL;
88  }
89 }
virtual ~WebviewStartPageRequestProcessor()
Destructor.
Fawkes library namespace.
std::string timestr
Time encoded as string.
Definition: cache.h:85
Logging Cache.
Definition: cache.h:40
warning, should be investigated but software still functions, an example is that something was reques...
Definition: logger.h:48
LogLevel log_level
log level
Definition: cache.h:82
virtual fawkes::WebReply * process_request(const fawkes::WebRequest *request)
Process a request.
error, may be recoverable (software still running) or not (software has to terminate).
Definition: logger.h:51
Web request meta data carrier.
Definition: request.h:42
WebviewStartPageRequestProcessor(fawkes::CacheLogger *cache_logger)
Constructor.
debug output, relevant only when tracking down problems
Definition: logger.h:46
std::string message
Message.
Definition: cache.h:86
Basic page reply.
Definition: page_reply.h:36
Basic web reply.
Definition: reply.h:36
Cache entry struct.
Definition: cache.h:81
void append_body(const char *format,...)
Append to body.
Definition: reply.cpp:224
std::string component
component
Definition: cache.h:83
const std::string & url() const
Get URL.
Definition: request.h:69