Rapicorn - Experimental UI Toolkit - Source Code 10.08.1
rapicornmsg.hh
Go to the documentation of this file.
00001 /* Rapicorn
00002  * Copyright (C) 2006 Tim Janik
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2.1 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Lesser General Public License for more details.
00013  *
00014  * A copy of the GNU Lesser General Public License should ship along
00015  * with this library; if not, see http://www.gnu.org/copyleft/.
00016  */
00017 #ifndef __RAPICORN_MSG_HH__
00018 #define __RAPICORN_MSG_HH__
00019 
00020 #include <rcore/utilities.hh>
00021 #include <stdarg.h>
00022 
00023 namespace Rapicorn {
00024 
00025 /* --- messaging --- */
00026 struct Msg : protected NonCopyable {
00027   /* message parts */
00028   struct Part;                           /* base for Text* and Check */
00029   struct Text0; typedef Text0 Title;     /* message title */
00030   struct Text1; typedef Text1 Primary;   /* primary message */
00031   struct Text2; typedef Text2 Secondary; /* secondary message (lengthy) */
00032   struct Text3; typedef Text3 Detail;    /* message details */
00033   struct Check;                          /* enable/disable message text */
00034   struct CustomType;
00035   typedef enum {
00036     LOG_TO_STDERR     = 1,
00037     LOG_TO_STDLOG     = 2,
00038     LOG_TO_HANDLER    = 4,
00039     _1FORCE32 = 0xf000000
00040   } LogFlags;
00041   /* message types */
00042   typedef enum {
00043     NONE        = 0,    /* always off */
00044     ALWAYS      = 1,    /* always on */
00045     ERROR,      WARNING,        SCRIPT,
00046     INFO,       DIAG,           DEBUG,
00047     _2FORCE32 = 0xf000000
00048   } Type;
00049   static Type        register_type      (const char         *ident,
00050                                          Type                default_ouput,
00051                                          const char         *label);
00052   static Type        lookup_type        (const String       &ident);
00053   static const char* type_ident         (Type                mtype);
00054   static const char* type_label         (Type                mtype);
00055   static uint32      type_flags         (Type                mtype);
00056   static inline bool check              (Type                mtype);
00057   static void        enable             (Type                mtype);
00058   static void        disable            (Type                mtype);
00059   static void        configure          (Type                mtype,
00060                                          LogFlags            log_mask,
00061                                          const String       &logfile);
00062   static void        allow_msgs         (const String       &key);
00063   static void        deny_msgs          (const String       &key);
00064   static void        configure_stdlog   (bool                redirect_stdlog_to_stderr,
00065                                          const String       &stdlog_filename,
00066                                          uint                syslog_priority);
00067   /* messaging */
00068   static inline void display         (Type                message_type,
00069                                       const Part &p0 = empty_part, const Part &p1 = empty_part,
00070                                       const Part &p2 = empty_part, const Part &p3 = empty_part,
00071                                       const Part &p4 = empty_part, const Part &p5 = empty_part,
00072                                       const Part &p6 = empty_part, const Part &p7 = empty_part,
00073                                       const Part &p8 = empty_part, const Part &p9 = empty_part);
00074   static inline void display         (const CustomType   &message_type,
00075                                       const char         *format,
00076                                       ...) RAPICORN_PRINTF (2, 3);
00077   /* message handling */
00078   struct Part {
00079     String string;
00080     uint8  ptype;
00081   public:
00082     explicit            Part();
00083   protected:
00084     void                setup (uint8       ptype,
00085                                String      smsg);
00086     void                setup (uint8       ptype,
00087                                const char *format,
00088                                va_list     varargs);
00089   };
00090   typedef void (*Handler)            (const char         *domain,
00091                                       Type                mtype,
00092                                       const vector<Part> &parts);
00093   static void    set_thread_handler  (Handler             handler);
00094   static void    default_handler     (const char         *domain,
00095                                       Type                mtype,
00096                                       const vector<Part> &parts);
00097   static void    display_parts       (const char         *domain,
00098                                       Type                message_type,
00099                                       const vector<Part> &parts);
00100 protected:
00101   static const Part   &empty_part;
00102   static void    display_aparts      (const char         *log_domain,
00103                                       Type                message_type,
00104                                       const Part &p0, const Part &p1,
00105                                       const Part &p2, const Part &p3,
00106                                       const Part &p4, const Part &p5,
00107                                       const Part &p6, const Part &p7,
00108                                       const Part &p8, const Part &p9);
00109   static void    display_vmsg        (const char         *log_domain,
00110                                       Type                message_type,
00111                                       const char         *format,
00112                                       va_list             args);
00113 private:
00114   static volatile int    n_msg_types;
00115   static uint8 *volatile msg_type_bits;
00116   static void            init_standard_types ();
00117   static void            key_list_change_L   (const String &keylist,
00118                                               bool          isenabled);
00119   static void            set_msg_type_L      (uint          mtype,
00120                                               uint32        flags,
00121                                               bool          enabled);
00122 public:
00123   struct Text0 : public Part {  /* message title */
00124     explicit RAPICORN_PRINTF (2, 3) Text0 (const char *format, ...) { va_list a; va_start (a, format); setup ('0', format, a); va_end (a); }
00125     explicit                      Text0 (const String &s)         { setup ('0', s); }
00126   };
00127   struct Text1 : public Part {  /* primary message */
00128     explicit RAPICORN_PRINTF (2, 3) Text1 (const char *format, ...) { va_list a; va_start (a, format); setup ('1', format, a); va_end (a); }
00129     explicit                      Text1 (const String &s)         { setup ('1', s); }
00130   };
00131   struct Text2 : public Part {  /* secondary message (lengthy) */
00132     explicit RAPICORN_PRINTF (2, 3) Text2 (const char *format, ...) { va_list a; va_start (a, format); setup ('2', format, a); va_end (a); }
00133     explicit                      Text2 (const String &s)         { setup ('2', s); }
00134   };
00135   struct Text3 : public Part {  /* message details */
00136     explicit RAPICORN_PRINTF (2, 3) Text3 (const char *format, ...) { va_list a; va_start (a, format); setup ('3', format, a); va_end (a); }
00137     explicit                      Text3 (const String &s)         { setup ('3', s); }
00138   };
00139   struct Check : public Part {  /* user switch */
00140     explicit RAPICORN_PRINTF (2, 3) Check (const char *format, ...) { va_list a; va_start (a, format); setup ('c', format, a); va_end (a); }
00141     explicit                      Check (const String &s)         { setup ('c', s); }
00142   };
00143   struct Custom : public Part { /* custom part / user defined */
00144     explicit RAPICORN_PRINTF (3, 4) Custom (uint8 ctype, const char *format, ...) { va_list a; va_start (a, format); setup (ctype | 0x80, format, a); va_end (a); }
00145     explicit                      Custom (uint8 ctype, const String &s)         { setup (ctype | 0x80, s); }
00146   };
00147   struct CustomType : protected NonCopyable {
00148     Type type;
00149     explicit CustomType (const char         *ident,
00150                          Type                default_ouput,
00151                          const char         *label = NULL) :
00152       type (register_type (ident, default_ouput, label))
00153     {}
00154   };
00155 };
00156 
00157 /* --- inline implementations --- */
00158 inline bool
00159 Msg::check (Type mtype)
00160 {
00161   /* this function is supposed to preserve errno */
00162   return (mtype >= 0 &&
00163           mtype < n_msg_types &&
00164           (msg_type_bits[mtype / 8] & (1 << mtype % 8)));
00165 }
00166 
00167 inline void
00168 Msg::display (Type        message_type,
00169               const Part &p0, const Part &p1,
00170               const Part &p2, const Part &p3,
00171               const Part &p4, const Part &p5,
00172               const Part &p6, const Part &p7,
00173               const Part &p8, const Part &p9)
00174 {
00175   /* this function is supposed to preserve errno */
00176   if (check (message_type))
00177     display_aparts (RAPICORN_LOG_DOMAIN, message_type, p0, p1, p2, p3, p4, p5, p6, p7, p8, p9);
00178 }
00179 
00180 inline void
00181 Msg::display (const CustomType   &message_type,
00182               const char         *format,
00183               ...)
00184 {
00185   if (check (message_type.type))
00186     {
00187       va_list args;
00188       va_start (args, format);
00189       display_vmsg (RAPICORN_LOG_DOMAIN, message_type.type, format, args);
00190       va_end (args);
00191     }
00192 }
00193 
00194 } // Rapicorn
00195 
00196 #endif /* __RAPICORN_MSG_HH__ */
00197 /* vim:set ts=8 sts=2 sw=2: */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines