00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #ifndef LOG4QT_HELPERS_INITIALISATIONHELPER_H
00031 #define LOG4QT_HELPERS_INITIALISATIONHELPER_H
00032
00033
00034
00035
00036
00037
00038 #include <QtCore/QAtomicPointer>
00039 #include <QtCore/QHash>
00040
00041 #if QT_VERSION >= QT_VERSION_CHECK(4, 4, 0)
00042 # ifndef Q_ATOMIC_POINTER_TEST_AND_SET_IS_ALWAYS_NATIVE
00043 # warning "QAtomicPointer test and set is not native. The macros Log4Qt::LOG4QT_GLOBAL_STATIC and Log4Qt::LOG4QT_IMPLEMENT_INSTANCE are not thread-safe."
00044 # endif
00045 #endif
00046
00047
00048
00049
00050
00051
00052 class QMutex;
00053
00054 namespace Log4Qt
00055 {
00111 #if QT_VERSION < QT_VERSION_CHECK(4, 4, 0)
00112 #define LOG4QT_GLOBAL_STATIC(TYPE, FUNCTION) \
00113 static volatile TYPE *sp_global_static_##FUNCTION = 0; \
00114 TYPE *FUNCTION() \
00115 { \
00116 if (!sp_global_static_##FUNCTION) \
00117 { \
00118 TYPE *p_temp = new TYPE; \
00119 if (!q_atomic_test_and_set_ptr(&sp_global_static_##FUNCTION, \
00120 0, p_temp)) \
00121 delete p_temp; \
00122 } \
00123 return const_cast<TYPE *>(sp_global_static_##FUNCTION); \
00124 }
00125 #else
00126 #define LOG4QT_GLOBAL_STATIC(TYPE, FUNCTION) \
00127 static QBasicAtomicPointer<TYPE > sp_global_static_##FUNCTION = \
00128 Q_BASIC_ATOMIC_INITIALIZER(0); \
00129 TYPE *FUNCTION() \
00130 { \
00131 if (!sp_global_static_##FUNCTION) \
00132 { \
00133 TYPE *p_temp = new TYPE; \
00134 if (!sp_global_static_##FUNCTION.testAndSetOrdered(0, \
00135 p_temp)) \
00136 delete p_temp; \
00137 } \
00138 return sp_global_static_##FUNCTION; \
00139 }
00140 #endif
00141
00184 #if QT_VERSION < QT_VERSION_CHECK(4, 4, 0)
00185 #define LOG4QT_IMPLEMENT_INSTANCE(TYPE) \
00186 static TYPE *sp_singleton_##TYPE = 0; \
00187 TYPE *TYPE::instance() \
00188 { \
00189 if (!sp_singleton_##TYPE) \
00190 { \
00191 TYPE *p_temp = new TYPE; \
00192 if (!q_atomic_test_and_set_ptr(&sp_singleton_##TYPE, \
00193 0, p_temp)) \
00194 delete p_temp; \
00195 } \
00196 return sp_singleton_##TYPE; \
00197 }
00198 #else
00199 #define LOG4QT_IMPLEMENT_INSTANCE(TYPE) \
00200 static QBasicAtomicPointer<TYPE > sp_singleton_##TYPE = \
00201 Q_BASIC_ATOMIC_INITIALIZER(0); \
00202 TYPE *TYPE::instance() \
00203 { \
00204 if (!sp_singleton_##TYPE) \
00205 { \
00206 TYPE *p_temp = new TYPE; \
00207 if (!sp_singleton_##TYPE.testAndSetOrdered(0, p_temp)) \
00208 delete p_temp; \
00209 } \
00210 return sp_singleton_##TYPE; \
00211 }
00212 #endif
00213
00234 class InitialisationHelper
00235 {
00236 private:
00237 InitialisationHelper();
00238 InitialisationHelper(const InitialisationHelper &rOther);
00239 virtual ~InitialisationHelper();
00240 InitialisationHelper &operator=(const InitialisationHelper &rOther);
00241
00242 public:
00243
00273 static QHash<QString, QString> environmentSettings();
00274
00278 static InitialisationHelper *instance();
00279
00323 static QString setting(const QString &rKey,
00324 const QString &rDefault = QString());
00325
00334 static qint64 startTime();
00335
00336 private:
00337 void doInitialiseEnvironmentSettings();
00338 void doRegisterTypes();
00339 QString doSetting(const QString &rKey,
00340 const QString &rDefault) const;
00341 static bool shutdown();
00342 static bool staticInitialisation();
00343
00344 private:
00345
00346 const qint64 mStartTime;
00347 QHash <QString, QString> mEnvironmentSettings;
00348 static bool msStaticInitialisation;
00349
00350 #ifndef QT_NO_DEBUG_STREAM
00351
00352 friend QDebug operator<<(QDebug debug,
00353 const InitialisationHelper &rInitialisationHelper);
00354 #endif // QT_NO_DEBUG_STREAM
00355 };
00356
00357
00358
00359
00360
00361
00362 #ifndef QT_NO_DEBUG_STREAM
00363
00377 QDebug operator<<(QDebug debug,
00378 const InitialisationHelper &rInitialisationHelper);
00379 #endif // QT_NO_DEBUG_STREAM
00380
00381
00382
00383
00384
00385
00386 inline QHash<QString, QString> InitialisationHelper::environmentSettings()
00387 {
00388 return instance()->mEnvironmentSettings; }
00389
00390 inline QString InitialisationHelper::setting(const QString &rKey,
00391 const QString &rDefault)
00392 {
00393 return instance()->doSetting(rKey, rDefault); }
00394
00395 inline qint64 InitialisationHelper::startTime()
00396 {
00397 return instance()->mStartTime; }
00398
00399 }
00400
00401
00402
00403
00404
00405 #endif // LOG4QT_HELPERS_INITIALISATIONHELPER_H