classlogger.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /******************************************************************************
  2. *
  3. * package: Log4Qt
  4. * file: classlogger.cpp
  5. * created: September 2007
  6. * author: Martin Heinrich
  7. *
  8. *
  9. * changes: Sep 2008, Martin Heinrich:
  10. * - Replaced usage of q_atomic_test_and_set_ptr with
  11. * QAtomicPointer
  12. *
  13. *
  14. * Copyright 2007 - 2008 Martin Heinrich
  15. *
  16. * Licensed under the Apache License, Version 2.0 (the "License");
  17. * you may not use this file except in compliance with the License.
  18. * You may obtain a copy of the License at
  19. *
  20. * http://www.apache.org/licenses/LICENSE-2.0
  21. *
  22. * Unless required by applicable law or agreed to in writing, software
  23. * distributed under the License is distributed on an "AS IS" BASIS,
  24. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  25. * See the License for the specific language governing permissions and
  26. * limitations under the License.
  27. *
  28. ******************************************************************************/
  29. /******************************************************************************
  30. * Dependencies
  31. ******************************************************************************/
  32. #include "log4qt/helpers/classlogger.h"
  33. #include <QtCore/QDebug>
  34. #include "log4qt/logmanager.h"
  35. namespace Log4Qt
  36. {
  37. /**************************************************************************
  38. * Declarations
  39. **************************************************************************/
  40. /**************************************************************************
  41. * C helper functions
  42. **************************************************************************/
  43. /**************************************************************************
  44. * Class implementation: ClassLogger
  45. **************************************************************************/
  46. ClassLogger::ClassLogger() :
  47. mpLogger(0)
  48. {
  49. }
  50. Logger *ClassLogger::logger(const QObject *pObject)
  51. {
  52. Q_ASSERT_X(pObject, "ClassLogger::logger()", "pObject must not be null");
  53. #if QT_VERSION < QT_VERSION_CHECK(4, 4, 0)
  54. if (!mpLogger)
  55. q_atomic_test_and_set_ptr(&mpLogger,
  56. 0,
  57. LogManager::logger(QLatin1String(pObject->metaObject()->className())));
  58. return const_cast<Logger *>(mpLogger);
  59. #elif QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
  60. if (!static_cast<Logger *>(mpLogger))
  61. mpLogger.testAndSetOrdered(0,
  62. LogManager::logger(QLatin1String(pObject->metaObject()->className())));
  63. return const_cast<Logger *>(static_cast<Logger *>(mpLogger));
  64. #else
  65. if (!static_cast<Logger *>(mpLogger.loadAcquire()))
  66. mpLogger.testAndSetOrdered(0,
  67. LogManager::logger(QLatin1String(pObject->metaObject()->className())));
  68. return const_cast<Logger *>(static_cast<Logger *>(mpLogger.loadAcquire()));
  69. #endif
  70. }
  71. /**************************************************************************
  72. * Implementation: Operators, Helper
  73. **************************************************************************/
  74. } // namespace Log4Qt