Fawkes API  Fawkes Development Version
network_logger.h
00001 
00002 /***************************************************************************
00003  *  network_logger.h - Fawkes network logger
00004  *
00005  *  Created: Sat Dec 15 00:45:54 2007 (after I5 xmas party)
00006  *  Copyright  2006-2007  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00022  */
00023 
00024 #ifndef __NETCOMM_UTILS_NETWORK_LOGGER_H_
00025 #define __NETCOMM_UTILS_NETWORK_LOGGER_H_
00026 
00027 #include <core/utils/lock_list.h>
00028 #include <core/utils/lock_queue.h>
00029 #include <utils/logging/logger.h>
00030 #include <netcomm/fawkes/handler.h>
00031 #include <netcomm/fawkes/message_content.h>
00032 
00033 #include <stdint.h>
00034 
00035 namespace fawkes {
00036 
00037 class Mutex;
00038 class FawkesNetworkHub;
00039 
00040 class NetworkLogger
00041 : public Logger,
00042   public FawkesNetworkHandler
00043 {
00044  public:
00045   NetworkLogger(FawkesNetworkHub *hub, LogLevel log_level = LL_DEBUG);
00046   virtual ~NetworkLogger();
00047 
00048   virtual void log_debug(const char *component, const char *format, ...);
00049   virtual void log_info(const char *component, const char *format, ...);
00050   virtual void log_warn(const char *component, const char *format, ...);
00051   virtual void log_error(const char *component, const char *format, ...);
00052 
00053   virtual void log_debug(const char *component, Exception &e);
00054   virtual void log_info(const char *component, Exception &e);
00055   virtual void log_warn(const char *component, Exception &e);
00056   virtual void log_error(const char *component, Exception &e);
00057 
00058   virtual void vlog_debug(const char *component, const char *format, va_list va);
00059   virtual void vlog_info(const char *component, const char *format, va_list va);
00060   virtual void vlog_warn(const char *component, const char *format, va_list va);
00061   virtual void vlog_error(const char *component, const char *format, va_list va);
00062 
00063   virtual void tlog_debug(struct timeval *t, const char *component, const char *format, ...);
00064   virtual void tlog_info(struct timeval *t, const char *component, const char *format, ...);
00065   virtual void tlog_warn(struct timeval *t, const char *component, const char *format, ...);
00066   virtual void tlog_error(struct timeval *t, const char *component, const char *format, ...);
00067 
00068   virtual void tlog_debug(struct timeval *t, const char *component, Exception &e);
00069   virtual void tlog_info(struct timeval *t, const char *component, Exception &e);
00070   virtual void tlog_warn(struct timeval *t, const char *component, Exception &e);
00071   virtual void tlog_error(struct timeval *t, const char *component, Exception &e);
00072 
00073   virtual void vtlog_debug(struct timeval *t, const char *component,
00074                            const char *format, va_list va);
00075   virtual void vtlog_info(struct timeval *t, const char *component,
00076                           const char *format, va_list va);
00077   virtual void vtlog_warn(struct timeval *t, const char *component,
00078                           const char *format, va_list va);
00079   virtual void vtlog_error(struct timeval *t, const char *component,
00080                            const char *format, va_list va);
00081 
00082   virtual void handle_network_message(FawkesNetworkMessage *msg);
00083   virtual void client_connected(unsigned int clid);
00084   virtual void client_disconnected(unsigned int clid);
00085 
00086   /** NetworkLogger message types. */
00087   typedef enum {
00088     MSGTYPE_SUBSCRIBE   = 1,    /**< Subscribe for logging messages. */
00089     MSGTYPE_UNSUBSCRIBE = 2,    /**< Unsubscribe from receiving logging messages. */
00090     MSGTYPE_LOGMESSAGE  = 3     /**< Log message. */
00091   } network_logger_msgtype_t;
00092 
00093 #pragma pack(push,4)
00094   /** Network logging message header. */
00095   typedef struct {
00096     uint32_t        log_level :  4;     /**< LogLevel, @see Logger::LogLevel */
00097     uint32_t        exception :  1;     /**< 1 if message was generated by exception, 0 otherwise */
00098     uint32_t        reserved  : 27;     /**< reserved for future use */
00099     uint64_t        time_sec;           /**< time in seconds since the epoch, encoded in network byte order */
00100     uint32_t        time_usec;          /**< addition to time in usec, encoded in network byte order */
00101   } network_logger_header_t;
00102 #pragma pack(pop)
00103 
00104  private:
00105   void send_message(Logger::LogLevel level, struct timeval *t,
00106                     const char *component, bool is_exception,
00107                     const char *format, va_list va);
00108   void send_message(Logger::LogLevel level, struct timeval *t,
00109                     const char *component, bool is_exception, const char *message);
00110 
00111   FawkesNetworkHub *hub;
00112 
00113   LockQueue< FawkesNetworkMessage * > inbound_queue;
00114 
00115   LockList<unsigned int>           __subscribers;
00116   LockList<unsigned int>::iterator __ssit;
00117 };
00118 
00119 
00120 class NetworkLoggerMessageContent : public FawkesNetworkMessageContent
00121 {
00122  public:
00123   NetworkLoggerMessageContent(Logger::LogLevel log_level, struct timeval *t,
00124                               const char *component, bool is_exception,
00125                               const char *message);
00126   NetworkLoggerMessageContent(Logger::LogLevel log_level, struct timeval *t,
00127                               const char *component, bool is_exception,
00128                               const char *format, va_list va);
00129   NetworkLoggerMessageContent(const NetworkLoggerMessageContent *content);
00130   NetworkLoggerMessageContent(unsigned int component_id, unsigned int msg_id,
00131                               void *payload, size_t payload_size);
00132   virtual ~NetworkLoggerMessageContent();
00133 
00134   struct timeval    get_time() const;
00135   Logger::LogLevel  get_loglevel() const;
00136   const char *      get_component() const;
00137   const char *      get_message() const;
00138   bool              is_exception() const;
00139 
00140   virtual void   serialize();
00141 
00142  private:
00143   NetworkLogger::network_logger_header_t *header;
00144   const char *__component;
00145   const char *__message;
00146   bool __own_payload;
00147 };
00148 
00149 } // end namespace fawkes
00150 
00151 #endif