QsLogDest.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright (c) 2013, Razvan Petru
  2. // All rights reserved.
  3. // Redistribution and use in source and binary forms, with or without modification,
  4. // are permitted provided that the following conditions are met:
  5. // * Redistributions of source code must retain the above copyright notice, this
  6. // list of conditions and the following disclaimer.
  7. // * Redistributions in binary form must reproduce the above copyright notice, this
  8. // list of conditions and the following disclaimer in the documentation and/or other
  9. // materials provided with the distribution.
  10. // * The name of the contributors may not be used to endorse or promote products
  11. // derived from this software without specific prior written permission.
  12. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  13. // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  14. // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  15. // IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  16. // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  17. // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  18. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  19. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  20. // OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  21. // OF THE POSSIBILITY OF SUCH DAMAGE.
  22. #ifndef QSLOGDEST_H
  23. #define QSLOGDEST_H
  24. #include "QsLogLevel.h"
  25. #include <QSharedPointer>
  26. #include <QtGlobal>
  27. class QString;
  28. class QObject;
  29. #ifdef QSLOG_IS_SHARED_LIBRARY
  30. #define QSLOG_SHARED_OBJECT Q_DECL_EXPORT
  31. #elif QSLOG_IS_SHARED_LIBRARY_IMPORT
  32. #define QSLOG_SHARED_OBJECT Q_DECL_IMPORT
  33. #else
  34. #define QSLOG_SHARED_OBJECT
  35. #endif
  36. namespace QsLogging
  37. {
  38. class QSLOG_SHARED_OBJECT Destination
  39. {
  40. public:
  41. typedef void (*LogFunction)(const QString &message, Level level);
  42. virtual void rotate() = 0;
  43. public:
  44. virtual ~Destination();
  45. virtual void write(const QString& message, Level level) = 0;
  46. virtual bool isValid() = 0; // returns whether the destination was created correctly
  47. };
  48. typedef QSharedPointer<Destination> DestinationPtr;
  49. // a series of "named" paramaters, to make the file destination creation more readable
  50. enum LogRotationOption
  51. {
  52. DisableLogRotation = 0,
  53. EnableLogRotation = 1,
  54. EnableLogRotationOnOpen = 2,
  55. };
  56. struct QSLOG_SHARED_OBJECT MaxSizeBytes
  57. {
  58. MaxSizeBytes() : size(0) {}
  59. explicit MaxSizeBytes(qint64 size_) : size(size_) {}
  60. qint64 size;
  61. };
  62. struct QSLOG_SHARED_OBJECT MaxOldLogCount
  63. {
  64. MaxOldLogCount() : count(0) {}
  65. explicit MaxOldLogCount(int count_) : count(count_) {}
  66. int count;
  67. };
  68. //! Creates logging destinations/sinks. The caller shares ownership of the destinations with the logger.
  69. //! After being added to a logger, the caller can discard the pointers.
  70. class QSLOG_SHARED_OBJECT DestinationFactory
  71. {
  72. public:
  73. static DestinationPtr MakeFileDestination(const QString& filePath,
  74. LogRotationOption rotation = DisableLogRotation,
  75. const MaxSizeBytes &sizeInBytesToRotateAfter = MaxSizeBytes(),
  76. const MaxOldLogCount &oldLogsToKeep = MaxOldLogCount());
  77. static DestinationPtr MakeDebugOutputDestination();
  78. // takes a pointer to a function
  79. static DestinationPtr MakeFunctorDestination(Destination::LogFunction f);
  80. // takes a QObject + signal/slot
  81. static DestinationPtr MakeFunctorDestination(QObject *receiver, const char *member);
  82. };
  83. } // end namespace
  84. #endif // QSLOGDEST_H