00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00160 #ifndef _QMMS_LOG_HH
00161 #define _QMMS_LOG_HH
00162
00163 #include <iostream>
00164 #include <cstdlib>
00165 #include <fstream>
00166 #include <typeinfo>
00167 #include <syslog.h>
00168
00169 #include <qmutex.h>
00170 #include <qmap.h>
00171 #include <qstring.h>
00172 #include <qdict.h>
00173 #include <qptrlist.h>
00174 #include <qdatetime.h>
00175 #include <qregexp.h>
00176 #include <qstringlist.h>
00177 #include <qfile.h>
00178 #include <qdir.h>
00179 #include <qfileinfo.h>
00180 #include <qtextedit.h>
00181
00182 using namespace std;
00183
00184 namespace Qmms {
00185 namespace Log {
00186
00187
00188 class LogLog;
00189 class BaseCategory;
00190 class AppenderSkeleton;
00191 class Hierarchy;
00192 class Category;
00193
00195 typedef QMap< QString, int > StrIntMap;
00196
00198 typedef QMap< int, QString > IntStrMap;
00199
00202 typedef QDict< BaseCategory > BaseDict;
00203
00205 typedef QDict< AppenderSkeleton > AppDict;
00206
00209 typedef QMap< QString, QStringList > StrListMap;
00210
00211
00223 class LogLog
00224 {
00225 private:
00227 LogLog();
00228
00233 static QString DEBUG_KEY;
00234
00236 static QString PREFIX;
00237
00239 static QString ERR_PREFIX;
00240
00242 static QString WARN_PREFIX;
00243
00244 protected:
00246 static bool debugEnabled;
00247
00249 static bool quietMode;
00250
00251 public:
00253 virtual ~LogLog();
00254
00259 static void init( bool enabled, bool quiet );
00260
00264 static void setInternalDebugging( bool enabled );
00265
00270 static bool isEnabled();
00271
00275 static bool isQuiet();
00276
00282 static void setQuietMode( bool mode );
00283
00288 static void debug( QString msg );
00289
00294 static void warn( QString msg );
00295
00300 static void error( QString msg );
00301
00305 virtual void dummy() const = 0;
00306 };
00307
00308
00309
00334 class LogLevel
00335 {
00336 private:
00340 static StrIntMap level;
00341
00344 static IntStrMap revLevel;
00345
00349 LogLevel();
00350
00354 static void init();
00355
00356 public:
00357
00359 friend class Hierarchy;
00360
00365 static void addLevel( QString key, const int val );
00366
00370 static void showLevels();
00371
00379 static bool isGreaterOrEqual( const QString p, const QString q );
00380
00386 static bool isLevel( const QString key );
00387
00392 static int getValue( const QString key );
00393
00398 static QString getKey( const int value );
00399
00401 virtual ~LogLevel();
00402
00406 virtual void dummy() const = 0;
00407 };
00408
00413 class LoggingEvent
00414 {
00415 private:
00417 Category* category;
00418
00420 QString level;
00421
00423 QString message;
00424
00426 QString fqnOfCategoryClass;
00427
00429 QString categoryName;
00430
00432 QDateTime timeStamp;
00433
00434 public:
00436 LoggingEvent();
00437
00445 LoggingEvent( const QString fqn, Category* cat,
00446 const QString lvl, const QString msg );
00447
00449 ~LoggingEvent();
00450
00453 QString getMessage() const;
00454
00457 QString getFQN() const;
00458
00461 Category* getCategory() const;
00462
00465 QString getCategoryName() const;
00466
00469 QString getLevel() const;
00470
00473 QDateTime getTimestamp() const;
00474 };
00475
00483 class Layout
00484 {
00485 public:
00490 virtual QString formatEvent( const LoggingEvent* evnt ) = 0;
00491
00493 virtual ~Layout() {};
00494 };
00495
00517 class PatternLayout : public Layout
00518 {
00519 private:
00520
00522 QString pattern;
00523
00525 const static QString DEFAULT_PATTERN;
00526
00528 const static QString DEFAULT_DATETIME_FORMAT;
00529
00531 QString dtFormat;
00532
00537 bool isValidPattern( const QString ptn );
00538
00539 public:
00542 PatternLayout();
00543
00548 PatternLayout( const QString& ptn );
00549
00551 ~PatternLayout();
00552
00558 QString formatEvent( const LoggingEvent* evnt );
00559
00565 bool changePattern( const QString ptn );
00566
00570 QString getPattern() const;
00571
00577 QString getDate( const LoggingEvent* evnt ) const;
00578
00585 bool changeDateTimeFormat( const QString format );
00586 };
00587
00594 class SimpleLayout : public Layout
00595 {
00596 private:
00598 const static QString DEFAULT_DATETIME_FORMAT;
00599 const static QString OPEN_PAREN;
00600 const static QString CLOSE_PAREN;
00601 const static QString HYPHEN;
00602 const static QString COLON_SPACE;
00603
00604 public:
00606 SimpleLayout();
00607
00609 ~SimpleLayout();
00610
00615 QString formatEvent( const LoggingEvent* evnt );
00616 };
00617
00621 class HTMLLayout : public Layout
00622 {
00623
00624 };
00625
00633 class Appender
00634 {
00635 public:
00637 Appender();
00638
00640 virtual ~Appender();
00641
00643 virtual void close() = 0;
00644
00646 virtual void doAppend( LoggingEvent* evnt) = 0;
00647
00649 virtual QString getName() const = 0;
00650
00652 virtual void setName( const QString& name ) = 0;
00653
00654 };
00655
00661 class AppenderSkeleton : public Appender
00662 {
00663 protected:
00665 QString name;
00666
00668 QString threshold;
00669
00670 public:
00672 AppenderSkeleton();
00673
00677 AppenderSkeleton( const QString& name );
00678
00680 virtual ~AppenderSkeleton();
00681
00691 virtual void append( LoggingEvent* evnt ) = 0;
00692
00697 virtual bool checkEntryConditions() const = 0;
00698
00703 void doAppend( LoggingEvent* evnt );
00704
00709 void setName( const QString& name );
00710
00714 QString getName() const;
00715
00720 void setThreshold( const QString& thold );
00721
00725 QString getThreshold() const;
00726
00732 bool isAsSevereAsThreshold( const QString lvl ) const;
00733
00737 virtual bool requiresLayout() const = 0;
00738 };
00739
00743 class LayoutAppender : public AppenderSkeleton
00744 {
00745 protected:
00747 Layout* layout;
00748
00749 public:
00751 LayoutAppender();
00752
00757 LayoutAppender( const QString& name );
00758
00763 LayoutAppender( const QString& name, Layout* lyt );
00764
00766 virtual ~LayoutAppender();
00767
00771 void setLayout( Layout* lyt );
00772
00776 Layout* getLayout();
00777
00782 bool requiresLayout() const;
00783
00788 virtual bool checkEntryConditions() const;
00789
00794 virtual void append( LoggingEvent* evnt );
00795
00800 virtual void subAppend( LoggingEvent* evnt ) = 0;
00801 };
00802
00808 class TextEditAppender : public LayoutAppender, public QTextEdit
00809 {
00810 public:
00812 TextEditAppender();
00813
00818 TextEditAppender( QWidget* parent = 0, const QString& name = 0 );
00819
00821 virtual ~TextEditAppender();
00822
00835 virtual void subAppend( LoggingEvent* evnt );
00836
00839 void close();
00840 };
00841
00847 class StreamAppender : public LayoutAppender
00848 {
00849 protected:
00855 bool immediateFlush;
00856
00860 QFile out;
00861
00862 public:
00864 StreamAppender();
00865
00869 StreamAppender( const QString& name );
00870
00875 StreamAppender( const QString& name, Layout* lyt );
00876
00878 virtual ~StreamAppender();
00879
00892 virtual void subAppend( LoggingEvent* evnt );
00893
00898 virtual bool checkEntryConditions() const;
00899
00904 virtual void open( QString& name ) = 0;
00905
00908 virtual void close();
00909
00913 bool requiresLayout() const;
00914
00916 bool getImmediateFlush() const;
00917
00919 void setImmediateFlush( const bool flush );
00920 };
00921
00928 class ConsoleAppender : public StreamAppender
00929 {
00930 protected:
00932 QString target;
00933
00935 const static QString STDOUT;
00936
00938 const static QString STDERR;
00939
00940 public:
00942 ConsoleAppender();
00943
00947 ConsoleAppender( const QString& name );
00948
00954 ConsoleAppender( const QString& name, Layout* lyt );
00955
00962 ConsoleAppender( const QString& name, Layout* lyt, const QString& tget );
00963
00965 ~ConsoleAppender();
00966
00970 void setTarget( const QString& tget );
00971
00975 QString getTarget() const;
00976
00980 void open( QString& name );
00981 };
00982
00986 class FileAppender : public StreamAppender
00987 {
00988 protected:
00990 QString fileName;
00991
00993 QDir directory;
00994
00995 public:
00997 FileAppender();
00998
01003 FileAppender( const QString& name );
01004
01012 FileAppender( const QString& name, Layout* lyt, QString fname );
01013
01016 ~FileAppender();
01017
01021 virtual QString getFilename() const;
01022
01027 virtual void open( QString& name );
01028
01033 void setFile( QString fname );
01034
01038 uint getFileSize() const;
01039
01057 virtual void subAppend( LoggingEvent* evnt );
01058
01060 void setDirectory( QString dir );
01061
01063 QDir getDirectory() const;
01064 };
01065
01070 class RollingFileAppender : public FileAppender
01071 {
01072 protected:
01073
01075 const static uint DEFAULT_MAXFILESIZE;
01076
01079 const static int DEFAULT_MAXBACKUP_INDEX;
01080
01082 uint maxFileSize;
01083
01085 int maxBackupIndex;
01086
01096 void rollOver();
01097
01098 public:
01100 RollingFileAppender();
01101
01105 RollingFileAppender( const QString& name );
01106
01114 RollingFileAppender( const QString& name,
01115 Layout* lyt,
01116 QString filename,
01117 const bool append );
01118
01121 ~RollingFileAppender();
01122
01127 int getMaxBackupIndex() const;
01128
01132 void setMaxBackupIndex( const int maxbackups );
01133
01137 uint getMaxFileSize() const;
01138
01142 void setMaxFileSize( uint maxfilesize );
01143
01160 void subAppend( LoggingEvent* evnt );
01161 };
01162
01167 class SMTPAppender : public AppenderSkeleton
01168 {
01169 public:
01170
01171 SMTPAppender();
01172
01173 ~SMTPAppender();
01174
01175 bool requiresLayout() const;
01176
01177 void append( LoggingEvent* evnt );
01178
01179 bool checkEntryConditions() const;
01180
01181 void subAppend( LoggingEvent* evnt );
01182
01183 };
01184
01188 class SocketAppender : public AppenderSkeleton
01189 {
01190
01191
01192 };
01193
01197 class SyslogAppender : public LayoutAppender
01198 {
01199 protected:
01200
01206 const static int loglevels[8];
01207
01211 QString syslogName;
01212
01220 int facility;
01221
01223 Layout* layout;
01224
01226 virtual void open();
01227
01229 virtual void close();
01230
01234 virtual bool reopen();
01235
01236 public:
01238 SyslogAppender();
01239
01245 SyslogAppender( const QString& name, int fac = LOG_USER );
01246
01253 SyslogAppender( const QString& name, const QString& syslogName,
01254 int fac = LOG_USER );
01255
01257 virtual ~SyslogAppender();
01258
01265 void setSyslogName( const QString& str );
01266
01270 QString getSyslogName() const;
01271
01275 int getFacility() const;
01276
01280 void subAppend( LoggingEvent* evnt );
01281
01289 static int toSyslogLevel( QString p );
01290 };
01291
01296 class BaseCategory
01297 {
01298 public:
01299 BaseCategory();
01300
01301 BaseCategory( const QString name );
01302
01304 virtual bool isCategory() = 0;
01305
01307 virtual ~BaseCategory();
01308
01312 QString getName() const;
01313
01314 protected:
01316 QString name;
01317
01318
01319 };
01320
01382 class Category : public BaseCategory
01383 {
01384 private:
01385
01386 protected:
01388 Category();
01389
01396 Category( const QString name, const QString fqn );
01397
01400 Category( const Category& rhs );
01401
01403 Category* parent;
01404
01406 QString FQN;
01407
01409 static Hierarchy* defaultHierarchy;
01410
01414 QString level;
01415
01417 AppDict appenderList;
01418
01425 bool additive;
01426
01429 bool hasLevel;
01430
01436 QString getLevel() const;
01437
01438 public:
01440 ~Category();
01441
01444 friend class Hierarchy;
01445
01453 static Category* getInstance( const QString name, const QString fqn );
01454
01459 static Category* getInstance( const QString name );
01460
01464 static Hierarchy* getDefaultHierarchy();
01465
01469 static Category* getRootCat();
01470
01474 static void shutdown();
01475
01477 bool isCategory();
01478
01482 Category* getParent() const;
01483
01487 void setParent( Category* p );
01488
01492 QString getFQN() const;
01493
01498 void setFQN( const QString fqn );
01499
01503 bool getAdditivity() const;
01504
01508 void setAdditivity( const bool atv );
01509
01513 void setLevel( const QString lvl );
01514
01521 QString getChainedLevel();
01522
01530 bool isEnabledFor( const QString lvl );
01531
01535 void addAppender( const AppenderSkeleton* apdr );
01536
01541 AppenderSkeleton* getAppender( const QString name ) const;
01542
01548 bool removeAppender( AppenderSkeleton* apdr );
01549
01555 bool removeAppender( QString name );
01556
01561 bool contains( const AppenderSkeleton* apdr ) const;
01562
01567 bool contains( const QString& name ) const;
01568
01574 void callAppenders( LoggingEvent* evnt );
01575
01578 void closeAllAppenders();
01579
01583 void removeAllAppenders();
01584
01588 AppDict* getAppenderList();
01589
01595 void logWithLevel( QString lvl, QString msg );
01596
01600 void debug( QString msg );
01601
01605 void info ( QString msg );
01606
01610 void notice( QString msg );
01611
01615 void warn ( QString msg );
01616
01620 void error( QString msg );
01621
01625 void fatal( QString msg );
01626 };
01627
01632 class RootCategory : public Category
01633 {
01634 private:
01636 const static QString ROOTNAME;
01637
01639 const static QString DEFAULT_LEVEL;
01640
01645 static RootCategory* theInstance;
01646
01650 RootCategory();
01651
01652 public:
01657 static RootCategory* instance();
01658
01662 QString getRootName() const;
01663
01665 ~RootCategory();
01666 };
01667
01674 class ProvisionNode : public BaseCategory
01675 {
01676 private:
01679 ProvisionNode();
01680
01686 ProvisionNode( const QString name, const Category* cat );
01687
01688 public:
01692 friend class Hierarchy;
01693
01698 QPtrList< Category > catList;
01699
01701 ~ProvisionNode();
01702
01707 void addElement( const Category* cat );
01708
01713 Category* elementAt( const uint index );
01714
01719 int getSize() const;
01720
01725 bool isCategory();
01726 };
01727
01749 class Hierarchy
01750 {
01751 private:
01754 static Hierarchy* theInstance;
01755
01757 static RootCategory* rootCat;
01758
01760 static BaseDict hierDict;
01761
01765 static const uint DICTSIZE;
01766
01767
01768
01770 Hierarchy();
01771
01777 Hierarchy( RootCategory* r );
01778
01800 void updateParents( Category* cat );
01801
01818 void updateChildren( ProvisionNode* pn, Category* cat );
01819
01820 public:
01823 static RootCategory* getRootCat();
01824
01826 static Hierarchy* instance();
01827
01834 Category* getCatInstance( const QString name, const QString fqn );
01835
01839 void shutdown();
01840
01842 void testMe();
01843
01847 QString getRootName() const;
01848
01851 ~Hierarchy();
01852 };
01853
01857 class LogException
01858 {
01859 public:
01861 QString str_error;
01862
01864 QString str_detail;
01865
01867 LogException();
01868
01874 LogException( const QString e, const QString d );
01875
01879 QString getError();
01880
01884 QString getDetail();
01885 };
01886
01894 class BasicConfigurator
01895 {
01896 protected:
01898 BasicConfigurator();
01899
01900 public:
01902 ~BasicConfigurator();
01903
01909 static void configure();
01910
01912 static void qmmsLogOutput( QtMsgType type, const char* msg );
01913 };
01914
02026 class PropertyConfigurator : public BasicConfigurator
02027 {
02028 private:
02030 const static QString CATEGORY_PREFIX;
02031
02033 const static QString ROOTCATEGORY_PREFIX;
02034
02036 const static QString APPENDER_PREFIX;
02037
02039 const static QString LEVEL_PREFIX;
02040
02042 const static QString DEFAULT_PROPERTIES;
02043
02045 const static QString DEBUG_PREFIX;
02046
02047 protected:
02051 static AppDict apdrDict;
02052
02057 static StrListMap propMap;
02058
02062 static void parseFile( const QString filename );
02063
02066 static void addLevels();
02067
02069 static void parseAppenders();
02070
02074 static bool verifyAppenderExists( QString name );
02075
02077 static void configureRoot();
02078
02080 static void parseCategories();
02081
02083 static void configureInternalLogging();
02084
02085 public:
02087 PropertyConfigurator();
02088
02090 ~PropertyConfigurator();
02091
02099 static void configure();
02100
02104 static void configure( const QString filename );
02105
02109 static void configureWQt();
02110
02114 static void configureWQt( const QString filename );
02115
02119 static void summarize();
02120 };
02121
02122 }
02123 }
02124
02125 #endif