1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0"" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 #include "logoutputimpl.h" |
|
18 #include <QFile> |
|
19 #include <QDir> |
|
20 #include <QFileInfo> |
|
21 #include <QTextStream> |
|
22 #include <QSettings> |
|
23 #include "logger.h" |
|
24 #include "loglogger.h" |
|
25 #include "logoutputfactory.h" |
|
26 |
|
27 #ifdef Q_WS_WIN |
|
28 #include <Windows.h> |
|
29 #endif |
|
30 |
|
31 #ifdef Q_OS_SYMBIAN |
|
32 #include <e32debug.h> |
|
33 #endif |
|
34 |
|
35 //DebugLogOutput |
|
36 REGISTER_OUTPUT_LOG(DEBUGOUTPUT_NAME,DebugLogOutput) |
|
37 |
|
38 DebugLogOutput::DebugLogOutput() |
|
39 { |
|
40 } |
|
41 |
|
42 DebugLogOutput::~DebugLogOutput() |
|
43 { |
|
44 } |
|
45 |
|
46 void DebugLogOutput::output(const QString &log) |
|
47 { |
|
48 #if defined(Q_WS_WIN) |
|
49 QT_WA({ |
|
50 OutputDebugStringW(reinterpret_cast<const WCHAR*>(log.utf16())); |
|
51 }, { |
|
52 OutputDebugStringA(log.toLocal8Bit().data()); |
|
53 }); |
|
54 #elif defined(Q_OS_SYMBIAN) |
|
55 RDebug::Printf(log.toLocal8Bit().data()); |
|
56 #else |
|
57 fprintf(stderr, log.toLocal8Bit().data()); |
|
58 fflush(stderr); |
|
59 #endif |
|
60 } |
|
61 |
|
62 bool DebugLogOutput::init() |
|
63 { |
|
64 return true; |
|
65 } |
|
66 |
|
67 |
|
68 bool DebugLogOutput::doLoad(QSettings &settings) |
|
69 { |
|
70 Q_UNUSED(settings); |
|
71 return true; |
|
72 } |
|
73 |
|
74 |
|
75 //ConsoleLogOutput |
|
76 |
|
77 REGISTER_OUTPUT_LOG(CONSOLEOUTPUT_NAME,ConsoleLogOutput) |
|
78 |
|
79 ConsoleLogOutput::ConsoleLogOutput() |
|
80 { |
|
81 } |
|
82 |
|
83 ConsoleLogOutput::~ConsoleLogOutput() |
|
84 { |
|
85 } |
|
86 |
|
87 void ConsoleLogOutput::output(const QString &log) |
|
88 { |
|
89 printf(log.toLocal8Bit().data()); |
|
90 } |
|
91 |
|
92 bool ConsoleLogOutput::init() |
|
93 { |
|
94 return true; |
|
95 } |
|
96 |
|
97 |
|
98 bool ConsoleLogOutput::doLoad(QSettings &settings) |
|
99 { |
|
100 Q_UNUSED(settings); |
|
101 return true; |
|
102 } |
|
103 |
|
104 //FileLogOutput |
|
105 |
|
106 REGISTER_OUTPUT_LOG(FILEOUTPUT_NAME,FileLogOutput) |
|
107 |
|
108 FileLogOutput::FileLogOutput(const QString &logFilePath /*= QString()*/) : |
|
109 mLogFile(0),mTextStream(0),mLogFilePath(logFilePath),mTruncateFile(false) |
|
110 { |
|
111 } |
|
112 |
|
113 FileLogOutput::~FileLogOutput() |
|
114 { |
|
115 delete mTextStream; |
|
116 delete mLogFile; |
|
117 } |
|
118 |
|
119 void FileLogOutput::output(const QString &log) |
|
120 { |
|
121 *mTextStream << log; |
|
122 mTextStream->flush(); |
|
123 } |
|
124 |
|
125 bool FileLogOutput::init() |
|
126 { |
|
127 if (mTextStream) { |
|
128 delete mTextStream; |
|
129 mTextStream = 0; |
|
130 } |
|
131 |
|
132 if (mLogFile) { |
|
133 delete mLogFile; |
|
134 mLogFile = 0; |
|
135 } |
|
136 |
|
137 QFileInfo fileInfo(mLogFilePath); |
|
138 QDir parentDir = fileInfo.dir(); |
|
139 if (!parentDir.exists()) |
|
140 { |
|
141 QString path = parentDir.absolutePath(); |
|
142 parentDir.cdUp(); |
|
143 parentDir.mkpath(path); |
|
144 } |
|
145 |
|
146 mLogFile = new QFile(mLogFilePath); |
|
147 QFile::OpenMode mode = QIODevice::WriteOnly | QIODevice::Text; |
|
148 |
|
149 if (mTruncateFile) { |
|
150 mode |= QIODevice::Truncate; |
|
151 } |
|
152 else { |
|
153 mode |= QIODevice::Append; |
|
154 } |
|
155 |
|
156 if (!mLogFile->open(mode)) |
|
157 { |
|
158 LOGGER_LOG(QString("FileLogOutput::init << Open open file failed: ") + mLogFilePath); |
|
159 return false; |
|
160 } |
|
161 mTextStream = new QTextStream(mLogFile); |
|
162 return true; |
|
163 } |
|
164 |
|
165 bool FileLogOutput::doLoad(QSettings &settings) |
|
166 { |
|
167 if (!parentLogger()) { |
|
168 return false; |
|
169 } |
|
170 |
|
171 settings.beginGroup(parentLogger()->name()); |
|
172 mLogFilePath = settings.value( |
|
173 FILEOUTPUT_NAME + SETTINGS_SECTION_SEPERATOR + FILEOUTPUT_LOGFILE).toString(); |
|
174 mTruncateFile = settings.value( |
|
175 FILEOUTPUT_NAME + SETTINGS_SECTION_SEPERATOR + FILEOUTPUT_TRUNCATE,QVariant(false)).toBool(); |
|
176 settings.endGroup(); |
|
177 |
|
178 if (mLogFilePath.isEmpty()) { |
|
179 return false; |
|
180 } |
|
181 |
|
182 return true; |
|
183 } |
|
184 |
|
185 bool FileLogOutput::truncate() const |
|
186 { |
|
187 return mTruncateFile; |
|
188 } |
|
189 |
|
190 void FileLogOutput::setTruncate(bool truncate) |
|
191 { |
|
192 mTruncateFile = truncate; |
|
193 } |
|
194 |
|
195 QString FileLogOutput::logFile() const |
|
196 { |
|
197 return mLogFilePath; |
|
198 } |
|
199 |
|
200 void FileLogOutput::setLogFile(const QString &logFile) |
|
201 { |
|
202 mLogFilePath = logFile; |
|
203 } |
|
204 |
|