1
|
// Copyright (c) 2013, Razvan Petru
|
2
|
// All rights reserved.
|
3
|
|
4
|
// Redistribution and use in source and binary forms, with or without modification,
|
5
|
// are permitted provided that the following conditions are met:
|
6
|
|
7
|
// * Redistributions of source code must retain the above copyright notice, this
|
8
|
// list of conditions and the following disclaimer.
|
9
|
// * Redistributions in binary form must reproduce the above copyright notice, this
|
10
|
// list of conditions and the following disclaimer in the documentation and/or other
|
11
|
// materials provided with the distribution.
|
12
|
// * The name of the contributors may not be used to endorse or promote products
|
13
|
// derived from this software without specific prior written permission.
|
14
|
|
15
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
16
|
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
17
|
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
18
|
// IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
19
|
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
20
|
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
21
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
22
|
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
|
23
|
// OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
24
|
// OF THE POSSIBILITY OF SUCH DAMAGE.
|
25
|
|
26
|
#include "QsLog.h"
|
27
|
#include "QsLogDest.h"
|
28
|
#include "log_example_shared.h"
|
29
|
#include <QLibrary>
|
30
|
#include <QCoreApplication>
|
31
|
#include <QDir>
|
32
|
#include <iostream>
|
33
|
|
34
|
void logFunction(const QString &message, QsLogging::Level level)
|
35
|
{
|
36
|
std::cout << "From log function: " << qPrintable(message) << " " << static_cast<int>(level)
|
37
|
<< std::endl;
|
38
|
}
|
39
|
|
40
|
// This small example shows how QsLog can be used inside a project.
|
41
|
int main(int argc, char *argv[])
|
42
|
{
|
43
|
QCoreApplication a(argc, argv);
|
44
|
|
45
|
using namespace QsLogging;
|
46
|
|
47
|
// 1. init the logging mechanism
|
48
|
Logger& logger = Logger::instance();
|
49
|
logger.setLoggingLevel(QsLogging::TraceLevel);
|
50
|
const QString sLogPath(QDir(a.applicationDirPath()).filePath("log.txt"));
|
51
|
|
52
|
// 2. add two destinations
|
53
|
DestinationPtr fileDestination(DestinationFactory::MakeFileDestination(
|
54
|
sLogPath, EnableLogRotation, MaxSizeBytes(512), MaxOldLogCount(2)));
|
55
|
DestinationPtr debugDestination(DestinationFactory::MakeDebugOutputDestination());
|
56
|
DestinationPtr functorDestination(DestinationFactory::MakeFunctorDestination(&logFunction));
|
57
|
logger.addDestination(debugDestination);
|
58
|
logger.addDestination(fileDestination);
|
59
|
logger.addDestination(functorDestination);
|
60
|
|
61
|
// 3. start logging
|
62
|
QLOG_INFO() << "Program started";
|
63
|
QLOG_INFO() << "Built with Qt" << QT_VERSION_STR << "running on" << qVersion();
|
64
|
|
65
|
QLOG_TRACE() << "Here's a" << QString::fromUtf8("trace") << "message";
|
66
|
QLOG_DEBUG() << "Here's a" << static_cast<int>(QsLogging::DebugLevel) << "message";
|
67
|
QLOG_WARN() << "Uh-oh!";
|
68
|
qDebug() << "This message won't be picked up by the logger";
|
69
|
QLOG_ERROR() << "An error has occurred";
|
70
|
qWarning() << "Neither will this one";
|
71
|
QLOG_FATAL() << "Fatal error!";
|
72
|
|
73
|
logger.setLoggingLevel(QsLogging::OffLevel);
|
74
|
for (int i = 0;i < 10000000;++i) {
|
75
|
QLOG_ERROR() << QString::fromUtf8("this message should not be visible");
|
76
|
}
|
77
|
logger.setLoggingLevel(QsLogging::TraceLevel);
|
78
|
|
79
|
// 4. log from a shared library - should automatically share the same log instance as above
|
80
|
QLibrary myLib("log_example_shared");
|
81
|
typedef LogExampleShared* (*LogExampleGetter)();
|
82
|
typedef void(*LogExampleDeleter)(LogExampleShared*);
|
83
|
LogExampleGetter fLogCreator = (LogExampleGetter) myLib.resolve("createExample");
|
84
|
LogExampleDeleter fLogDeleter = (LogExampleDeleter)myLib.resolve("destroyExample");
|
85
|
LogExampleShared *logFromShared = 0;
|
86
|
if (fLogCreator && fLogDeleter) {
|
87
|
logFromShared = fLogCreator();
|
88
|
logFromShared->logSomething();
|
89
|
fLogDeleter(logFromShared);
|
90
|
} else if (!fLogCreator || !fLogDeleter) {
|
91
|
QLOG_ERROR() << "could not resolve shared library function(s)";
|
92
|
}
|
93
|
|
94
|
QLOG_DEBUG() << "Program ending";
|
95
|
|
96
|
QsLogging::Logger::destroyInstance();
|
97
|
return 0;
|
98
|
}
|