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  const char *method,
57  const char *version,
58  const char *upload_data,
59  size_t *upload_data_size,
60  void **session_data)
61 {
62  if ( strncmp("/", url, 1) == 0 ) {
63 
64  WebPageReply *r = new WebPageReply("Fawkes", "<h1>Welcome to Fawkes.</h1>\n");
65 
66  std::list<CacheLogger::CacheEntry> & messages = __cache_logger->get_messages();
67  std::list<CacheLogger::CacheEntry>::reverse_iterator i;
68 
69  *r += "<h2>Latest log messages</h2>\n";
70  *r += "<table>\n";
71  for (i = messages.rbegin(); i != messages.rend(); ++i) {
73  const char *color = NULL;
74  switch (e.log_level) {
75  case Logger::LL_DEBUG: color = "#888888"; break;
76  case Logger::LL_WARN: color = "orange"; break;
77  case Logger::LL_ERROR: color = "red"; break;
78  default: ;
79  }
80  if (color) {
81  r->append_body("<tr><td>%s</td><td>%s</td><td><span style=\"color:%s\">%s</span></td></tr>\n",
82  e.timestr.c_str(), e.component.c_str(), color, e.message.c_str());
83  } else {
84  r->append_body("<tr><td>%s</td><td>%s</td><td>%s</td></tr>\n",
85  e.timestr.c_str(), e.component.c_str(), e.message.c_str());
86  }
87  }
88  *r += "</table>\n";
89 
90  return r;
91  } else {
92  return NULL;
93  }
94 }
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
error, may be recoverable (software still running) or not (software has to terminate).
Definition: logger.h:51
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:34
Cache entry struct.
Definition: cache.h:81
void append_body(const char *format,...)
Append to body.
Definition: reply.cpp:179
std::string component
component
Definition: cache.h:83
virtual fawkes::WebReply * process_request(const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **session_data)
Process a request.